-
Notifications
You must be signed in to change notification settings - Fork 99
/
textcaptcha.js
176 lines (148 loc) · 5.26 KB
/
textcaptcha.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
(async () => {
function is_present(settings) {
try {
if (settings?.textcaptcha_image_selector && settings?.textcaptcha_input_selector) {
const $image = document.querySelector(settings.textcaptcha_image_selector);
if (!$image) {
return false;
}
const $input = document.querySelector(settings.textcaptcha_input_selector);
if (!$input || $input.value) {
return false;
}
return true;
}
} catch (e) {}
return false;
}
async function get_image_data(selector) {
function get_style_image_url($e) {
let bg = $e.style.backgroundImage;
if (bg) {
const matches = bg.trim().match(/(?!^)".*?"/g);
if (!matches || matches.length === 0) {
bg = null;
}
bg = matches[0].replaceAll('"', '');
}
return bg;
}
function get_style_image_elem($e) {
return new Promise(resolve => {
const $img = new Image();
$img.onload = () => resolve($img);
$img.src = get_style_image_url($e);
});
}
async function get_canvas(selector) {
const $e = document.querySelector(selector);
// Canvas
if ($e instanceof HTMLCanvasElement) {
// console.log('found canvas element', $e);
return $e;
}
let $img;
if ($e instanceof HTMLImageElement) {
// console.log('found image element', $e);
$img = $e;
}
else {
// console.log('found background image', $e);
$img = await get_style_image_elem($e);
}
if (!$img) {
throw Error(`failed to get image element for ${selector}`);
}
const $canvas = document.createElement('canvas');
$canvas.width = $img.naturalWidth;
$canvas.height = $img.naturalHeight;
const ctx = $canvas.getContext('2d');
ctx.drawImage($img, 0, 0);
return $canvas;
}
try {
const $canvas = await get_canvas(selector);
return $canvas.toDataURL('image/jpeg').split(';base64,')[1];
} catch (e) {
// console.error('failed to encode image data', e);
return null;
}
}
let last_image_data = null;
function on_task_ready(i=500) {
return new Promise(resolve => {
let checking = false;
const check_interval = setInterval(async () => {
if (checking) {
return;
}
checking = true;
const settings = await BG.exec('Settings.get');
if (!settings.textcaptcha_auto_solve) {
checking = false;
return;
}
const image_data = await get_image_data(settings.textcaptcha_image_selector);
console.log('image_data', image_data);
if (!image_data) {
checking = false;
return;
}
if (last_image_data === image_data) {
checking = false;
return;
}
last_image_data = image_data;
clearInterval(check_interval);
checking = false;
return resolve({image_data});
}, i);
});
}
async function on_present() {
const {image_data} = await on_task_ready();
// console.log('image_data', image_data);
const settings = await BG.exec('Settings.get');
if (!settings.enabled || !settings.textcaptcha_auto_solve) {
return;
}
const solve_start = Time.time();
// Detect images
const {job_id, data} = await NopeCHA.post({
captcha_type: IS_DEVELOPMENT ? 'textcaptcha_dev' : 'textcaptcha',
image_data: [image_data],
key: settings.key,
});
if (!data) {
return;
}
console.log('data', data);
let delay = parseInt(settings.textcaptcha_solve_delay_time);
delay = delay ? delay : 100;
const delta = settings.textcaptcha_solve_delay ? (delay - (Time.time() - solve_start)) : 0;
if (delta > 0) {
await Time.sleep(delta);
}
// Fill input
if (data && data.length > 0) {
const $input = document.querySelector(settings.textcaptcha_input_selector);
if ($input && !$input.value) {
$input.value = data[0];
}
}
}
while (true) {
await Time.sleep(1000);
const settings = await BG.exec('Settings.get');
if (!settings || !settings.enabled) {
continue;
}
const hostname = await Location.hostname();
if (settings.disabled_hosts.includes(hostname)) {
continue;
}
if (settings.textcaptcha_auto_solve && is_present(settings)) {
await on_present();
}
}
})();