From 60bdc0ae156c07cec499e25ea5b4be4dc785f9ce Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 30 Dec 2022 03:48:59 +0000 Subject: [PATCH] more attempts for v3 and v2 simultaneous support so far v3 works in firefox but doesn't work with chrome, for some reason it refuses to import the webpack chunk --- extension/build | 3 ++- extension/src/js/background.js | 11 ++++++++++- extension/webpack.config.js | 32 ++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/extension/build b/extension/build index 715cd99..f7fd1e4 100755 --- a/extension/build +++ b/extension/build @@ -47,9 +47,10 @@ def main(): ], **kwargs) env = { - 'TARGET': target, + 'TARGET' : target, 'RELEASE': 'YES' if args.release else 'NO', 'PUBLISH': 'YES' if args.publish else 'NO', + 'EXT_ID' : IDS[target], **os.environ, } diff --git a/extension/src/js/background.js b/extension/src/js/background.js index 26f16bc..2f5c157 100644 --- a/extension/src/js/background.js +++ b/extension/src/js/background.js @@ -6,7 +6,8 @@ // I assume this is only processed with webpack // import * as browser from "./browser-polyfill" -// this works with v3 if you need to +// this works with v3 but NOT if type: module +// also unclear how to make it work with webpack?? // const manifestVersion = chrome.runtime.getManifest().manifest_version // if (manifestVersion == 3) { // // for v3 it's provided via manifest @@ -18,6 +19,14 @@ // } // } + +// this works for v2 both in chrome and firefox +// for v3 +// - in chrome it works if type: module, but somehow NOT if we define chunks?? +// without type: module this isn't working +// - in firefox it just works :shrug: +// but firefox foesn't allow type: module?? +// could it be bug in chrome?? import * as browser from "webextension-polyfill" diff --git a/extension/webpack.config.js b/extension/webpack.config.js index e2ac05a..8bd3902 100644 --- a/extension/webpack.config.js +++ b/extension/webpack.config.js @@ -14,10 +14,11 @@ const T = { const v3 = false const env = { - TARGET : process.env.TARGET, // TODO assert not null? - RELEASE: process.env.RELEASE, - PUBLISH: process.env.PUBLISH, -}; + TARGET : process.env.TARGET, + RELEASE: process.env.RELEASE, + PUBLISH: process.env.PUBLISH, +} +const ext_id = process.env.EXT_ID const pkg = require('./package.json'); const baseManifest = require('./src/manifest.json'); @@ -90,12 +91,11 @@ const permissions = [ const optional_permissions = [] - -if (target === T.FIREFOX) { - // chrome v2 doesn't support scripting api - // code has a fallback just for that - // need to get selected text - permissions.push("scripting") +if (target === T.FIREFOX || v3) { + // chrome v2 doesn't support scripting api + // code has a fallback just for that + // (needed to get selected text) + permissions.push("scripting") } @@ -118,7 +118,9 @@ const manifestExtra = { manifest_version: v3 ? 3 : 2, background: (v3 ? { service_worker: 'background.js', - type: 'module', + // wtf -- firefox doesn't support module?? + // it just fails to load manifest in this case atm + ...(target === T.FIREFOX? {} : {type: 'module'}), } : { scripts: [ 'webextension-polyfill.js', @@ -135,7 +137,13 @@ manifestExtra[action_name] = action if (v3) { manifestExtra['host_permissions'] = host_permissions manifestExtra['optional_host_permissions'] = optional_host_permissions - manifestExtra.permissions.push("scripting") + if (target === T.FIREFOX) { + manifestExtra['browser_specific_settings'] = { + "gecko": { + "id": ext_id, + }, + } + } } else { manifestExtra.permissions.push(...host_permissions) manifestExtra.optional_permissions.push(...optional_host_permissions)