@@ -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
7090class 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
152174class 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
167213class 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+
204277const 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
0 commit comments