diff --git a/src/content_scripts/main.js b/src/content_scripts/main.js index 50a30f844..ce9b29633 100644 --- a/src/content_scripts/main.js +++ b/src/content_scripts/main.js @@ -81,7 +81,6 @@ const { nonce } = [...document.scripts].find(script => script.getAttributeNames().includes('nonce')); const script = document.createElement('script'); - script.type = 'module'; script.nonce = nonce; script.src = browser.runtime.getURL('/main_world/index.js'); document.documentElement.append(script); diff --git a/src/main_world/index.js b/src/main_world/index.js index 63f12c1a2..5d7bc1d12 100644 --- a/src/main_world/index.js +++ b/src/main_world/index.js @@ -1,34 +1,38 @@ -const moduleCache = {}; +'use strict'; -document.documentElement.addEventListener('xkitinjectionrequest', async event => { - const { detail, target } = event; - const { id, path, args } = JSON.parse(detail); +{ + const moduleCache = {}; - try { - moduleCache[path] ??= await import(path); - const func = moduleCache[path].default; + document.documentElement.addEventListener('xkitinjectionrequest', async event => { + const { detail, target } = event; + const { id, path, args } = JSON.parse(detail); - if (target.isConnected === false) return; + try { + moduleCache[path] ??= await import(path); + const func = moduleCache[path].default; - const result = await func.apply(target, args); - target.dispatchEvent( - new CustomEvent('xkitinjectionresponse', { detail: JSON.stringify({ id, result }) }) - ); - } catch (exception) { - target.dispatchEvent( - new CustomEvent('xkitinjectionresponse', { - detail: JSON.stringify({ - id, - exception: { - message: exception.message, - name: exception.name, - stack: exception.stack, - ...exception - } + if (target.isConnected === false) return; + + const result = await func.apply(target, args); + target.dispatchEvent( + new CustomEvent('xkitinjectionresponse', { detail: JSON.stringify({ id, result }) }) + ); + } catch (exception) { + target.dispatchEvent( + new CustomEvent('xkitinjectionresponse', { + detail: JSON.stringify({ + id, + exception: { + message: exception.message, + name: exception.name, + stack: exception.stack, + ...exception + } + }) }) - }) - ); - } -}); + ); + } + }); -document.documentElement.dispatchEvent(new CustomEvent('xkitinjectionready')); + document.documentElement.dispatchEvent(new CustomEvent('xkitinjectionready')); +}