Skip to content

Commit

Permalink
fix: no-op check to work with Safari and support of old browsers. (#582)
Browse files Browse the repository at this point in the history
Verify the object that might be at `globalThis.browser` is not already
implementing the basic Web Extension APIs. This is symmetric with the
check to verify the poly fill is only included in extension contexts.

Don't use optional-chaining for these checks, since this needs to
continuing working in older browsers.
  • Loading branch information
xeenon committed Mar 28, 2024
1 parent 0cf8915 commit 871b49d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/browser-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

if (!globalThis.chrome?.runtime?.id) {
if (!(globalThis.chrome && globalThis.chrome.runtime && globalThis.chrome.runtime.id)) {
throw new Error("This script should only be loaded in a browser extension.");
}

if (typeof globalThis.browser === "undefined" || Object.getPrototypeOf(globalThis.browser) !== Object.prototype) {
if (!(globalThis.browser && globalThis.browser.runtime && globalThis.browser.runtime.id)) {
const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received.";

// Wrapping the bulk of this polyfill in a one-time-use function is a minor
Expand Down

0 comments on commit 871b49d

Please sign in to comment.