From a82abfdbccb2bfcf3202268278d108359192ca9a Mon Sep 17 00:00:00 2001 From: Louis-Marie Michelin Date: Mon, 15 Aug 2022 23:45:42 +0200 Subject: [PATCH 1/2] chore: lint --- typings/declarations.d.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/typings/declarations.d.ts b/typings/declarations.d.ts index eb3bc18c..0cb4a739 100644 --- a/typings/declarations.d.ts +++ b/typings/declarations.d.ts @@ -10,10 +10,7 @@ declare interface IMiddlewareTemplateParams { reloadPage: boolean; } -declare type InjectMiddleware = ( - assets: Record, - chunks: Set, -) => Record; +declare type InjectMiddleware = (assets: Record, chunks: Set) => Record; declare type MiddlewareInjector = ( { background, contentScript, extensionPage }: IEntriesOption, @@ -22,10 +19,7 @@ declare type MiddlewareInjector = ( declare type Triggerer = (onlyPageChanged: boolean) => Promise; -declare type TriggererFactory = ( - port: number, - reloadPage: boolean, -) => Triggerer; +declare type TriggererFactory = (port: number, reloadPage: boolean) => Triggerer; declare type VersionPair = [number | undefined, number | undefined]; @@ -45,13 +39,7 @@ declare type LOG_WARN = 3; declare type LOG_ERROR = 4; declare type LOG_DEBUG = 5; -declare type LOG_LEVEL = - | LOG_NONE - | LOG_LOG - | LOG_INFO - | LOG_WARN - | LOG_ERROR - | LOG_DEBUG; +declare type LOG_LEVEL = LOG_NONE | LOG_LOG | LOG_INFO | LOG_WARN | LOG_ERROR | LOG_DEBUG; declare interface IWebpackChunk { files: string[]; From 931d5843bfc98136c1328e42dfe9228c941c528e Mon Sep 17 00:00:00 2001 From: Louis-Marie Michelin Date: Mon, 15 Aug 2022 23:40:23 +0200 Subject: [PATCH 2/2] add compatibility with manifest V3 --- src/messages/errors.ts | 6 +++++- src/middleware/wer-middleware.raw.ts | 11 +++++++---- src/utils/manifest.ts | 4 ++-- typings/declarations.d.ts | 4 +--- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/messages/errors.ts b/src/messages/errors.ts index ded58890..5c48dfc8 100644 --- a/src/messages/errors.ts +++ b/src/messages/errors.ts @@ -10,4 +10,8 @@ the provided on 'manifest.json' or 'entry.background' \ option of the plugin", ); -export const bgScriptManifestRequiredMsg = new Message(ERROR, 2, "Background script on manifest is required"); +export const bgScriptManifestRequiredMsg = new Message( + ERROR, + 2, + "Background script or service worker on manifest is required", +); diff --git a/src/middleware/wer-middleware.raw.ts b/src/middleware/wer-middleware.raw.ts index 2f6089ab..9f16dd8a 100644 --- a/src/middleware/wer-middleware.raw.ts +++ b/src/middleware/wer-middleware.raw.ts @@ -5,7 +5,7 @@ /* This will be converted into a lodash templ., any */ /* external argument must be provided using it */ /* -------------------------------------------------- */ -(function(window) { +(function() { const injectionContext = this || window || {browser: null}; @@ -71,7 +71,9 @@ if (type === SIGN_CHANGE && (!payload || !payload.onlyPageChanged)) { tabs.query({ status: "complete" }).then(loadedTabs => { loadedTabs.forEach( - tab => tab.id && tabs.sendMessage(tab.id, { type: SIGN_RELOAD }), + // in MV3 tabs.sendMessage returns a Promise and we need to catch the errors + // https://groups.google.com/a/chromium.org/g/chromium-extensions/c/st_Nh7j3908/m/1muOgSX5AwAJ + tab => tab.id && tabs.sendMessage(tab.id, { type: SIGN_RELOAD })?.catch(() => null), ); socket.send( JSON.stringify({ @@ -137,9 +139,10 @@ // ======================= Bootstraps the middleware =========================== // runtime.reload - ? extension.getBackgroundPage() === window ? backgroundWorker(new WebSocket(wsHost)) : extensionPageWorker() + // in MV3 background service workers don't have access to the DOM + ? (typeof window === 'undefined' || extension.getBackgroundPage() === window) ? backgroundWorker(new WebSocket(wsHost)) : extensionPageWorker() : contentScriptWorker(); -})(window); +})(); /* ----------------------------------------------- */ /* End of Webpack Hot Extension Middleware */ diff --git a/src/utils/manifest.ts b/src/utils/manifest.ts index b3ed89f2..53f3e38d 100644 --- a/src/utils/manifest.ts +++ b/src/utils/manifest.ts @@ -16,11 +16,11 @@ export function extractEntries( throw new Error("Please specify the `output.filename` in your webpack config."); } - if (!background?.scripts) { + if (!(background?.scripts || background?.service_worker)) { throw new TypeError(bgScriptManifestRequiredMsg.get()); } - const bgScriptFileNames = background.scripts; + const bgScriptFileNames = background.service_worker ? [background.service_worker] : background.scripts ?? []; const toRemove = (filename as string).replace("[name]", ""); const bgWebpackEntry = Object.keys(webpackEntry).find((entryName) => diff --git a/typings/declarations.d.ts b/typings/declarations.d.ts index 0cb4a739..e088f087 100644 --- a/typings/declarations.d.ts +++ b/typings/declarations.d.ts @@ -61,13 +61,11 @@ declare interface IExtensionManifest { background?: { page?: string; scripts?: string[]; + service_worker?: string; }; icons?: { [key: string]: string; }; - browser_action?: { - default_popup: string; - }; content_scripts?: [ { matches: string[];