forked from NopeCHALLC/nopecha-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recaptcha_speech.js
184 lines (145 loc) · 6.04 KB
/
recaptcha_speech.js
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
(async () => {
function is_widget_frame() {
return document.querySelector('.recaptcha-checkbox') !== null;
}
function is_image_frame() {
return document.querySelector('.rc-imageselect-instructions') !== null;
}
function is_speech_frame() {
return document.querySelector('#audio-instructions') !== null || document.querySelector('.rc-doscaptcha-header') !== null;
}
function open_image_frame() {
document.querySelector('#recaptcha-anchor')?.click();
}
function is_solved() {
if (got_solve_error()) {
return false;
}
const is_widget_frame_solved = document.querySelector('.recaptcha-checkbox')?.getAttribute('aria-checked') === 'true';
const is_image_frame_solved = document.querySelector('#recaptcha-verify-button')?.disabled;
return is_widget_frame_solved || is_image_frame_solved;
}
function submit() {
document.querySelector('#recaptcha-verify-button')?.click();
}
function got_solve_error() {
return document.querySelector('.rc-doscaptcha-header')?.innerText === 'Try again later';
}
async function on_widget_frame(settings) {
// Check if parent frame marked this frame as visible on screen
const is_visible = await BG.exec('Cache.get', {name: 'recaptcha_widget_visible', tab_specific: true});
if (is_visible !== true) {
return;
}
// Wait if already solved
if (is_solved()) {
return;
}
await Time.sleep(500);
open_image_frame();
}
async function on_image_frame(settings) {
// Check if parent frame marked this frame as visible on screen
const is_visible = await BG.exec('Cache.get', {name: 'recaptcha_image_visible', tab_specific: true});
if (is_visible !== true) {
return;
}
// Wait if already solved
if (is_solved()) {
return;
}
// Switch to speech
await Time.sleep(500);
document.querySelector('#recaptcha-audio-button')?.click();
}
async function on_speech_frame(settings) {
// Check if parent frame marked this frame as visible on screen
const is_visible = await BG.exec('Cache.get', {name: 'recaptcha_image_visible', tab_specific: true});
if (is_visible !== true) {
return;
}
// Wait if verify button is disabled
if (is_solved()) {
return;
}
// Try again later error
if (got_solve_error()) {
// await BG.exec('set_settings', {id: 'solve_method', value: 'image'});
// await BG.exec('reset_recaptcha');
return;
}
const dl_url = document.querySelector('.rc-audiochallenge-tdownload-link')?.href;
const r = fetch(dl_url);
const src_url = document.querySelector('#audio-source')?.src?.replace('recaptcha.net', 'google.com');
let lang = document.querySelector('html')?.getAttribute('lang')?.trim();
if (!lang || lang.length === 0) {
lang = 'en';
}
const solve_start = Time.time();
const res = await Net.fetch('https://engageub.pythonanywhere.com', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'input=' + encodeURIComponent(src_url) + '&lang=' + lang,
});
document.querySelector('#audio-response').value = res;
let delay = parseInt(settings.recaptcha_solve_delay_time);
delay = delay ? delay : 1000;
const delta = settings.recaptcha_solve_delay ? (delay - (Time.time() - solve_start)) : 0;
if (delta > 0) {
await Time.sleep(delta);
}
submit();
}
async function check_image_frame_visibility() {
const $image_frames = [
...document.querySelectorAll('iframe[src*="/recaptcha/api2/bframe"]'),
...document.querySelectorAll('iframe[src*="/recaptcha/enterprise/bframe"]'),
];
if ($image_frames.length > 0) {
for (const $frame of $image_frames) {
if (window.getComputedStyle($frame).visibility === 'visible') {
return await BG.exec('Cache.set', {name: 'recaptcha_image_visible', value: true, tab_specific: true});
}
}
await BG.exec('Cache.set', {name: 'recaptcha_image_visible', value: false, tab_specific: true});
}
}
async function check_widget_frame_visibility() {
const $widget_frames = [
...document.querySelectorAll('iframe[src*="/recaptcha/api2/anchor"]'),
...document.querySelectorAll('iframe[src*="/recaptcha/enterprise/anchor"]'),
];
if ($widget_frames.length > 0) {
for (const $frame of $widget_frames) {
if (window.getComputedStyle($frame).visibility === 'visible') {
return await BG.exec('Cache.set', {name: 'recaptcha_widget_visible', value: true, tab_specific: true});
}
}
await BG.exec('Cache.set', {name: 'recaptcha_widget_visible', value: false, tab_specific: true});
}
return false;
}
while (true) {
await Time.sleep(1000);
const settings = await BG.exec('Settings.get');
// Using another solve method
if (!settings || !settings.enabled || settings.recaptcha_solve_method !== 'Speech') {
continue;
}
const hostname = await Location.hostname();
if (settings.disabled_hosts.includes(hostname)) {
continue;
}
await check_image_frame_visibility();
await check_widget_frame_visibility();
if (settings.recaptcha_auto_open && is_widget_frame()) {
await on_widget_frame(settings);
}
else if (settings.recaptcha_auto_solve && is_image_frame()) {
await on_image_frame(settings);
}
else if (settings.recaptcha_auto_solve && is_speech_frame()) {
await on_speech_frame(settings);
}
}
})();