Skip to content

Commit 9452551

Browse files
committed
🐛 Fix injection context (Closes #494)
1 parent adf61ff commit 9452551

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/middleware/wer-middleware.raw.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint:disable */
21
/* -------------------------------------------------- */
32
/* Start of Webpack Hot Extension Middleware */
43
/* ================================================== */
@@ -7,15 +6,26 @@
76
/* -------------------------------------------------- */
87
(function() {
98

10-
const injectionContext = this || window || {browser: null};
11-
129
(function() {
1310
`<%= polyfillSource %>`;
1411
})();
1512

16-
const { browser }: any = injectionContext || {};
17-
const signals: any = JSON.parse('<%= signals %>');
18-
const config: any = JSON.parse('<%= config %>');
13+
const formatter = (msg: string) => `[ WER: ${msg} ]`;
14+
const logger = (msg, level = "info") => console[level](formatter(msg));
15+
const timeFormatter = (date: Date) =>
16+
date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
17+
18+
// eslint-disable-next-line no-restricted-globals
19+
const injectionContext = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this;
20+
const browser = injectionContext.browser || injectionContext.chrome;
21+
22+
if (!browser) {
23+
logger("Browser API is not available", "warn");
24+
return;
25+
}
26+
27+
const signals = JSON.parse('<%= signals %>');
28+
const config = JSON.parse('<%= config %>');
1929

2030
const reloadPage: boolean = ("<%= reloadPage %>" as "true" | "false") === "true";
2131
const wsHost = "<%= WSHost %>";
@@ -31,12 +41,6 @@
3141
const { extension, runtime, tabs } = browser;
3242
const manifest = runtime.getManifest();
3343

34-
// =============================== Helper functions ======================================= //
35-
const formatter = (msg: string) => `[ WER: ${msg} ]`;
36-
const logger = (msg, level = "info") => console[level](formatter(msg));
37-
const timeFormatter = (date: Date) =>
38-
date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
39-
4044
// ========================== Called only on content scripts ============================== //
4145
function contentScriptWorker() {
4246
runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg));
@@ -71,8 +75,6 @@
7175
if (type === SIGN_CHANGE && (!payload || !payload.onlyPageChanged)) {
7276
tabs.query({ status: "complete" }).then(loadedTabs => {
7377
loadedTabs.forEach(
74-
// in MV3 tabs.sendMessage returns a Promise and we need to catch the errors
75-
// https://groups.google.com/a/chromium.org/g/chromium-extensions/c/st_Nh7j3908/m/1muOgSX5AwAJ
7678
tab => tab.id && tabs.sendMessage(tab.id, { type: SIGN_RELOAD })?.catch(() => null),
7779
);
7880
socket.send(
@@ -139,7 +141,6 @@
139141

140142
// ======================= Bootstraps the middleware =========================== //
141143
runtime.reload
142-
// in MV3 background service workers don't have access to the DOM
143144
? (typeof window === 'undefined' || extension.getBackgroundPage() === window) ? backgroundWorker(new WebSocket(wsHost)) : extensionPageWorker()
144145
: contentScriptWorker();
145146
})();

0 commit comments

Comments
 (0)