This repository has been 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
/
utils.js
189 lines (188 loc) · 4.74 KB
/
utils.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
'use strict'
class Type {
static _string_constructor = 'string'.constructor
static _array_constructor = [].constructor
static _object_constructor = {}.constructor
static of (e) {
return null === e
? 'null'
: void 0 === e
? 'undefined'
: e.constructor === Type._string_constructor
? 'string'
: e.constructor === Type._array_constructor
? 'array'
: e.constructor === Type._object_constructor
? 'object'
: ''
}
}
class Logger {
static debug = true
static log (e = 0) {
const t = new Array(...arguments).map(e =>
['array', 'object'].includes(Type.of(e))
? JSON.stringify(e, null, 4)
: '' + e
)
t.join(' ')
}
}
class Time {
static time () {
return Date.now || (Date.now = () => new Date().getTime()), Date.now()
}
static sleep (t = 1e3) {
return new Promise(e => setTimeout(e, t))
}
static async random_sleep (e, t) {
t = Math.floor(Math.random() * (t - e) + e)
return Time.sleep(t)
}
static pad (e) {
var t = 2 - String(e).length + 1
return 0 < t ? '' + new Array(t).join('0') + e : '' + e
}
static date () {
return new Date()
}
static string (e = null) {
return (
(e = e || Time.date()),
Time.pad(e.getMonth() + 1) +
`/${Time.pad(e.getDate())}/${e.getFullYear()} ${Time.pad(
e.getHours() % 12
)}:${Time.pad(e.getMinutes())}:${Time.pad(e.getSeconds())} ` +
(12 <= e.getHours() ? 'PM' : 'AM')
)
}
}
class BG {
static exec (e, r) {
return new Promise(t => {
try {
chrome.runtime.sendMessage({ method: e, data: r }, t)
} catch (e) {
t()
}
})
}
}
class Net {
static async fetch (e, t) {
return BG.exec('fetch', { url: e, options: t })
}
}
class Image {
static encode (t) {
return new Promise(r => {
if (null === t) return r(null)
const e = new XMLHttpRequest()
;(e.onload = () => {
const t = new FileReader()
;(t.onloadend = () => {
let e = t.result
if (e.startsWith('data:text/html;base64,')) return r(null)
;(e = e.replace('data:image/jpeg;base64,', '')), r(e)
}),
t.readAsDataURL(e.response)
}),
(e.onerror = () => {
r(null)
}),
(e.onreadystatechange = () => {
4 == this.readyState && 200 != this.status && r(null)
}),
e.open('GET', t),
(e.responseType = 'blob'),
e.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: e,
task: t,
image_urls: r,
grid: a,
key: n
}) {
for (
var o = Date.now(), s = await BG.exec('info_tab');
!(Date.now() - o > 1e3 * NopeCHA.MAX_WAIT_POST);
) {
const u = {
type: e,
task: t,
image_urls: r,
v: chrome.runtime.getManifest().version,
key: n,
url: s.url
}
a && (u.grid = a)
var i = await Net.fetch(NopeCHA.INFERENCE_URL, {
method: 'POST',
body: JSON.stringify(u),
headers: { 'Content-Type': 'application/json' }
})
try {
var c = JSON.parse(i)
if ('error' in c) {
if (c.error === NopeCHA.ERRORS.RATE_LIMITED) {
await Time.sleep(2e3)
continue
}
if (c.error === NopeCHA.ERRORS.INVALID_KEY) break
if (c.error === NopeCHA.ERRORS.NO_CREDIT) break
break
}
var l = 'id' in c ? c.id : c.data
return await NopeCHA.get({ job_id: l, key: n })
} catch (e) {
break
}
}
return { job_id: null, clicks: null }
}
static async get ({ key: e, job_id: t }) {
for (var r = Date.now(); !(Date.now() - r > 1e3 * NopeCHA.MAX_WAIT_GET); ) {
await Time.sleep(500)
var a = await Net.fetch(NopeCHA.INFERENCE_URL + `?id=${t}&key=` + e)
try {
var n = JSON.parse(a)
if ('error' in n) {
if (n.error !== NopeCHA.ERRORS.INCOMPLETE_JOB)
return { job_id: t, clicks: null }
continue
}
return { job_id: t, clicks: n.data }
} catch (e) {
break
}
}
return { job_id: t, clicks: null }
}
}
function oep (a, n = 1, e = 100) {
return new Promise(t => {
const r = setInterval(() => {
var e = document.querySelectorAll(a)
if (e.length === n) return clearInterval(r), t(1 === n ? e[0] : e)
}, e)
})
}
export { Type, Logger, Time, BG, Net, Image, NopeCHA, oep }