From 4e0db5838a871a3ff43ac47a9f0b1ccb243de082 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 24 Dec 2024 17:59:41 +0300 Subject: [PATCH] fix listener registration --- src/background/style-via-webrequest.js | 11 +++++++---- src/js/msg.js | 3 +++ src/manage/index.js | 4 +++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/background/style-via-webrequest.js b/src/background/style-via-webrequest.js index 97f1b7aec8..7fd2f212cd 100644 --- a/src/background/style-via-webrequest.js +++ b/src/background/style-via-webrequest.js @@ -102,7 +102,7 @@ function toggle(prefKey) { return; } let v; - if (__.BUILD !== 'chrome' && FIREFOX || ( + if (__.BUILD !== 'chrome' && FIREFOX || off !== curOFF && ( __.MV3 ? csp !== curCSP : (xhr || csp) !== (curXHR || curCSP) @@ -110,14 +110,16 @@ function toggle(prefKey) { v = chrome.webRequest.onHeadersReceived; // unregister first since new registrations are additive internally toggleListener(v, false, modifyHeaders); - toggleListener(v, true, modifyHeaders, WR_FILTER, [ + toggleListener(v, !off, modifyHeaders, WR_FILTER, [ 'blocking', 'responseHeaders', !__.MV3 && xhr && chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS, ].filter(Boolean)); } - if (mv3init || off !== curOFF) { - toggleListener(chrome.webRequest.onBeforeRequest, mv3init || !off, prepareStyles, WR_FILTER); + if (mv3init || off !== curOFF && (xhr || csp) !== (curXHR || curCSP)) { + toggleListener(chrome.webRequest.onBeforeRequest, + mv3init || !off && (xhr || csp), + prepareStyles, WR_FILTER); } curCSP = csp; curOFF = off; @@ -126,6 +128,7 @@ function toggle(prefKey) { /** @type {typeof chrome.webRequest.onBeforeRequest.callback} */ async function prepareStyles(req) { + if (!curXHR && !curCSP && !bgBusy) return; const {tabId, frameId, url} = req; if (tabId < 0) return; const key = tabId + ':' + frameId; const bgPreInitLen = __.MV3 && bgPreInit.length; diff --git a/src/js/msg.js b/src/js/msg.js index b28f03ae65..ace7f780c9 100644 --- a/src/js/msg.js +++ b/src/js/msg.js @@ -16,14 +16,17 @@ const handler = { // TODO: maybe move into browser.js and hook addListener to wrap/unwrap automatically chrome.runtime.onMessage.addListener(onRuntimeMessage); +/** @param {function} fn - return `undefined` by default to avoid breaking onRuntimeMessage */ export function onMessage(fn) { handler.both.add(fn); } +/** @param {function} fn - return `undefined` by default to avoid breaking onRuntimeMessage */ export function onTab(fn) { handler.tab.add(fn); } +/** @param {function} fn - return `undefined` by default to avoid breaking onRuntimeMessage */ export function onExtension(fn) { handler.extension.add(fn); } diff --git a/src/manage/index.js b/src/manage/index.js index 3092979d03..2ff0f15680 100644 --- a/src/manage/index.js +++ b/src/manage/index.js @@ -26,7 +26,9 @@ tBody(); if (newUI.hasFavs()) readBadFavs(badFavs); showStyles(styles, ids); renderSyncStatus(sync); - onExtension(e => e.method === 'syncStatusUpdate' && renderSyncStatus(e.status)); + onExtension(e => { // returning `undefined` by default to avoid breaking bg::onRuntimeMessage + if (e.method === 'syncStatusUpdate') renderSyncStatus(e.status); + }); import('./lazy-init'); })();