Skip to content

Commit 14de746

Browse files
committed
ocr autodetect WIP
1 parent f1883e1 commit 14de746

11 files changed

Lines changed: 520 additions & 62 deletions

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## NopeCHA Chrome Extension & Firefox Add-on
22

3+
[Official Website](https://www.nopecha.com)
4+
5+
[Chrome Extension](https://www.nopecha.com/chrome)
6+
7+
[Firefox Add-on](https://www.nopecha.com/firefox)
8+
9+
[API Reference](https://www.nopecha.com/api)
310

411
## Development
512
### Prerequisites
@@ -21,10 +28,10 @@ For that, you need to install python's `watchdog` in your machine:
2128

2229
`python -m pip install watchdog`
2330

24-
Then you can run `build.py` command with the `-w` option so your changes immediately apply:
31+
Then you can run `build.py` command with the `-w` option so your changes immediately apply:
2532

26-
`python build.py -w`
27-
or
33+
`python build.py -w`
34+
or
2835
`./build.py -w`
2936

3037

autodetect.js

Lines changed: 332 additions & 0 deletions
Large diffs are not rendered by default.

background.js

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,34 @@ class Tab {
6464
}
6565
});
6666
}
67+
68+
static active() {
69+
// TODO: Implement MV2
70+
// https://developer.chrome.com/docs/extensions/reference/tabs/#get-the-current-tab
71+
72+
// `tab` will either be a `tabs.Tab` instance or `undefined`
73+
return new Promise(async resolve => {
74+
// MV3
75+
const [tab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
76+
return resolve(tab);
77+
78+
// MV2
79+
// chrome.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) => {
80+
// if (chrome.runtime.lastError) {
81+
// console.error(chrome.runtime.lastError);
82+
// }
83+
// resolve(tab);
84+
// });
85+
});
86+
}
6787
}
6888

6989

7090
class Settings {
7191
static DEFAULT = {
72-
version: 4,
92+
version: 5,
93+
94+
active_tab: '#hcaptcha_tab',
7395

7496
key: '',
7597

@@ -150,26 +172,47 @@ class Settings {
150172

151173

152174
class Injector {
153-
static inject({tab_id, data: {func, args}}) {
154-
// console.log('injecting', tab_id, func);
175+
static async _inject(options) {
176+
// Inject into active tab if target tabId is undefined
177+
if (!options.target.tabId) {
178+
const tab = await Tab.active();
179+
options.target.tabId = tab.id;
180+
}
181+
182+
// Convert callback to promise
183+
console.log('inject', options);
184+
const prom = new Promise(resolve => bapi.browser.scripting.executeScript(options, resolve));
185+
return await prom;
186+
}
187+
188+
static async inject_func({tab_id, data: {func, args}}) {
155189
const options = {
156190
target: {tabId: tab_id, allFrames: true},
157191
world: 'MAIN',
158192
injectImmediately: true,
159193
func: func,
160194
args: args,
161195
};
162-
return new Promise(resolve => bapi.browser.scripting.executeScript(options, resolve));
196+
// return new Promise(resolve => bapi.browser.scripting.executeScript(options, resolve));
197+
return await Injector._inject(options);
198+
}
199+
200+
static async inject_files({tab_id, data: {files}}) {
201+
const options = {
202+
target: {tabId: tab_id, allFrames: true},
203+
world: 'MAIN',
204+
injectImmediately: true,
205+
files: files,
206+
};
207+
return await Injector._inject(options);
163208
}
164209
}
210+
// Injector.inject_files({tab_id: null, data: {files: ['autodetect.js']}});
165211

166212

167213
class Recaptcha {
168214
static async reset({tab_id}) {
169-
function func() {
170-
try {window.grecaptcha?.reset();} catch {}
171-
}
172-
await Injector.inject({tab_id, data: {func, args: []}});
215+
await Injector.inject_func({tab_id, data: {func: () => {try {window.grecaptcha?.reset();} catch {}}, args: []}});
173216
return true;
174217
}
175218
}
@@ -201,6 +244,36 @@ class Server {
201244
}
202245

203246

247+
class Image {
248+
static b64({data: {url}}) {
249+
return new Promise(resolve => {
250+
fetch(url)
251+
.then(response => response.blob())
252+
.then(blob => {
253+
const reader = new FileReader();
254+
reader.onload = () => resolve(reader.result);
255+
reader.readAsDataURL(blob);
256+
});
257+
});
258+
}
259+
}
260+
261+
262+
class Relay {
263+
static async send({tab_id, data}) {
264+
// Send to active tab if tab_id is undefined
265+
if (!tab_id) {
266+
const tab = await Tab.active();
267+
tab_id = tab.id;
268+
}
269+
bapi.browser.tabs.sendMessage(tab_id, data);
270+
}
271+
}
272+
// setInterval(() => {
273+
// Relay.send({tab_id: undefined, data: {testing: 'hello world!'}});
274+
// }, 1000);
275+
276+
204277
const FN = {
205278
set_cache: Cache.set,
206279
get_cache: Cache.get,
@@ -217,14 +290,21 @@ const FN = {
217290
close_tab: Tab.close,
218291
open_tab: Tab.open,
219292
info_tab: Tab.info,
293+
active_tab: Tab.active,
220294

221295
get_settings: Settings.get,
222296
set_settings: Settings.set,
223297
reset_settings: Settings.reset,
224298

299+
inject_files: Injector.inject_files,
300+
225301
reset_recaptcha: Recaptcha.reset,
226302

227303
get_server_plan: Server.get_plan,
304+
305+
b64_image: Image.b64,
306+
307+
relay: Relay.send,
228308
};
229309

230310

funcaptcha_demo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
}
2323
let nframes = 21;
2424

25+
2526
reset();
26-
open_frame_token('ea signup', 0, nframes);
27+
// open_frame_token('ea signup', 0, nframes);
28+
2729

2830
function reset() {
2931
document.body.innerHTML = '';

funcaptcha_scrape.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110

111111
const $timeout = document.querySelector('#timeout_widget');
112112
if ($timeout?.style?.display === 'block') {
113-
console.log('$timeout', $timeout);
113+
// console.log('$timeout', $timeout);
114114
window.parent.postMessage({nopecha: true, action: 'reload'}, '*');
115115
window.location.reload(true);
116116
}

hcaptcha.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
(async () => {
22
function is_widget_frame() {
3+
if (document.body.getBoundingClientRect()?.width === 0 || document.body.getBoundingClientRect()?.height === 0) {
4+
return false;
5+
}
36
return document.querySelector('div.check') !== null;
47
}
58

manifest.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787

8888
{
8989
"matches": ["<all_urls>"],
90-
"js": ["ocr.js"],
90+
"js": ["ocr.js", "autodetect.js"],
9191
"all_frames": true,
9292
"run_at": "document_end"
9393
},

popup.css

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ html, body {
5555
margin-top: 8px;
5656
width: 100%;
5757
text-align: center;
58-
color: #999;
5958
height: 20px;
6059
}
60+
#footer > a {
61+
color: #999;
62+
text-decoration: none;
63+
}
6164

6265
#manage {
66+
text-decoration: none;
6367
width: 100%;
6468
text-align: center;
6569
background-color: #1a73e8;
@@ -122,7 +126,16 @@ html, body {
122126
background-color: #fff;
123127
}
124128
.settings_group > input[type="text"].text {
125-
width: calc(120px * var(--input_scale_x));
129+
/* width: calc(120px * var(--input_scale_x)); */
130+
width: calc(100px * var(--input_scale_x));
131+
}
132+
.settings_group > input[type="button"].button {
133+
width: calc(80px * var(--input_scale_x));
134+
padding: 0;
135+
background-color: #fff;
136+
border-radius: 0;
137+
border: 1px solid #eee;
138+
outline: none;
126139
}
127140

128141
.switch {
@@ -281,10 +294,6 @@ input:checked + .slider:before {
281294
border-radius: 0 0 4px 4px;
282295
}
283296

284-
.footer_group {
285-
width: 296px;
286-
}
287-
288297
.warning_box {
289298
background-color: #FEF9C3;
290299
border: 1px solid #FCD62E;
@@ -295,4 +304,4 @@ input:checked + .slider:before {
295304
.warning_box * {
296305
font-size: 10px;
297306
text-align: center;
298-
}
307+
}

popup.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<div id="main">
88
<div class="content">
99
<div class="settings_group">
10-
<div id="manage" class="clickable">Manage Subscription</div>
10+
<!-- <div id="manage" class="clickable">Manage Subscription</div> -->
11+
<a id="manage" class="clickable" href="https://nopecha.com/manage" target="_blank">Manage Subscription</a>
1112
</div>
1213

1314
<div id="ipbanned_warning" class="warning_box hidden">
@@ -144,13 +145,17 @@
144145
</div>
145146

146147
<div class="settings_group">
147-
<div class="label">Image Selector</div>
148-
<input id="ocr_image_selector" type="text" class="text" placeholder='CSS Selector'>
148+
<div class="label">Image</div>
149+
<input type="button" class="button autodetect" data-key="ocr_image_selector" value="Autodetect">
150+
<div style="width: 8px;"></div>
151+
<input id="ocr_image_selector" type="text" class="text" placeholder="CSS Selector">
149152
</div>
150153

151154
<div class="settings_group">
152-
<div class="label">Input Selector</div>
153-
<input id="ocr_input_selector" type="text" class="text" placeholder='CSS Selector'>
155+
<div class="label">Input</div>
156+
<input type="button" class="button autodetect" data-key="ocr_input_selector" value="Autodetect">
157+
<div style="width: 8px;"></div>
158+
<input id="ocr_input_selector" type="text" class="text" placeholder="CSS Selector">
154159
</div>
155160

156161
<div class="settings_group">
@@ -168,8 +173,8 @@
168173
</label>
169174
</div> -->
170175

171-
<div class="settings_group footer_group">
172-
<div id="footer" class="clickable">Join us on Discord for free credits!</div>
176+
<div id="footer" class="clickable">
177+
<a href="https://nopecha.com/discord" target="_blank">Join us on Discord for free credits!</a>
173178
</div>
174179
</div>
175180

0 commit comments

Comments
 (0)