crxjs / chrome-extension-tools

Bundling Chrome Extensions can be pretty complex. It doesn't have to be.
https://crxjs.dev/vite-plugin
2.82k stars 188 forks source link

Example won't work on firefox #882

Open renyuneyun opened 5 months ago

renyuneyun commented 5 months ago

Build tool

Vite

Where do you see the problem?

Describe the bug

After following the instruction to create a new project and loaded into browser, the extension won't actually load correctly. Clicking on the icon will show the following page:

image

Error messages appear in browser console (see below).

Reproduction

Logs

Content-Security-Policy: The page’s settings blocked a script (script-src-elem) at http://localhost:5173/@vite/env from being executed because it violates the following directive: “script-src 'self' 'wasm-unsafe-eval'” service-worker-loader.js:1:1
Content-Security-Policy: The page’s settings blocked a script (script-src-elem) at http://localhost:5173/@vite/env from being executed because it violates the following directive: “script-src 'self'” service-worker-loader.js:1:1
Content-Security-Policy: The page’s settings blocked a script (script-src-elem) at http://localhost:5173/@crx/client-worker from being executed because it violates the following directive: “script-src 'self' 'wasm-unsafe-eval'” service-worker-loader.js:2:1
Content-Security-Policy: The page’s settings blocked a script (script-src-elem) at http://localhost:5173/@crx/client-worker from being executed because it violates the following directive: “script-src 'self'” service-worker-loader.js:2:1
Uncaught (in promise) TypeError: error loading dynamically imported module: http://localhost:5173/@vite/env
    <anonymous> moz-extension://6c920f88-52e6-4b5a-bc9e-33003968c870/service-worker-loader.js:1
service-worker-loader.js:1:1
Uncaught (in promise) TypeError: error loading dynamically imported module: http://localhost:5173/@crx/client-worker
    <anonymous> moz-extension://6c920f88-52e6-4b5a-bc9e-33003968c870/service-worker-loader.js:2

System Info

System:
    OS: macOS 14.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 5.78 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    npm: 10.2.4 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 124.0.6367.119
    Edge: 124.0.2478.80
    Safari: 17.1
  npmPackages:
    @crxjs/vite-plugin: ^2.0.0-beta.23 => 2.0.0-beta.23 
    vite: ^5.2.0 => 5.2.11

Also

  Firefox: 125.0.3
  Firefox Developer Edition: 126.0b9

Severity

blocking all usage of RPCE

jhholm commented 4 months ago

Firefox doesn't allow remote code execution when using extension manifest version 3. Localhost is technically an external/remote source, as you the extension is loaded from your hard drive. See mv3 migration guide

You can run vite build --watch when developing for Firefox. It's not as nice as just running vite-dev for Chromium browsers with HMR. I'm using web-ext to automatically start Firefox with the extension and autoreload on changes.

dotproto commented 4 months ago

Thanks for sharing the info about vite build --watch, @jhholm. I for one wasn't familiar with that command.

Bug 1864284 appears to be the relevant feature request for Firefox. In comment 3, @Rob--W indicated that he's "leaning towards … allowing localhost and 127.0.0.1 for temporarily loaded add-ons only." Until someone is able to land that change, vite build --watch should at least unblock @crxjs/vite-plugin users.