@@ -5,7 +5,6 @@ import "../content-scripts/ufs_global.js"; // https://stackoverflow.com/a/628060
55console . log ( UfsGlobal ) ;
66
77const {
8- runScriptInCurrentTab,
98 convertBlobToBase64,
109 getAllActiveScriptIds,
1110 trackEvent,
@@ -25,6 +24,9 @@ const GLOBAL = {
2524 trackEvent,
2625 fetch : customFetch ,
2726 getCached,
27+ runScriptsTab,
28+ runScriptsBackground,
29+ checkWillRun,
2830} ;
2931
3032function getCached ( ) {
@@ -90,15 +92,30 @@ function getDetailIds(details) {
9092 } ;
9193}
9294
93- function runScripts ( eventChain , details , data ) {
95+ function runScriptsBackground (
96+ eventChain ,
97+ details ,
98+ data ,
99+ runAllScripts = false
100+ ) {
94101 let allResponse ;
95- for ( let scriptId of CACHED . activeScriptIds ) {
102+ let scriptIds = runAllScripts
103+ ? Object . keys ( allScripts )
104+ : CACHED . activeScriptIds ;
105+
106+ for ( let scriptId of scriptIds ) {
96107 const fn = checkWillRun ( scriptId , "backgroundScript" , eventChain , details ) ;
97108 if ( fn ) {
98- let res = fn ( data ?? details ) ;
99- if ( res ) {
100- if ( ! allResponse ) allResponse = { } ;
101- allResponse [ scriptId ] = res ;
109+ try {
110+ // inject background context (GLOBAL) to func
111+ let res = fn ( data ?? details , GLOBAL ) ;
112+ console . log ( "runScriptsBackground" , scriptId , eventChain , res ) ;
113+ if ( res ) {
114+ if ( ! allResponse ) allResponse = { } ;
115+ allResponse [ scriptId ] = res ;
116+ }
117+ } catch ( e ) {
118+ console . log ( "runScriptsBackground ERROR" , scriptId , eventChain , e ) ;
102119 }
103120 }
104121 }
@@ -212,7 +229,7 @@ function listenWebRequest() {
212229 if ( details . initiator ?. startsWith ( "chrome-extension://" ) ) return ;
213230
214231 // console.log("details ne", rqEvent, details);
215- let allData = runScripts ( eventChain , details ) ;
232+ let allData = runScriptsBackground ( eventChain , details ) ;
216233
217234 let modifiedDetails = {
218235 ...details ,
@@ -275,7 +292,7 @@ function listenNavigation() {
275292 }
276293 runScriptsTab ( eventChain , MAIN , details ) ;
277294 runScriptsTab ( eventChain , ISOLATED , details ) ;
278- runScripts ( eventChain , details ) ;
295+ runScriptsBackground ( eventChain , details ) ;
279296 } catch ( e ) {
280297 console . log ( "ERROR:" , e ) ;
281298 }
@@ -297,7 +314,7 @@ function listenTabs() {
297314 // "onZoomChange",
298315 ] . forEach ( ( event ) => {
299316 chrome . tabs [ event ] . addListener ( ( details ) => {
300- runScripts ( "tabs." + event , details ) ;
317+ runScriptsBackground ( "tabs." + event , details ) ;
301318 } ) ;
302319 } ) ;
303320}
@@ -318,7 +335,7 @@ function listenMessage() {
318335 sended = true ;
319336 sendResponse ( data ) ;
320337 } ;
321- runScripts (
338+ runScriptsBackground (
322339 "runtime.onMessage" ,
323340 null ,
324341 // {
@@ -340,7 +357,7 @@ function main() {
340357 // listen change active scripts
341358 cacheActiveScriptIds ( ) ;
342359 chrome . storage . onChanged . addListener ( ( changes , areaName ) => {
343- runScripts ( "storage.onChanged" , null , { changes, areaName } ) ;
360+ runScriptsBackground ( "storage.onChanged" , null , { changes, areaName } ) ;
344361
345362 // areaName = "local" / "sync" / "managed" / "session" ...
346363 if ( changes ?. [ listActiveScriptsKey ] ) cacheActiveScriptIds ( ) ;
@@ -352,53 +369,22 @@ function main() {
352369 listenMessage ( ) ;
353370
354371 chrome . contextMenus . onClicked . addListener ( async ( info ) => {
355- console . log ( info ) ;
356- if ( info . menuItemId == "ufs-magnify-image" ) {
357- trackEvent ( "magnify-image-CONTEXT-MENU" ) ;
358- /*
359- {
360- "editable": false,
361- "frameId": 2491,
362- "frameUrl": "https://www.deviantart.com/_nsfgfb/?realEstateId=166926a9-15ab-458d-b424-4385d5c9acde&theme=dark&biClientId=fdb7b474-671d-686c-7ebc-7027eecd49f0&biClientIdSigned=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJiaUNsaWVudElkIjoiZmRiN2I0NzQtNjcxZC02ODZjLTdlYmMtNzAyN2VlY2Q0OWYwIiwidHMiOjE3MTM0NjgyNTAsInVzZXJVdWlkIjoiZmRiN2I0NzQtNjcxZC02ODZjLTdlYmMtNzAyN2VlY2Q0OWYwIn0.z98X9tXSYMaUubtwGGG08NsikaoZ7iODsn_aWaeiGD0&newApi=2&platform=desktop",
363- "linkUrl": "https://www.deviantart.com/join?referer=https%3A%2F%2Fwww.deviantart.com%2Fdreamup%3Fda_dealer_footer=1",
364- "mediaType": "image",
365- "menuItemId": "ufs-magnify-image",
366- "pageUrl": "https://www.deviantart.com/kat-zaphire/art/Deep-in-the-forest-989494503",
367- "srcUrl": "https://wixmp-70a14ff54af6225c7974eec7.wixmp.com/offers-assets/94f22a36-bb47-4836-8bce-fea45f844aa4.gif"
368- } */
369- let tab = await utils . getCurrentTab ( ) ;
370- utils . runScriptInTabWithEventChain ( {
371- target : {
372- tabId : tab . id ,
373- frameIds : [ 0 ] ,
374- } ,
375- scriptIds : [ "magnify_image" ] ,
376- eventChain : "contentScript._createPreview" ,
377- details : info ,
378- world : "ISOLATED" ,
379- } ) ;
380- }
372+ runScriptsBackground ( "contextMenus.onClicked" , null , info , true ) ;
381373 } ) ;
382374
383375 chrome . runtime . onStartup . addListener ( async function ( ) {
384- runScripts ( "runtime.onStartup" ) ;
376+ runScriptsBackground ( "runtime.onStartup" , null , null , true ) ;
385377 } ) ;
386378
387379 chrome . runtime . onInstalled . addListener ( async function ( reason ) {
388380 if ( utils . hasUserId ( ) ) {
389- await GLOBAL . trackEvent ( "ufs-RE-INSTALLED" ) ;
381+ await trackEvent ( "ufs-RE-INSTALLED" ) ;
390382 }
391383 // create new unique id and save it
392384 await setUserId ( ) ;
393- GLOBAL . trackEvent ( "ufs-INSTALLED" ) ;
394-
395- chrome . contextMenus . create ( {
396- title : "Magnify this image" ,
397- contexts : [ "image" ] ,
398- id : "ufs-magnify-image" ,
399- } ) ;
385+ trackEvent ( "ufs-INSTALLED" ) ;
400386
401- runScripts ( "runtime.onInstalled" , null , reason ) ;
387+ runScriptsBackground ( "runtime.onInstalled" , null , reason , true ) ;
402388 } ) ;
403389
404390 chrome . action . setBadgeBackgroundColor ( { color : "#666" } ) ;
0 commit comments