-
Notifications
You must be signed in to change notification settings - Fork 102
/
content.js
210 lines (184 loc) · 6.12 KB
/
content.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
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
class BG {
static exec() {
return new Promise(resolve => {
try {
// console.log('exec', arguments);
chrome.runtime.sendMessage([...arguments], resolve);
} catch (e) {
console.log('exec failed', e);
resolve(null);
}
});
}
}
class Net {
static async fetch(url, options) {
return await BG.exec('Net.fetch', {url, options});
}
}
class Script {
static inject_file(file) {
return new Promise(resolve => {
const $script = document.createElement('script');
$script.src = chrome.runtime.getURL(file);
$script.onload = resolve;
(document.head || document.documentElement).appendChild($script);
});
}
}
class Location {
static parse_hostname(url) {
return url.replace(/^(.*:)\/\/([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$/, '$2');
}
static async hostname() {
const tab = await BG.exec('Tab.info');
const tab_url = tab.url ? tab.url : 'Unknown Host';
return Location.parse_hostname(tab_url);
}
}
class Image {
static encode(url) {
return new Promise(resolve => {
if (url === null) {
return resolve(null);
}
const xhr = new XMLHttpRequest();
xhr.onload = () => {
const reader = new FileReader();
reader.onloadend = () => {
let res = reader.result;
if (res.startsWith('data:text/html;base64,')) {
return resolve(null);
}
res = res.replace('data:image/jpeg;base64,', '');
resolve(res);
};
reader.readAsDataURL(xhr.response);
};
xhr.onerror = () => {
resolve(null);
};
xhr.onreadystatechange = () => {
if (this.readyState == 4 && this.status != 200) {
resolve(null);
}
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
});
}
}
class NopeCHA {
static INFERENCE_URL = 'https://api.nopecha.com';
static MAX_WAIT_POST = 60;
static MAX_WAIT_GET = 60;
static ERRORS = {
UNKNOWN: 9,
INVALID_REQUEST: 10,
RATE_LIIMTED: 11,
BANNED_USER: 12,
NO_JOB: 13,
INCOMPLETE_JOB: 14,
INVALID_KEY: 15,
NO_CREDIT: 16,
UPDATE_REQUIRED: 17,
};
static async post({captcha_type, task, image_urls, image_data, grid, audio_data, key}) {
const start_time = Date.now();
const info = await BG.exec('Tab.info');
while (true) {
const now = Date.now();
if (now - start_time > NopeCHA.MAX_WAIT_POST * 1000) {
break;
}
const data = {
type: captcha_type,
task: task,
key: key,
v: chrome.runtime.getManifest().version,
url: info ? info.url : window.location.href,
};
if (image_urls) {
data.image_urls = image_urls;
}
if (image_data) {
data.image_data = image_data;
}
if (grid) {
data.grid = grid;
}
if (audio_data) {
data.audio_data = audio_data;
}
try {
const headers = {'Content-Type': 'application/json'};
if (key && key !== 'undefined') {
headers.Authorization = `Bearer ${key}`;
}
const text = await Net.fetch(NopeCHA.INFERENCE_URL, {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
});
const r = JSON.parse(text);
if ('error' in r) {
if (r.error === NopeCHA.ERRORS.RATE_LIMITED) {
await Time.sleep(2000);
continue;
}
else if (r.error === NopeCHA.ERRORS.INVALID_KEY) {
console.log('solve error. invalid key');
break;
}
else if (r.error === NopeCHA.ERRORS.NO_CREDIT) {
console.log('solve error. out of credit');
break;
}
else {
console.log('unknown error', r.error);
break
}
}
const job_id = r.data;
return await NopeCHA.get({job_id, key});
} catch (e) {
console.log('failed to parse post response', e);
// break;
}
}
return {job_id: null, data: null};
}
static async get({job_id, key}) {
const start_time = Date.now();
while (true) {
const now = Date.now();
if (now - start_time > NopeCHA.MAX_WAIT_GET * 1000) {
break;
}
await Time.sleep(1000);
const headers = {};
if (key && key !== 'undefined') {
headers.Authorization = `Bearer ${key}`;
}
const text = await Net.fetch(`${NopeCHA.INFERENCE_URL}?id=${job_id}&key=${key}`, {
headers: headers,
});
try {
const r = JSON.parse(text);
if ('error' in r) {
// TODO: handle errors
if (r.error !== NopeCHA.ERRORS.INCOMPLETE_JOB) {
return {job_id, data: null, metadata: null};
}
continue;
}
return {job_id, data: r.data, metadata: r.metadata};
} catch (e) {
console.log('failed to parse server response for solution', e);
// break;
}
}
return {job_id, data: null, metadata: null};
}
}