This repository was archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbackground.js
362 lines (362 loc) · 9.78 KB
/
background.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
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
;(() => {
function t (t) {
return JSON.parse(JSON.stringify(t))
}
class s {
static cache = {}
static async set ({
tab_id: t,
data: { name: a, value: e, tab_specific: c }
}) {
return c && (a = t + '_' + a), (s.cache[a] = e), s.cache[a]
}
static async get ({ tab_id: t, data: { name: a, tab_specific: e } }) {
return e && (a = t + '_' + a), s.cache[a]
}
static async remove ({ tab_id: t, data: { name: a, tab_specific: e } }) {
e && (a = t + '_' + a)
e = s.cache[a]
return delete s.cache[a], e
}
static async append ({
tab_id: t,
data: { name: a, value: e, tab_specific: c }
}) {
return (
(a = c ? t + '_' + a : a) in s.cache || (s.cache[a] = []),
s.cache[a].push(e),
s.cache[a]
)
}
static async empty ({ tab_id: t, data: { name: a, tab_specific: e } }) {
e && (a = t + '_' + a)
e = s.cache[a]
return (s.cache[a] = []), e
}
static async inc ({ tab_id: t, data: { name: a, tab_specific: e } }) {
return (
(a = e ? t + '_' + a : a) in s.cache || (s.cache[a] = 0),
s.cache[a]++,
s.cache[a]
)
}
static async dec ({ tab_id: t, data: { name: a, tab_specific: e } }) {
return (
(a = e ? t + '_' + a : a) in s.cache || (s.cache[a] = 0),
s.cache[a]--,
s.cache[a]
)
}
static async zero ({ tab_id: t, data: { name: a, tab_specific: e } }) {
return e && (a = t + '_' + a), (s.cache[a] = 0), s.cache[a]
}
}
class n {
static reloads = {}
static _reload ({ tab_id: a }) {
return new Promise(t => chrome.tabs.reload(a, { bypassCache: true }, t))
}
static async reload ({
tab_id: t,
data: { delay: a, overwrite: e } = { delay: 0, overwrite: true }
}) {
a = parseInt(a)
let c = n.reloads[t]?.delay - (Date.now() - n.reloads[t]?.start)
return (
(c = isNaN(c) || c < 0 ? 0 : c),
!!(e || 0 == c || a <= c) &&
(clearTimeout(n.reloads[t]?.timer),
(n.reloads[t] = {
delay: a,
start: Date.now(),
timer: setTimeout(() => n._reload({ tab_id: t }), a)
}),
true)
)
}
static close ({ tab_id: a }) {
return new Promise(t => chrome.tabs.remove(a, t))
}
static async open ({ data: { url: t } }) {
chrome.tabs.create({ url: t })
}
static info ({ tab_id: t }) {
return new Promise(a => {
try {
chrome.tabs.get(t, t => a(t))
} catch (t) {
a(false)
}
})
}
}
class e {
static DEFAULT = {
version: 2,
hcaptcha_auto_solve: true,
hcaptcha_solve_delay: 1e3,
hcaptcha_auto_open: true,
hcaptcha_open_delay: 1e3,
recaptcha_auto_solve: true,
recaptcha_solve_delay: 1e3,
recaptcha_auto_open: true,
recaptcha_open_delay: 1e3,
recaptcha_solve_method: 'image',
debug: false
}
static data = {}
static _save () {
return new Promise(t => chrome.storage.sync.set({ settings: e.data }, t))
}
static load () {
return new Promise(a => {
chrome.storage.sync.get(['settings'], async ({ settings: t }) => {
t
? ((e.data = t),
e.data.version !== e.DEFAULT.version && (await e.reset()))
: await e.reset(),
a()
})
})
}
static async get () {
return e.data
}
static async set ({ data: { id: t, value: a } }) {
;(e.data[t] = a), await e._save()
}
static async reset () {
;(e.data = t(e.DEFAULT)), await e._save()
}
}
class r {
static inject ({ tab_id: t, data: { func: a, args: e } }) {
const c = {
target: { tabId: t, allFrames: true },
world: 'MAIN',
injectImmediately: true,
func: a,
args: e
}
return new Promise(t => chrome.scripting.executeScript(c, t))
}
}
class a {
static async reset ({ tab_id: t }) {
return (
await r.inject({
tab_id: t,
data: {
func: function () {
try {
window.grecaptcha?.reset()
} catch {}
},
args: []
}
}),
true
)
}
static fetch ({ tab_id: t }) {
return new Promise(async a => {
const e = 'recaptcha_response',
c =
(await r.inject({
tab_id: t,
data: {
func: function (t) {
window.grecaptcha &&
window.postMessage({
method: 'set_cache',
data: { name: t, value: window.grecaptcha.getResponse() }
})
},
args: [e]
}
}),
setInterval(async () => {
var t = await s.get({ data: { name: e } })
if (t)
return (
clearInterval(c), await s.remove({ data: { name: e } }), a(t)
)
}, 1e3))
})
}
}
class i {
static STATUS_URL =
'https://api.nopecha.com/status?v=' + chrome.runtime.getManifest().version
static STATUS_CHECK_INTERVAL = 1e4
static status = 'Online'
static checking_status = false
static async run_status_check () {
return (
setInterval(() => {
i.check_status()
}, i.STATUS_CHECK_INTERVAL),
true
)
}
static async check_status () {
if (i.checking_status) return false
i.checking_status = true
let t = 'Offline'
try {
const a = await fetch(i.STATUS_URL)
t = await a.text()
} catch {}
return (
await i.set_status({ data: { status: t } }), (i.checking_status = false), t
)
}
static async set_status ({ data: { status: c } }) {
if (i.status !== c) {
let t,
a = [0, 0, 0, 0],
e = ''
if ('Online' === (i.status = c))
t = {
16: 'icon/16.png',
32: 'icon/32.png',
48: 'icon/48.png',
128: 'icon/128.png'
}
else if ('Offline' === c)
(t = {
16: 'icon/16.png',
32: 'icon/32.png',
48: 'icon/48.png',
128: 'icon/128.png'
}),
(e = 'Off'),
(a = '#a44')
else if ('Slow' === c)
(t = {
16: 'icon/16.png',
32: 'icon/32.png',
48: 'icon/48.png',
128: 'icon/128.png'
}),
(e = 'Slow'),
(a = '#f8d66d')
else {
if ('Update Required' !== c) return false
;(t = {
16: 'icon/16.png',
32: 'icon/32.png',
48: 'icon/48.png',
128: 'icon/128.png'
}),
(e = 'Update'),
(a = '#f8d66d')
}
return (
chrome.action.setIcon({ path: t }),
chrome.action.setBadgeText({ text: e }),
chrome.action.setBadgeBackgroundColor({ color: a }),
true
)
}
}
static async get_status () {
return await i.check_status(), i.status
}
static async check_plan ({ data: { key: t } }) {
if (i.checking_plan) return false
i.checking_plan = true
let a = { plan: 'free', credit: 0 }
try {
'undefined' === t && (t = '')
const e = await fetch(i.STATUS_URL + '&k=' + t)
a = JSON.parse(await e.text())
} catch {}
return (i.checking_plan = false), a
}
static async get_plan ({ data: { key: t } }) {
return await i.check_plan({ data: { key: t } })
}
}
const o = {
set_cache: s.set,
get_cache: s.get,
remove_cache: s.remove,
append_cache: s.append,
empty_cache: s.empty,
inc_cache: s.inc,
dec_cache: s.dec,
zero_cache: s.zero,
fetch: class {
static async fetch ({ data: { url: t, options: a } }) {
try {
const e = await fetch(t, a)
return await e.text()
} catch {
return null
}
}
}.fetch,
reload_tab: n.reload,
close_tab: n.close,
open_tab: n.open,
info_tab: n.info,
get_settings: e.get,
set_settings: e.set,
reset_settings: e.reset,
reset_recaptcha: a.reset,
fetch_recaptcha: a.fetch,
translate: class d {
static base_url = 'https://translate.googleapis.com/translate_a/single'
static async translate ({ data: { from: t, to: a, text: e } }) {
let c = await fetch(
d.base_url + `?client=gtx&sl=${t}&tl=${a}&dt=t&q=` + encodeURI(e)
).then(t => t.json())
return (c = c && c[0] && c[0][0] && c[0].map(t => t[0]).join(''))
}
}.translate,
get_server_plan: i.get_plan
}
;(async () => {
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
{
id: 1,
priority: 1,
action: {
type: 'redirect',
redirect: {
transform: {
queryTransform: {
addOrReplaceParams: [{ key: 'hl', value: 'en-US' }]
}
}
}
},
condition: {
regexFilter:
'^https://[^\\.]*\\.(google|recaptcha)\\.(com|net)/recaptcha',
resourceTypes: ['sub_frame', 'script']
}
}
],
removeRuleIds: [1]
}),
await e.load(),
chrome.runtime.onMessage.addListener((t, a, e) => {
const c = !['get_settings', 'set_settings', 'set_cache'].includes(
t.method
)
return (
c,
o[t.method]({ tab_id: a?.tab?.id, data: t.data }).then(t => {
c
try {
e(t)
} catch (t) {}
}),
true
)
})
})()
})()