From ab5d3ce03109be76ce5a9a836544717de512e121 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 9 Nov 2021 20:01:54 +0100 Subject: [PATCH 1/4] Enable usage via `ProvidePlugin` --- src/browser-polyfill.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/browser-polyfill.js b/src/browser-polyfill.js index 3ece6274..ebac2d1a 100644 --- a/src/browser-polyfill.js +++ b/src/browser-polyfill.js @@ -6,7 +6,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; -if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.prototype) { +if (typeof window != "object" || typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) { + throw new Error("This script should only be loaded in a browser extension."); +} + +if (typeof window.browser === "undefined" || Object.getPrototypeOf(window.browser) !== Object.prototype) { const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received."; const SEND_RESPONSE_DEPRECATION_WARNING = "Returning a Promise is the preferred way to send a reply from an onMessage/onMessageExternal listener, as the sendResponse will be removed from the specs (See https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage)"; @@ -540,10 +544,6 @@ if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object. return wrapObject(extensionAPIs, staticWrappers, apiMetadata); }; - if (typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) { - throw new Error("This script should only be loaded in a browser extension."); - } - // The build process adds a UMD wrapper around this file, which makes the // `module` variable available. module.exports = wrapAPIs(chrome); From d61a168f31ede287a20b5b873fff8ce7075650c5 Mon Sep 17 00:00:00 2001 From: Federico Date: Thu, 25 Nov 2021 21:12:30 +0700 Subject: [PATCH 2/4] Use `globalThis` --- .eslintrc | 1 + src/browser-polyfill.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.eslintrc b/.eslintrc index def11446..31424355 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,6 +13,7 @@ }, "globals": { + "globalThis": true, }, "rules": { diff --git a/src/browser-polyfill.js b/src/browser-polyfill.js index ebac2d1a..8decde41 100644 --- a/src/browser-polyfill.js +++ b/src/browser-polyfill.js @@ -6,11 +6,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; -if (typeof window != "object" || typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) { +if (typeof globalThis != "object" || typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) { throw new Error("This script should only be loaded in a browser extension."); } -if (typeof window.browser === "undefined" || Object.getPrototypeOf(window.browser) !== Object.prototype) { +if (typeof globalThis.browser === "undefined" || Object.getPrototypeOf(globalThis.browser) !== Object.prototype) { const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received."; const SEND_RESPONSE_DEPRECATION_WARNING = "Returning a Promise is the preferred way to send a reply from an onMessage/onMessageExternal listener, as the sendResponse will be removed from the specs (See https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage)"; From 3e267312b3c05a8b0c143caa028ff4273bf8c0b0 Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 27 Nov 2021 11:41:32 +0700 Subject: [PATCH 3/4] Update lint to forbid `browser` global usage --- src/.eslintrc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/.eslintrc b/src/.eslintrc index bc39b257..e008c08f 100644 --- a/src/.eslintrc +++ b/src/.eslintrc @@ -2,10 +2,13 @@ "env": { "browser": true, "node": false, - "webextensions": true, + // Don't use `webextensions` because it enables the browser global. + // We want to use globalThis.browser instead: + // https://github.com/mozilla/webextension-polyfill/pull/351 }, "globals": { // Allow the `module` global, but not the `require(…)` function "module": false, + "chrome": true, }, } From d58fa73519482c06d06c3c7f724b9c2edc477312 Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 27 Nov 2021 11:41:57 +0700 Subject: [PATCH 4/4] =?UTF-8?q?Lint=20=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/browser-polyfill.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/browser-polyfill.js b/src/browser-polyfill.js index 8decde41..ca65b3b8 100644 --- a/src/browser-polyfill.js +++ b/src/browser-polyfill.js @@ -548,5 +548,5 @@ if (typeof globalThis.browser === "undefined" || Object.getPrototypeOf(globalThi // `module` variable available. module.exports = wrapAPIs(chrome); } else { - module.exports = browser; + module.exports = globalThis.browser; }