Fix Firefox install: use background.scripts instead of service_worker
CI / Build & Test (push) Successful in 3m22s
CI / Build & Test (push) Successful in 3m22s
Firefox MV3 does not support background.service_worker. Split background into background-boot.js (globals) and background-main.js (startup logic); Chrome keeps using importScripts() in background.js, Firefox manifest uses background.scripts with the same file list so the extension loads in both.
This commit is contained in:
+5
-45
@@ -1,54 +1,14 @@
|
||||
/**
|
||||
* Background service worker entry point.
|
||||
* Loads modules in dependency order via importScripts().
|
||||
* Background service worker entry point (Chrome).
|
||||
* Loads boot globals, then modules, then main logic via importScripts().
|
||||
*/
|
||||
|
||||
const SW_VERSION = 3; // increment to force service worker reload detection
|
||||
|
||||
/** Set to true for super detailed debug logging. */
|
||||
const DEBUG_VERBOSE = false;
|
||||
|
||||
const browser = (typeof chrome !== 'undefined' && chrome.runtime?.connectNative)
|
||||
? chrome
|
||||
: (globalThis.browser ?? chrome);
|
||||
|
||||
importScripts(
|
||||
'background/background-boot.js',
|
||||
'background/logs.js',
|
||||
'background/state.js',
|
||||
'background/proxy.js',
|
||||
'background/native-messaging.js',
|
||||
'background/tab-lifecycle.js',
|
||||
'background/message-router.js'
|
||||
'background/message-router.js',
|
||||
'background/background-main.js'
|
||||
);
|
||||
|
||||
log('background: SW started v' + SW_VERSION);
|
||||
|
||||
// Apply PAC immediately on startup using default port; updated once native host connects
|
||||
applyPAC();
|
||||
connect();
|
||||
|
||||
// Redirect *.host.test (and similar typos) to extension page so we can show "use .hole.sail"
|
||||
const WRONG_DOMAIN_RULE_ID = 1;
|
||||
if (browser.declarativeNetRequest && browser.runtime.getURL) {
|
||||
const redirectUrl = browser.runtime.getURL('wrong-domain.html') + '?host=\\1';
|
||||
browser.declarativeNetRequest.updateDynamicRules({
|
||||
removeRuleIds: [WRONG_DOMAIN_RULE_ID],
|
||||
addRules: [{
|
||||
id: WRONG_DOMAIN_RULE_ID,
|
||||
priority: 1,
|
||||
condition: {
|
||||
regexFilter: '^https?://([^/:]+)\\.host\\.test($|/)',
|
||||
resourceTypes: ['main_frame']
|
||||
},
|
||||
action: {
|
||||
type: 'redirect',
|
||||
redirect: { regexSubstitution: redirectUrl }
|
||||
}
|
||||
}]
|
||||
}).catch((err) => log('declarativeNetRequest rule failed:', err.message));
|
||||
}
|
||||
|
||||
// Open dashboard when extension icon is clicked
|
||||
browser.action?.onClicked?.addListener((tab) => {
|
||||
browser.tabs.create({ url: 'dashboard/dashboard.html' });
|
||||
});
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Boot globals for background (shared by service worker and Firefox scripts context).
|
||||
* Loaded first so DEBUG_VERBOSE and browser are available to logs.js and others.
|
||||
*/
|
||||
const SW_VERSION = 3; // increment to force service worker reload detection
|
||||
|
||||
/** Set to true for super detailed debug logging. */
|
||||
const DEBUG_VERBOSE = false;
|
||||
|
||||
const browser = (typeof chrome !== 'undefined' && chrome.runtime?.connectNative)
|
||||
? chrome
|
||||
: (globalThis.browser ?? chrome);
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Background main logic (after all modules loaded).
|
||||
* Used by both Chrome (via importScripts from background.js) and Firefox (via background.scripts).
|
||||
*/
|
||||
log('background: SW started v' + SW_VERSION);
|
||||
|
||||
// Apply PAC immediately on startup using default port; updated once native host connects
|
||||
applyPAC();
|
||||
connect();
|
||||
|
||||
// Redirect *.host.test (and similar typos) to extension page so we can show "use .hole.sail"
|
||||
const WRONG_DOMAIN_RULE_ID = 1;
|
||||
if (browser.declarativeNetRequest && browser.runtime.getURL) {
|
||||
const redirectUrl = browser.runtime.getURL('wrong-domain.html') + '?host=\\1';
|
||||
browser.declarativeNetRequest.updateDynamicRules({
|
||||
removeRuleIds: [WRONG_DOMAIN_RULE_ID],
|
||||
addRules: [{
|
||||
id: WRONG_DOMAIN_RULE_ID,
|
||||
priority: 1,
|
||||
condition: {
|
||||
regexFilter: '^https?://([^/:]+)\\.host\\.test($|/)',
|
||||
resourceTypes: ['main_frame']
|
||||
},
|
||||
action: {
|
||||
type: 'redirect',
|
||||
redirect: { regexSubstitution: redirectUrl }
|
||||
}
|
||||
}]
|
||||
}).catch((err) => log('declarativeNetRequest rule failed:', err.message));
|
||||
}
|
||||
|
||||
// Open dashboard when extension icon is clicked
|
||||
browser.action?.onClicked?.addListener((tab) => {
|
||||
browser.tabs.create({ url: 'dashboard/dashboard.html' });
|
||||
});
|
||||
@@ -29,7 +29,16 @@
|
||||
}
|
||||
},
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
"scripts": [
|
||||
"background/background-boot.js",
|
||||
"background/logs.js",
|
||||
"background/state.js",
|
||||
"background/proxy.js",
|
||||
"background/native-messaging.js",
|
||||
"background/tab-lifecycle.js",
|
||||
"background/message-router.js",
|
||||
"background/background-main.js"
|
||||
]
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user