@@ -20,7 +20,25 @@ function createMainWorldExecutor(
2020 requiredUrls ,
2121 gmInfo
2222) {
23- const MAX_RETRIES = 10 ;
23+ // Always expose unsafeWindow in MAIN world FIRST (it's just the window object)
24+ if ( ! window . unsafeWindow ) {
25+ try {
26+ Object . defineProperty ( window , "unsafeWindow" , {
27+ value : window ,
28+ writable : false ,
29+ configurable : false ,
30+ } ) ;
31+ console . log ( 'CodeTweak: unsafeWindow exposed via defineProperty' ) ;
32+ } catch ( e ) {
33+ window . unsafeWindow = window ;
34+ console . log ( 'CodeTweak: unsafeWindow exposed via direct assignment' ) ;
35+ }
36+ } else {
37+ console . log ( 'CodeTweak: unsafeWindow already exists' ) ;
38+ }
39+ console . log ( 'CodeTweak: unsafeWindow check:' , typeof window . unsafeWindow , window . unsafeWindow === window ) ;
40+
41+ const MAX_RETRIES = 30 ;
2442 const RETRY_INTERVAL = 100 ;
2543
2644 function waitForGMBridge ( worldType , callback ) {
@@ -43,7 +61,14 @@ function createMainWorldExecutor(
4361 setTimeout ( check , RETRY_INTERVAL ) ;
4462 } else {
4563 console . error (
46- `CodeTweak: Timed out waiting for core script to load for script '${ scriptId } '${ worldType ? ` in ${ worldType } world` : '' } .`
64+ `CodeTweak: Timed out waiting for core script to load for script '${ scriptId } '${ worldType ? ` in ${ worldType } world` : '' } .` ,
65+ 'Missing objects:' , {
66+ GMBridge : typeof window . GMBridge ,
67+ ResourceManager : typeof window . GMBridge ?. ResourceManager ,
68+ GMAPIRegistry : typeof window . GMBridge ?. GMAPIRegistry ,
69+ ExternalScriptLoader : typeof window . GMBridge ?. ExternalScriptLoader ,
70+ executeUserScriptWithDependencies : typeof window . GMBridge ?. executeUserScriptWithDependencies
71+ }
4772 ) ;
4873 }
4974 }
@@ -103,8 +128,6 @@ function createMainWorldExecutor(
103128 waitForGMBridge ( 'MAIN' , ( ) => {
104129 if ( preventReExecution ( ) ) return ;
105130
106-
107-
108131 const bridge = new window . GMBridge ( scriptId , extensionId , 'MAIN' ) ;
109132 const resourceManager = window . GMBridge . ResourceManager . fromScript ( script ) ;
110133 const apiRegistry = new window . GMBridge . GMAPIRegistry ( bridge , resourceManager ) ;
@@ -133,7 +156,7 @@ function createIsolatedWorldExecutor(
133156 requiredUrls ,
134157 gmInfo
135158) {
136- const MAX_RETRIES = 10 ;
159+ const MAX_RETRIES = 30 ;
137160 const RETRY_INTERVAL = 100 ;
138161
139162 function waitForGMBridge ( worldType , callback ) {
@@ -156,7 +179,14 @@ function createIsolatedWorldExecutor(
156179 setTimeout ( check , RETRY_INTERVAL ) ;
157180 } else {
158181 console . error (
159- `CodeTweak: Timed out waiting for core script to load for script '${ scriptId } '${ worldType ? ` in ${ worldType } world` : '' } .`
182+ `CodeTweak: Timed out waiting for core script to load for script '${ scriptId } '${ worldType ? ` in ${ worldType } world` : '' } .` ,
183+ 'Missing objects:' , {
184+ GMBridge : typeof window . GMBridge ,
185+ ResourceManager : typeof window . GMBridge ?. ResourceManager ,
186+ GMAPIRegistry : typeof window . GMBridge ?. GMAPIRegistry ,
187+ ExternalScriptLoader : typeof window . GMBridge ?. ExternalScriptLoader ,
188+ executeUserScriptWithDependencies : typeof window . GMBridge ?. executeUserScriptWithDependencies
189+ }
160190 ) ;
161191 }
162192 }
@@ -277,7 +307,7 @@ class ScriptInjector {
277307 await chrome . scripting . executeScript ( {
278308 target : { tabId } ,
279309 world,
280- files : [ "GM/gm_core.js" ] ,
310+ files : [ "GM/gm_core.js" , "GM/gm_api_registry.js" ] ,
281311 } ) ;
282312 tabCoreScripts . add ( world ) ;
283313 }
0 commit comments