diff --git a/Makefile b/Makefile index b5266c5..994cbd2 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,10 @@ $(BUILD)/firefox: $(SRC) mkdir -p $@ cp -r $(SRC) $@/ + # Firefox MV3 has no background.service_worker / type:"module": + # bundle api.js + background.js into one classic script and use event-page scripts. + node tools/bundle-background.js $@ + node -e "const fs=require('fs'); const p='$@/manifest.json'; const m=JSON.parse(fs.readFileSync(p)); m.background={scripts:['src/background.js']}; fs.writeFileSync(p, JSON.stringify(m,null,2));" sed -i 's|url("../assets/|url("assets/|g' $@/lib/gnexus-ui-kit.css sed -i "s|url('/assets/|url('assets/|g" $@/lib/gnexus-ui-kit.css diff --git a/README.md b/README.md index b25b851..802694b 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,13 @@ ### Firefox -1. Откройте `about:debugging#/runtime/this-firefox`. -2. Нажмите "Load Temporary Add-on" и выберите `manifest.json`. -3. (Или установите `.zip` из `dist/`). +> Firefox MV3 не поддерживает `background.service_worker`, поэтому грузить нужно +> собранную версию из `build/firefox/` (или `.zip` из `dist/`), а не корневой `manifest.json`. + +1. Соберите: `make firefox`. +2. Откройте `about:debugging#/runtime/this-firefox`. +3. Нажмите "Load Temporary Add-on" и выберите `build/firefox/manifest.json` + (или установите `dist/gnexus-creds-extension-firefox-0.1.0.zip`). ## Настройка diff --git a/tools/bundle-background.js b/tools/bundle-background.js new file mode 100644 index 0000000..996e55e --- /dev/null +++ b/tools/bundle-background.js @@ -0,0 +1,31 @@ +#!/usr/bin/env node +/** + * Bundle src/api.js + src/background.js into a single classic (non-module) + * background.js for Firefox MV3, which does not support background.service_worker + * or type:"module" (event-page scripts run as classic scripts sharing one scope). + * + * Usage: node tools/bundle-background.js + */ +const fs = require("fs"); +const path = require("path"); + +const dir = process.argv[2]; +if (!dir) { + console.error("build dir argument required"); + process.exit(1); +} + +const apiPath = path.join(dir, "src", "api.js"); +const bgPath = path.join(dir, "src", "background.js"); + +// Strip ES module keywords so the result is valid as a classic script. +const api = fs.readFileSync(apiPath, "utf8").replace(/^export /gm, ""); +const bg = fs.readFileSync(bgPath, "utf8").replace( + /^\s*import\s+.*\s+from\s+["']\.\/api\.js["'];\s*\n/m, + "" +); + +// api.js function declarations are hoisted into the shared classic scope, +// so background.js can reference them without an import. +fs.writeFileSync(bgPath, api.trimEnd() + "\n\n" + bg); +fs.rmSync(apiPath); \ No newline at end of file