|
5 | 5 | if (typeof window.GMBridge !== "undefined") { |
6 | 6 | return; |
7 | 7 | } |
8 | | - |
9 | 8 | // Lets start with trusted types |
10 | 9 | // Is this allowed on the web store? |
11 | 10 | let ctTrustedTypesPolicy = null; |
|
266 | 265 | window.addEventListener('error', errorHandler); |
267 | 266 | window.addEventListener('unhandledrejection', rejectionHandler); |
268 | 267 |
|
269 | | - // Ensure unsafeWindow is available globally before loading external scripts |
270 | | - // (jQuery and other libraries may try to attach to it) |
271 | | - if (!window.unsafeWindow) { |
272 | | - window.unsafeWindow = window; |
273 | | - } |
274 | | - |
275 | 268 | try { |
276 | 269 | await loader.loadScripts(requireUrls); |
277 | 270 |
|
278 | | - const wrappedCode = `(async function() {\n'use strict';\nconst unsafeWindow = window.unsafeWindow || window;\n${userCode}\n})();`; |
279 | | - console.log('CodeTweak: Wrapped code preview (first 500 chars):', wrappedCode.substring(0, 500)); |
280 | | - const blob = new Blob([wrappedCode], { type: "text/javascript" }); |
281 | | - const blobUrl = URL.createObjectURL(blob); |
282 | | - |
283 | | - await new Promise((resolve, reject) => { |
284 | | - const scriptEl = document.createElement("script"); |
| 271 | + const wrappedCode = `(function() {\n'use strict';\nconst unsafeWindow = this;\n${userCode}\n}).call(window);`; |
| 272 | + |
| 273 | + const scriptEl = document.createElement("script"); |
| 274 | + scriptEl.setAttribute('data-script-id', scriptId); |
285 | 275 |
|
286 | | - const policy = getTrustedTypesPolicy(); |
287 | | - let trustedSrc = blobUrl; |
288 | | - if (policy) { |
289 | | - try { |
290 | | - trustedSrc = policy.createScriptURL(blobUrl); |
291 | | - } catch (e) { |
292 | | - console.error("Failed to create trusted script URL:", e); |
293 | | - console.warn("Falling back to raw URL."); |
294 | | - } |
| 276 | + const policy = getTrustedTypesPolicy(); |
| 277 | + let trustedCode = wrappedCode; |
| 278 | + if (policy) { |
| 279 | + try { |
| 280 | + trustedCode = policy.createScript(wrappedCode); |
| 281 | + } catch (e) { |
| 282 | + console.error("Failed to create trusted script:", e); |
| 283 | + console.warn("Falling back to raw code."); |
295 | 284 | } |
| 285 | + } |
| 286 | + |
| 287 | + scriptEl.textContent = trustedCode; |
| 288 | + (document.head || document.documentElement || document.body).appendChild(scriptEl); |
296 | 289 |
|
297 | | - scriptEl.src = trustedSrc; |
298 | | - scriptEl.async = false; // maintain execution order |
299 | | - scriptEl.setAttribute('data-script-id', scriptId); |
300 | | - |
301 | | - scriptEl.onload = () => { |
302 | | - URL.revokeObjectURL(blobUrl); |
303 | | - resolve(); |
304 | | - }; |
305 | | - |
306 | | - scriptEl.onerror = (event) => { |
307 | | - URL.revokeObjectURL(blobUrl); |
308 | | - const error = new Error( |
309 | | - `Failed to execute user script ${scriptId}: ${event?.message || "unknown error"}` |
310 | | - ); |
311 | | - |
312 | | - console.error(`CodeTweak: Failed to execute user script ${scriptId}:`, event); |
313 | | - // Report load error |
314 | | - try { |
315 | | - window.postMessage({ |
316 | | - type: 'SCRIPT_ERROR', |
317 | | - scriptId: scriptId, |
318 | | - error: { |
319 | | - message: error.message, |
320 | | - stack: error.stack || '', |
321 | | - timestamp: new Date().toISOString(), |
322 | | - type: 'error' |
323 | | - } |
324 | | - }, '*'); |
325 | | - } catch (e) { |
326 | | - console.error('Failed to report script load error:', e); |
327 | | - } |
328 | | - |
329 | | - reject(error); |
330 | | - }; |
331 | | - |
332 | | - (document.head || document.documentElement || document.body).appendChild(scriptEl); |
333 | | - }); |
334 | 290 | } catch (err) { |
335 | 291 | console.error(`CodeTweak: Error executing user script ${scriptId}:`, err); |
336 | 292 |
|
|
0 commit comments