-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwidget.js
More file actions
149 lines (138 loc) · 6.75 KB
/
widget.js
File metadata and controls
149 lines (138 loc) · 6.75 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
(function () {
'use strict';
function onReady(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
function getSelfSrc() {
var cs = document.currentScript;
if (cs && cs.src) return cs.src;
var scripts = document.getElementsByTagName('script');
for (var i = scripts.length - 1; i >= 0; i--) {
if (scripts[i] && scripts[i].src && /\/widget\.js(\?|#|$)/.test(scripts[i].src)) return scripts[i].src;
}
return '';
}
function resolveAssetUrl(assetPath) {
try {
var base = getSelfSrc();
if (!base) return assetPath;
var u = new URL(base);
return u.origin + assetPath;
} catch (_) {
return assetPath;
}
}
onReady(function () {
var mount = document.getElementById('smartcaptcha');
if (!mount) return;
var cssUrl = resolveAssetUrl('/style.css?v=2');
var jsUrl = resolveAssetUrl('/smartcaptcha.js?v=2');
var fbUrl = resolveAssetUrl('/firebaseFeedback.js?v=2');
var fbModalUrl = resolveAssetUrl('/feedbackModal.js?v=2');
mount.innerHTML = '';
var token = String(Math.random()).slice(2) + String(Date.now());
var iframe = document.createElement('iframe');
iframe.title = 'SmartCAPTCHA';
iframe.setAttribute('scrolling', 'no');
iframe.style.border = '0';
iframe.style.width = '100%';
iframe.style.height = '420px';
iframe.style.display = 'block';
function onMsg(e) {
try {
if (!e || e.source !== iframe.contentWindow) return;
var d = e.data;
if (!d || d.__scw_token !== token || d.__scw_type !== 'resize') return;
var h = Number(d.h);
if (!isFinite(h) || h <= 0) return;
iframe.style.height = Math.max(220, Math.min(1200, Math.ceil(h))) + 'px';
} catch (_) {
}
}
window.addEventListener('message', onMsg);
var doc = '';
doc += '<!doctype html>';
doc += '<html lang="en">';
doc += '<head>';
doc += '<meta charset="utf-8" />';
doc += '<meta name="viewport" content="width=device-width, initial-scale=1" />';
doc += '<link rel="stylesheet" href="' + cssUrl + '" />';
doc += '<style>';
doc += 'html,body{margin:0;padding:0;}';
doc += '.sc-embed{min-height:1px;}';
doc += '</style>';
doc += '</head>';
doc += '<body>';
doc += '<div class="sc-embed">';
doc += '<main class="page">';
doc += '<h1 class="title">SmartCAPTCHA</h1>';
doc += '<p class="subtitle">Complete the slider. The widget will send behavioral features to the deployed verifier service.</p>';
doc += '<section id="smartcaptcha-root" class="captcha-root"></section>';
doc += '<section class="result">';
doc += '<div id="smartcaptcha-status" class="status" aria-live="polite"></div>';
doc += '<div class="actions">';
doc += '<button id="smartcaptcha-reset" class="btn" type="button">Reset</button>';
doc += '</div>';
doc += '</section>';
doc += '</main>';
doc += '<footer class="sc-footer">';
doc += '<a id="smartcaptcha-feedback" class="sc-footer-link" href="#">Feedback</a>';
doc += '</footer>';
doc += '<div id="sc-feedback-modal" class="sc-modal" hidden>';
doc += '<div class="sc-modal__backdrop" data-sc-close="true"></div>';
doc += '<div class="sc-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="sc-feedback-title">';
doc += '<button type="button" class="sc-modal__close" data-sc-close="true" aria-label="Close">×</button>';
doc += '<h2 id="sc-feedback-title" class="sc-modal__title">Feedback</h2>';
doc += '<form id="sc-feedback-form" class="sc-modal__form">';
doc += '<label class="sc-modal__label">';
doc += '<span>Email *</span>';
doc += '<input id="sc-feedback-email" class="sc-modal__input" type="email" required autocomplete="email" />';
doc += '</label>';
doc += '<label class="sc-modal__label">';
doc += '<span>Message</span>';
doc += '<textarea id="sc-feedback-message" class="sc-modal__textarea" rows="4"></textarea>';
doc += '</label>';
doc += '<div id="sc-feedback-status" class="sc-modal__status" aria-live="polite"></div>';
doc += '<div class="sc-modal__actions">';
doc += '<button id="sc-feedback-submit" class="btn" type="submit">Send</button>';
doc += '</div>';
doc += '</form>';
doc += '</div>';
doc += '</div>';
doc += '</div>';
doc += '<script>';
doc += '(function(){';
doc += 'var T="' + token + '";';
doc += 'function send(){try{var h=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight);parent.postMessage({__scw_type:"resize",__scw_token:T,h:h},"*");}catch(_){}}';
doc += 'window.addEventListener("load",send);window.addEventListener("resize",send);';
doc += 'new MutationObserver(send).observe(document.documentElement,{subtree:true,childList:true,attributes:true,characterData:true});';
doc += 'setInterval(send,800);';
doc += '})();';
doc += '<\/script>';
doc += '<script>';
doc += '(function(){';
doc += 'function safePost(payload){try{window.parent&&window.parent.postMessage&&window.parent.postMessage(payload,"*");}catch(_){}}';
doc += 'function genToken(){try{if(window.crypto&&window.crypto.getRandomValues){var a=new Uint8Array(16);window.crypto.getRandomValues(a);var s="";for(var i=0;i<a.length;i++){s+=a[i].toString(16).padStart(2,"0");}return s;} }catch(_){};return (String(Math.random()).slice(2)+String(Date.now()));}';
doc += 'var sentOk=false;var sentFail=false;';
doc += 'function check(){try{var el=document.getElementById("smartcaptcha-status");if(!el)return;var t=(el.textContent||"").trim();';
doc += 'if(t==="Verified: Human"&&!sentOk){sentOk=true;sentFail=false;var tok=window.__SMARTCAPTCHA_WIDGET_VERIFICATION_TOKEN__||"";if(!tok){tok=genToken();window.__SMARTCAPTCHA_WIDGET_VERIFICATION_TOKEN__=tok;}safePost({type:"SMARTCAPTCHA_VERIFIED",success:true,token:String(tok)});}';
doc += 'if(t==="Verification failed. Try again."&&!sentFail){sentFail=true;sentOk=false;safePost({type:"SMARTCAPTCHA_FAILED",success:false});}';
doc += 'if(t===""||t==="Try again."||t==="Verifying..."){sentFail=false;}';
doc += '}catch(_){}}';
doc += 'function start(){try{check();var el=document.getElementById("smartcaptcha-status");if(!el)return;new MutationObserver(check).observe(el,{subtree:true,childList:true,characterData:true});}catch(_){}}';
doc += 'if(document.readyState==="loading"){document.addEventListener("DOMContentLoaded",start);}else{start();}';
doc += '})();';
doc += '<\/script>';
doc += '<script src="' + jsUrl + '"><\/script>';
doc += '<script src="' + fbUrl + '"><\/script>';
doc += '<script src="' + fbModalUrl + '"><\/script>';
doc += '</body>';
doc += '</html>';
iframe.srcdoc = doc;
mount.appendChild(iframe);
});
})();