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

How not to include in Firefox #55

Closed
DamienCassou opened this issue Sep 10, 2017 · 7 comments
Closed

How not to include in Firefox #55

DamienCassou opened this issue Sep 10, 2017 · 7 comments

Comments

@DamienCassou
Copy link

My add-on sends webextension-polyfill dynamically to the active tab from a popup or sidebar. I would like to stop including the polyfill in Firefox releases of my add-on. How can I not send the polyfill in Firefox while still sending it to Chromium?

@Rob--W
Copy link
Member

Rob--W commented Sep 10, 2017

The polyfill is already guarded on if (typeof browser === "undefined") {, so it does not polyfill anything in Firefox:

if (typeof browser === "undefined") {

If you don't want to load the file at all, you can either modify you build script to not include the polyfill or use an empty file instead of the polyfill, or use typeof browser === "undefined" to detect at runtime whether you need the polyfill.

@DamienCassou
Copy link
Author

thank you for your quick answer.

it does not polyfill anything in Firefox

yes, I have been including the polyfill unconditionally for some time without trouble in another add-on. Still, it bothers me to include code I don't need. Additionally, I get a lint warning when I do it:

Code            Message                     Description                                                                  File                      Line   Column
KNOWN_LIBRARY   Known JS library detected   JavaScript libraries are discouraged for simple add-ons, but are generally   browser-polyfill.min.js                
                                            accepted.                                                                                                           

Nothing serious here. It would just be nicer not to include it.

you can either modify you build script to not include the polyfill

if I do that, how do you suggest to adapt this code?

async function sendScript (tabId) {
  await browser.tabs.executeScript(tabId, {file: '/browser-polyfill.min.js'})
  await browser.tabs.executeScript(tabId, {file: '/content-script.js'})
}

use an empty file instead of the polyfill

that's a bit ugly, but the best solution I have heard so far. Thanks :-).

use typeof browser === "undefined" to detect at runtime whether you need the polyfill

that's not going to work here as the code which sends the content-script already knows about browser.

@Rob--W
Copy link
Member

Rob--W commented Sep 10, 2017

You could save the result of typeof browser === "undefined" before you load the polyfill in the extension page that invokes browser.tabs.executeScript.

@asamuzaK
Copy link

Like this? (I did not test it though)

async function sendScript (tabId) {
  if (browser.runtime.id !== 'your_webext_id') {
    await browser.tabs.executeScript(tabId, {file: '/browser-polyfill.min.js'})
  }
  await browser.tabs.executeScript(tabId, {file: '/content-script.js'})
}

@DamienCassou
Copy link
Author

@asamuzaK IIUC, you are proposing to distinguish the browser through the extension ID as it is going to differ from a browser to another one. This is smart. Thanks.

@fregante
Copy link
Contributor

fregante commented Nov 7, 2017

By the way, you don’t need to await executeScript, they’re executed in order anyway. I verified this before in chrome by having load a 70KB JS file first and then a one-liner.js: they still executed in order.

@Rob--W
Copy link
Member

Rob--W commented Jul 19, 2018

We have documented that the extension does not do anything in Firefox: https://github.com/mozilla/webextension-polyfill#issues-that-happen-only-when-running-on-firefox

If you somehow want to detect Firefox / Chrome based on API behavior, then the approach in #55 (comment) can be used (among others).

@Rob--W Rob--W closed this as completed Jul 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants