|
1 |
| -/* eslint:disable */ |
2 | 1 | /* -------------------------------------------------- */
|
3 | 2 | /* Start of Webpack Hot Extension Middleware */
|
4 | 3 | /* ================================================== */
|
|
7 | 6 | /* -------------------------------------------------- */
|
8 | 7 | (function() {
|
9 | 8 |
|
10 |
| - const injectionContext = this || window || {browser: null}; |
11 |
| - |
12 | 9 | (function() {
|
13 | 10 | `<%= polyfillSource %>`;
|
14 | 11 | })();
|
15 | 12 |
|
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 %>'); |
19 | 29 |
|
20 | 30 | const reloadPage: boolean = ("<%= reloadPage %>" as "true" | "false") === "true";
|
21 | 31 | const wsHost = "<%= WSHost %>";
|
|
31 | 41 | const { extension, runtime, tabs } = browser;
|
32 | 42 | const manifest = runtime.getManifest();
|
33 | 43 |
|
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 |
| - |
40 | 44 | // ========================== Called only on content scripts ============================== //
|
41 | 45 | function contentScriptWorker() {
|
42 | 46 | runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg));
|
|
71 | 75 | if (type === SIGN_CHANGE && (!payload || !payload.onlyPageChanged)) {
|
72 | 76 | tabs.query({ status: "complete" }).then(loadedTabs => {
|
73 | 77 | 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 |
76 | 78 | tab => tab.id && tabs.sendMessage(tab.id, { type: SIGN_RELOAD })?.catch(() => null),
|
77 | 79 | );
|
78 | 80 | socket.send(
|
|
139 | 141 |
|
140 | 142 | // ======================= Bootstraps the middleware =========================== //
|
141 | 143 | runtime.reload
|
142 |
| - // in MV3 background service workers don't have access to the DOM |
143 | 144 | ? (typeof window === 'undefined' || extension.getBackgroundPage() === window) ? backgroundWorker(new WebSocket(wsHost)) : extensionPageWorker()
|
144 | 145 | : contentScriptWorker();
|
145 | 146 | })();
|
|
0 commit comments