Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve browser detection fix #68 #86

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/browser-polyfill.js
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

if (typeof browser === "undefined") {
if (typeof window.browser === "undefined") {
Copy link
Contributor

@ExE-Boss ExE-Boss May 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quoting myself from #68 (comment):

I personally prefer using:

if (!("browser" in window)) {
    // [...]
}

because using window.browser directly when testing if it exists tends to print dereferencing an undefined reference warnings in the console.

Edit: Turns out that that causes the tests to fail because of jsdom/jsdom#1622:

// Set (or reset) the browser property.
if (browserObject) {
window.browser = browserObject;
} else {
// TODO: change into `delete window.browser` once tmpvar/jsdom#1622 has been fixed.
window.browser = undefined;
}

But using this function works:

const isDefined = (obj, key) => {
  if (typeof obj !== "object" || obj === null) {
    return false;
  }
  return key in obj && typeof obj[key] !== "undefined";
};

Edit 2: Turns out that that doesn’t work either because of #114 (comment) and #115 (comment)

// Wrapping the bulk of this polyfill in a one-time-use function is a minor
// optimization for Firefox. Since Spidermonkey does not fully parse the
// contents of a function until the first time it's called, and since it will
Expand Down