Skip to content

Commit

Permalink
fix: Throw error when loaded in non-extension contexts
Browse files Browse the repository at this point in the history
Fixes #186
  • Loading branch information
Rob--W committed Jun 4, 2019
1 parent 10bc37b commit c3e1dd5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/browser-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ 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);
Expand Down
11 changes: 11 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ function setupTestDOMWindow(chromeObject, browserObject = undefined) {
return new Promise((resolve, reject) => {
const window = testDOMWindow;

// Ensure that "chrome.runtime.id" is set, because the polyfill is only
// loaded in extension environments.
if (chromeObject) {
if (!chromeObject.runtime) {
chromeObject.runtime = {};
}
if (!chromeObject.runtime.id) {
chromeObject.runtime.id = "some-test-id-from-test-setup";
}
}

// Inject the fake chrome object used as a fixture for the particular
// browser-polyfill test scenario.
window.chrome = chromeObject;
Expand Down
11 changes: 11 additions & 0 deletions test/test-browser-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ const {deepEqual, equal, ok} = require("chai").assert;
const {setupTestDOMWindow} = require("./setup");

describe("browser-polyfill", () => {
it("throws an error in a non-extension environment", async () => {
try {
await setupTestDOMWindow(null);
ok(false, "The polyfill script should have failed to load.");
} catch (e) {
equal(e.message,
"This script should only be loaded in a browser extension.",
"Expected script to not load in a non-extension environment");
}
});

it("wraps the global chrome namespace with a global browser namespace", () => {
const fakeChrome = {};
return setupTestDOMWindow(fakeChrome).then(window => {
Expand Down
9 changes: 8 additions & 1 deletion test/test-proxied-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,16 @@ describe("browser-polyfill", () => {
Object.defineProperty(this, "runtime", {value});
return value;
},
get tabs() {
ok(false, "chrome.tabs should not lazily be initialized without explicit API call");
},
};
return setupTestDOMWindow(fakeChrome).then(window => {
equal(lazyInitCount, 0, "chrome.runtime should not be initialized without explicit API call");
// This used to be equal(lazyInitCount, 0, ...), but was changed to
// accomodate a change in the implementation of the polyfill.
// To verify that APIs are not unnecessarily initialized, the fakeChrome
// object has a "tabs" getter that fails the test upon access.
equal(lazyInitCount, 1, "chrome.runtime should be initialized because chrome.runtime.id is accessed during polyfill intiialization");

window.browser.runtime.onMessage.addListener(() => {});
equal(lazyInitCount, 1, "chrome.runtime should be initialized upon accessing browser.runtime");
Expand Down

0 comments on commit c3e1dd5

Please sign in to comment.