This repository was archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·436 lines (352 loc) · 13.4 KB
/
script.js
File metadata and controls
executable file
·436 lines (352 loc) · 13.4 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* Load necessary actions in main scope
*/
Config.getAll().then(function (config) {
const isBlocked = isBlockDomain(config.blackListDomain);
let scripts = [
["content/core_helpers.js"],
["content/communication_helpers.js"],
["content/captcha/geetest/interceptor.js", config.isPluginEnabled && config.enabledForGeetest && !isBlocked],
["content/captcha/geetest_v4/interceptor.js", config.isPluginEnabled && config.enabledForGeetest_v4 && !isBlocked],
["content/captcha/hcaptcha/interceptor.js", config.isPluginEnabled && config.enabledForHCaptcha && !isBlocked],
["content/captcha/hcaptcha/hunter.js"],
["content/captcha/keycaptcha/hunter.js"],
["content/captcha/recaptcha/interceptor.js", config.isPluginEnabled && (config.enabledForRecaptchaV2 || config.enabledForInvisibleRecaptchaV2 || config.enabledForRecaptchaV3) && !isBlocked],
["content/captcha/recaptcha/hunter.js"],
["content/captcha/arkoselabs/interceptor.js", config.isPluginEnabled && config.enabledForArkoselabs && !isBlocked],
["content/captcha/arkoselabs/hunter.js"],
["content/captcha/lemin/interceptor.js", config.isPluginEnabled && config.enabledForLemin && !isBlocked],
["content/captcha/yandex/interceptor.js", config.isPluginEnabled && config.enabledForYandex && !isBlocked],
["content/captcha/amazon_waf/interceptor.js", config.isPluginEnabled && config.enabledForAmazonWaf && !isBlocked],
["content/captcha/turnstile/hunter.js"]
];
scripts.forEach(s => {
if (s.length > 1 && !s[1]) return;
let script = document.createElement('script');
script.src = chrome.runtime.getURL(s[0]);
(document.head || document.documentElement).prepend(script);
});
});
/*
* Captcha Processors Repository
*/
var CaptchaProcessors = {
list: {},
register: function (processor) {
this.list[processor.captchaType] = processor;
},
get: function (captchaType) {
return this.list[captchaType];
},
};
/*
* Main loop.
* It iterates over found captcha widgets and processes them.
*/
let CAPTCHA_WIDGETS_LOOP = setInterval(function () {
Config.getAll().then(config => {
if (!config.isPluginEnabled) return;
if (config.apiKey === null) return;
if (isBlockDomain(config.blackListDomain)) return;
$("head").find("captcha-widget").each(function () {
let widget = $(this);
let widgetInfo = prepareWidgetInfo(widget[0].dataset);
if (widgetInfo.reset) {
getSolverButton(widgetInfo.captchaType, widgetInfo.widgetId).remove();
widget.removeAttr("data-loaded");
widget.removeAttr("data-reset");
}
if (widgetInfo.loaded) return;
let processor = CaptchaProcessors.get(widgetInfo.captchaType);
if (processor.canBeProcessed(widgetInfo, config)) {
let button = createSolverButton(widgetInfo.captchaType, widgetInfo.widgetId, config);
processor.attachButton(widgetInfo, config, button);
widget[0].dataset.loaded = true;
}
});
});
}, 2000);
/*
* Background communication
*/
var background = chrome.runtime.connect({ name: "content" });
background.onMessage.addListener(function (msg) {
if (msg.action == "solve") {
if (msg.request.messageId) {
return respondToWebPageMessage(msg);
}
let button = getSolverButton(msg.request.captchaType, msg.request.widgetId);
if (msg.error === undefined) {
changeSolverButtonState(button, "solved", chrome.i18n.getMessage("solved"));
doActionsOnSuccess(msg);
} else {
changeSolverButtonState(button, "error", msg.error);
tryAgain(button);
}
}
});
background.onDisconnect.addListener(function (port) {
clearInterval(CAPTCHA_WIDGETS_LOOP);
});
function doActionsOnSuccess(msg) {
let widget = getWidgetInfo(msg.request.captchaType, msg.request.widgetId);
let processor = CaptchaProcessors.get(msg.request.captchaType);
processor.onSolved(widget, msg.response.code);
Config.getAll().then(config => {
let callback = processor.getCallback(widget);
if (callback) {
let textarea = document.createElement('textarea');
textarea.id = 'twocaptcha-callback-trigger';
textarea.setAttribute('data-function', callback);
textarea.value = msg.response.code;
document.body.appendChild(textarea);
}
if (config.autoSubmitForms === true) {
let timeout = parseInt(config.submitFormsDelay) * 1000;
setTimeout(function () {
if (!executeAutoSubmitRule(config.autoSubmitRules)) {
processor.getForm(widget).submit();
}
}, timeout);
}
});
}
function isBlockDomain(blackListDomain) {
const domains = blackListDomain.split("\n") || [];
for (let i = 0; i < domains.length; i++) {
if (domains[i]) {
const regExp = new RegExp(domains[i], 'i');
if (regExp.test(location.href)) {
return true;
}
}
}
return false;
}
function executeAutoSubmitRule(rules) {
for (let i = 0; i < rules.length; i++) {
let regExp = new RegExp(rules[i].url_pattern);
if (regExp.test(location.href)) {
let textarea = document.createElement('textarea');
textarea.id = 'twocaptcha-autosubmit-code';
textarea.value = rules[i].code;
document.body.appendChild(textarea);
return true;
}
}
return false;
}
function executeAutoSubmitRuleCode(code) {
}
function tryAgain(button) {
Config.getAll().then(config => {
let countErrors = parseInt(button[0].dataset.countErrors || 0);
if (config.repeatOnErrorTimes >= countErrors) {
setTimeout(function () {
button.click();
}, config.repeatOnErrorDelay * 1000);
}
});
}
function attachProxyParams(params, config) {
if (!config.useProxy) return;
let proxy = config.proxy.trim();
if (!proxy.length) return;
params.proxy = {
type: config.proxytype,
uri: proxy,
};
}
/*
* Solver button
*/
function createSolverButton(captchaType, widgetId, config) {
let button = $(`
<div class="captcha-solver captcha-solver_${config.buttonPosition}" data-state="ready" data-captcha-type="${captchaType}" data-widget-id="${widgetId}">
<div class="captcha-solver-image">
<img src="${chrome.runtime.getURL("assets/images/icon_32.png")}">
</div>
<div class="captcha-solver-info">${chrome.i18n.getMessage("solveWith2Captcha")}</div>
</div>
`);
button.click(function () {
if (!["ready", "error"].includes(button.attr("data-state"))) return;
if (button[0].dataset.countErrors && button[0].dataset.disposable) {
return changeSolverButtonState(button, "error", "EXPIRED");
}
changeSolverButtonState(button, "solving", chrome.i18n.getMessage("solving"));
let widget = getWidgetInfo(captchaType, widgetId);
Config.getAll().then(function (config) {
let params = CaptchaProcessors.get(captchaType).getParams(widget, config);
attachProxyParams(params, config);
background.postMessage({
action: "solve",
captchaType: captchaType,
widgetId: widgetId,
params: params,
});
});
});
return button;
}
function changeSolverButtonState(button, state, message) {
button.attr("data-state", state)
button.find(".captcha-solver-info").text(message);
if (state === "error") {
button[0].dataset.countErrors = parseInt(button[0].dataset.countErrors || 0) + 1
}
}
function getSolverButton(captchaType, widgetId) {
return $(".captcha-solver[data-captcha-type=" + captchaType + "][data-widget-id=" + widgetId + "]");
}
function getWidgetInfo(captchaType, widgetId) {
let widget = $("head").find("captcha-widget[data-captcha-type=" + captchaType + "][data-widget-id=" + widgetId + "]");
if (!widget.length) return null;
return prepareWidgetInfo(widget[0].dataset);
}
function prepareWidgetInfo(dataset) {
let w = {};
for (let k in dataset) {
w[k] = dataset[k];
if (w[k] === "null") w[k] = null;
if (w[k] === "false") w[k] = false;
if (w[k] === "true") w[k] = true;
}
return w;
}
/*
* Communication with web page
*/
let webPageMsgInterval = setInterval(function () {
$("body > solver-ext-messages").children().each(function () {
let msg = $(this)[0];
if (!msg.dataset.received) {
msg.dataset.received = true;
if (msg.dataset.action === "getConfig") {
Config.getAll().then(config => {
setWebPageMessageResponse(msg, config);
}).catch(e => {
setWebPageMessageResponse(msg, { error: e.message });
});
} else if (msg.dataset.action === "solve") {
let data = JSON.parse(decodeURIComponent(msg.dataset.data));
background.postMessage({
action: "solve",
captchaType: data.captchaType,
widgetId: data.widgetId,
params: data.params,
messageId: msg.dataset.messageId,
});
} else if (msg.dataset.action === "getRecaptchaV3InterceptorInfo") {
setWebPageMessageResponse(msg, {
extId: chrome.runtime.id,
i18n: {
solving: chrome.i18n.getMessage("solving"),
solved: chrome.i18n.getMessage("solved"),
}
});
} else {
setWebPageMessageResponse(msg, { error: "unknown_action" });
}
}
});
}, 200);
function respondToWebPageMessage(msg) {
let message = $("body > solver-ext-messages > solver-ext-message[data-message-id=" + msg.request.messageId + "]");
if (!message.length) return;
if (msg.error) {
setWebPageMessageResponse(message[0], { error: msg.error });
} else {
setWebPageMessageResponse(message[0], { response: msg.response.code });
}
}
function setWebPageMessageResponse(message, response) {
message.dataset.response = encodeURIComponent(JSON.stringify(response));
}
/*
* ContextMenu helper
*/
let contextMenuEl = null;
document.addEventListener("contextmenu", function (event) {
contextMenuEl = event.target;
}, true);
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.command == "getContextMenuEl") {
sendResponse({ xpath: getXPath(contextMenuEl) });
if (request.element == 'input') {
$('.twocaptcha-toast .close').click();
} else if (request.showManual) {
showToast(chrome.i18n.getMessage("normalManual"));
}
}
});
function showToast(message) {
if (!$('body > .twocaptcha-toast-container').length) {
$('body').append(`<div class="twocaptcha-toast-container"></div>`);
}
let toastEl = $(`
<div class="twocaptcha-toast">
<img src="${chrome.runtime.getURL("assets/images/logo.svg")}" class="twocaptcha-toast-logo">
<button type="button" class="close">×</button>
<div class="twocaptcha-toast-message">${message}</div>
</div>
`).appendTo('.twocaptcha-toast-container');
setTimeout(function () {
toastEl.addClass('visible');
}, 50);
toastEl.find('button.close').click(function (e) {
toastEl.removeClass('visible');
setTimeout(function () {
toastEl.remove();
}, 300);
});
}
function getXPath(node) {
var comp, comps = [];
var parent = null;
var xpath = '';
var getPos = function (node) {
var position = 1, curNode;
if (node.nodeType == Node.ATTRIBUTE_NODE) {
return null;
}
for (curNode = node.previousSibling; curNode; curNode = curNode.previousSibling) {
if (curNode.nodeName == node.nodeName) {
++position;
}
}
return position;
}
if (node instanceof Document) {
return '/';
}
for (; node && !(node instanceof Document); node = node.nodeType == Node.ATTRIBUTE_NODE ? node.ownerElement : node.parentNode) {
comp = comps[comps.length] = {};
switch (node.nodeType) {
case Node.TEXT_NODE:
comp.name = 'text()';
break;
case Node.ATTRIBUTE_NODE:
comp.name = '@' + node.nodeName;
break;
case Node.PROCESSING_INSTRUCTION_NODE:
comp.name = 'processing-instruction()';
break;
case Node.COMMENT_NODE:
comp.name = 'comment()';
break;
case Node.ELEMENT_NODE:
comp.name = node.nodeName;
break;
}
comp.position = getPos(node);
}
for (var i = comps.length - 1; i >= 0; i--) {
comp = comps[i];
xpath += '/' + comp.name;
if (comp.position != null) {
xpath += '[' + comp.position + ']';
}
}
return xpath;
}