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
Conversation
By checking window.browser the webpack.ProvidePlugin won’t be confused.
@rpl is there anything I can help you with to get the PR merged? |
|
@@ -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") { |
There was a problem hiding this comment.
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 usingwindow.browser
directly when testing if it exists tends to print dereferencing anundefined
reference warnings in the console.Edit: Turns out that that causes the tests to fail because of
jsdom/jsdom#1622:webextension-polyfill/test/setup.js
Lines 44 to 50 in 6f9cfdf
// 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)
The polyfill is supposed to be a no-op (not doing anything) in Firefox. In a content script, Would #86 (comment), using if (this.browser === undefined && window.browser === undefined) {
} I'm closing the issue anyway, since there is a merge conflict and we intend to change the detection logic for Edge support (#3). |
So what is the suggested solution for using with plugins: [
new webpack.ProvidePlugin({
browser: ['webextension-polyfill'],
}),
] |
|
That won’t work on Firefox in content scripts, as in content scripts on Firefox, See also b3609f7. |
Just comparing with |
@HaNdTriX Thanks for solution. @Rob--W What about adding a short note about incompatibility with |
@vitalets The solution from @HaNdTriX should not be used, because 1) it causes the polyfill to load unconditionally in Firefox, and 2) it causes the polyfill to break in web pages that trigger registration of the "browser" global (see #153). I don't have a solution ready yet, but for the time being I have opened #156 to track documentation of this feature. |
@Rob--W Thanks for letting me know. Nevertheless we check inside webpack and exclude the polyfill for firefox anyway. Here is our current workaround. Please keep in mind that on firefox these lines will be omitted. |
In Firefox window.browser is undefined as opposed to browser. |
By checking window.browser the
webpack.ProvidePlugin
won’t be confused.