extension.js icon indicating copy to clipboard operation
extension.js copied to clipboard

Setting the execution environment for content script to MAIN throws an error

Open r3lays opened this issue 1 year ago • 3 comments

Same issue as #66 except on latest version (2.0.0.beta.9)

Image

Cheers! Amazing tool so far.

r3lays avatar Jan 16 '25 10:01 r3lays

@cezaraugusto I'm experiencing the same:

"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["scripts/bridge.ts"],
      "run_at": "document_start",
      "world": "MAIN"
    }
]

When no key is used in manifest.json you rightfully beautifully handle the case and print this message:

For MAIN world content_scripts, the extension ID must be specified.
Ensure your extension have a fixed ID and that the publicPath of your extension.config.js is defined as your extension URL.

Now I'm a little lost at the publicPath stage. The types at import('extension').FileConfig have nothing in regards so I thought its probably something to set in webpack land. Then I saw https://github.com/awesome-webextension/webpack-target-webextension?tab=readme-ov-file#main-world-content-script so I tried:

/** @type {import('extension').FileConfig} */
module.exports = {
  // ...
  config: (config) => {
    config.output.publicPath = 'chrome-extension://<myextensionidfromgoogle>/'
    return config
  }
  // ...
}

but

Uncaught Error: No chrome or browser runtime found
    at get runtime (content-0.js:6006:20)
    at content-0.js:6560:66
    at content-0.js:6562:13
    at content-0.js:7248:12

is still happening.

Note that chrome-extension://<myextensionidfromgoogle>/scripts/bridge.js is reachable when typed into chrome, so I'm quite sure that the id itself is not the problem.

@cezaraugusto can you point me somewhere I should look for debugging this? somewhere around resolve/steps/get-file-list/get-manifest-entries/content_scripts? or maybe somewhere around reload/steps/setup-reload-strategy/target-web-extension-plugin?

It would also be fine for me to disable HMR for this content script if thats possible as its super tiny and just exposes a tiny helper in the window object. Is there a way to bypass all compilation and just pass through a vanilla js content script untouched?

I'm also using 🧩 Extension.js 2.0.0-beta.9 aka webpack-target-webextension: ^2.1.3

tom2strobl avatar Mar 31 '25 15:03 tom2strobl

I encountered the same error, any update on this issue? I use [email protected]

wfk007 avatar Apr 22 '25 12:04 wfk007

same here on lastest version

orkeilius avatar Oct 16 '25 17:10 orkeilius