Skip to content

Commit

Permalink
fix listener registration
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Dec 24, 2024
1 parent 0e018f8 commit 4e0db58
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/background/style-via-webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ function toggle(prefKey) {
return;
}
let v;
if (__.BUILD !== 'chrome' && FIREFOX || (
if (__.BUILD !== 'chrome' && FIREFOX || off !== curOFF && (
__.MV3
? csp !== curCSP
: (xhr || csp) !== (curXHR || curCSP)
)) {
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;
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/js/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/manage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})();

Expand Down

0 comments on commit 4e0db58

Please # to comment.