Skip to content

Commit 37895b4

Browse files
committed
revert a89548a for broadcasts + use BroadcastChannel
1 parent 624d16e commit 37895b4

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

src/background/broadcast.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import '@/js/browser';
22
import {kTabOvr, pExposeIframes, pStyleViaASS} from '@/js/consts';
33
import {rxIgnorableError} from '@/js/msg-api';
4-
import {ownRoot} from '@/js/urls';
54
import {sleep0} from '@/js/util';
65
import {isOptionSite, optionSites} from './option-sites';
76
import {tabCache} from './tab-manager';
@@ -11,6 +10,7 @@ let toBroadcast;
1110
let toBroadcastCfg;
1211
let toBroadcastUpdStyles;
1312
const OLD = Symbol('old');
13+
const channel = new BroadcastChannel('sw');
1414

1515
export function broadcast(data, cfg) {
1616
toBroadcast ??= (setTimeout(doBroadcast), []);
@@ -49,22 +49,24 @@ async function doBroadcast() {
4949
const tabOverrides = tabCache[t.id]?.[kTabOvr];
5050
const patched = tabOverrides && Object.keys(tabOverrides).length &&
5151
patchStyles(updStyles, tabOverrides);
52-
if (!url.startsWith(ownRoot) || patched) {
53-
if (assSites) cfg.ass = isOptionSite(assSites, url);
54-
if (iframeSites) cfg.top = isOptionSite(iframeSites, url);
55-
sendTab(t.id, data, null, true);
56-
if (patched) for (const p of patched) p.enabled = p[OLD];
57-
/* Broadcast messages are tiny, but sending them takes some time anyway,
58-
so we're yielding for a possible navigation/messaging event. */
59-
if (++cnt > 50) {
60-
cnt = 0;
61-
await sleep0();
62-
}
52+
if (assSites) cfg.ass = isOptionSite(assSites, url);
53+
if (iframeSites) cfg.top = isOptionSite(iframeSites, url);
54+
sendTab(t.id, data, null, true);
55+
if (patched) for (const p of patched) p.enabled = p[OLD];
56+
/* Broadcast messages are tiny, but sending them takes some time anyway,
57+
so we're yielding for a possible navigation/messaging event. */
58+
if (++cnt > 50) {
59+
cnt = 0;
60+
await sleep0();
6361
}
6462
}
6563
}
6664

6765
export function broadcastExtension(data, multi) {
66+
if (__.MV3) {
67+
channel.postMessage({id: 1, args: [data, {}, multi, /*broadcast*/true]});
68+
return;
69+
}
6870
return unwrap(browser.runtime.sendMessage({data, multi, broadcast: true}));
6971
}
7072

src/js/msg-init.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/** Don't use this file in content script context! */
22
import './browser';
33
import {k_busy, k_deepCopy, k_msgExec, kInvokeAPI} from '@/js/consts';
4+
import {_execute} from './msg';
45
import {apiHandler, apiSendProxy, isPopup} from './msg-api';
5-
import {createPortExec, createPortProxy} from './port';
6+
import {createPortExec, createPortProxy, initRemotePort} from './port';
67
import {swPath, workerPath} from './urls';
78
import {deepCopy} from './util';
89
import {getOwnTab, ownTab} from './util-webext';
@@ -20,6 +21,9 @@ const swExec = __.MV3 &&
2021
const workerApiPrefix = 'worker.';
2122
let workerProxy;
2223
export let bg = __.IS_BG ? self : !__.MV3 && chrome.extension.getBackgroundPage();
24+
if (!__.IS_BG) {
25+
initRemotePort.call(_execute, {ports: [new BroadcastChannel('sw')]}, /*silent*/true);
26+
}
2327

2428
async function invokeAPI({name: path}, _thisObj, args) {
2529
// Non-cloneable event is passed when doing `elem.onclick = API.foo`

src/js/msg.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ function onRuntimeMessage({data, multi, TDM, broadcast}, sender, sendResponse) {
5959
return;
6060
}
6161
sender.TDM = TDM;
62-
let res = __.IS_BG && global[k_busy];
63-
res = res
62+
let res;
63+
res = __.IS_BG && (res = global[k_busy])
6464
? res.then(_execute.bind(null, data, sender, multi, broadcast))
6565
: _execute(data, sender, multi, broadcast);
66+
if (broadcast)
67+
return;
6668
if (res instanceof Promise) {
6769
res.then(wrapData, wrapError).then(sendResponse);
6870
return true;

src/js/port.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ export function createPortExec(getTarget, {lock, once} = {}, target) {
165165
/**
166166
* @this {Function | {}} executor
167167
* @param {MessageEvent} evt
168+
* @param {boolean} [silent]
168169
*/
169-
export function initRemotePort(evt) {
170+
export function initRemotePort(evt, silent) {
170171
const {id: once} = evt.data || {};
171172
const exec = this;
172173
const port = evt.ports[0];
@@ -198,7 +199,7 @@ export function initRemotePort(evt) {
198199
}
199200
}
200201
__.DEBUGPORT('%c%s port response', 'color:blue', PATH, id, {res, err});
201-
port.postMessage({id, res, err}, portEvent._transfer);
202+
if (!silent) port.postMessage({id, res, err}, portEvent._transfer);
202203
if (!--numJobs && willAutoClose && !bgLock) {
203204
autoClose(TTL);
204205
}

0 commit comments

Comments
 (0)