Bowser package for cross-browser extensions?

168 views
Skip to first unread message

Inter Net

unread,
Oct 22, 2021, 5:15:11 AM10/22/21
to Chromium Extensions
I try to use this pattern to develop cross browser extensions, is there any downside or reason I should not do it like this? (I use the npm package 'bowser')

```
import Bowser from 'bowser';

export const onMoved = (onMoved: () => void) => {
  const CurrBrowser = Bowser.getParser(window.navigator.userAgent);

  if (CurrBrowser.getBrowserName() === 'Chrome') {
    chrome.bookmarks.onMoved.addListener(onMoved);
  }

  if (CurrBrowser.getBrowserName() === 'Firefox') {
    browser.bookmarks.onMoved.addListener(onMoved);
  }
};
```

Kent Brewster

unread,
Oct 22, 2021, 12:52:15 PM10/22/21
to Inter Net, Chromium Extensions
If all you need is a polyfill for browser, why not do something like this?

// check for Chrome first, then "browser," which is the Web standards version
const browser = chrome || browser;
chrome.bookmarks.onMoved.addListener(onMoved);

That said, there's a use case (logging and stats) for knowing which browser is actually running your extension. Here's one way of doing it without a library:

function returnBrowser() {
// default: unknown browser
let browser = 'xx';
// feel free to clone this from a config file
const browserTest = [
// run me first
{
k: 'ff',
r: / Firefox\//,
},
// run me before Chrome
{
k: 'op',
r: / OPR\//,
},
// run me before Chrome
{
k: 'ms',
// to catch all versions this must be Edg, not Edge
r: / Edg\//,
},
// run me after Opera and Edge but before Safari
{
k: 'cr',
r: / Chrome\//,
},
// run me after Chrome
{
k: 'sa',
r: / Safari\//,
},
];
// search for matches
browserTest.forEach((it) => {
if (window.navigator.userAgent.match(it.r)) {
// found it!
browser = it.k;
// break out of the forEach loop
browserTest.length = 0;
};
});
return browser;
}

// tests good on current versions of all browsers listed above; paste into console window to try it yourself

console.log(window.navigator.userAgent);
console.log(returnBrowser());

--Kent



--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/56e54ab4-4697-4720-8a08-905452ebea10n%40chromium.org.

Inter Net

unread,
Oct 22, 2021, 1:07:18 PM10/22/21
to Chromium Extensions, Kent Brewster, Chromium Extensions, Inter Net
Thanks, did you mean in your first example to call the const browser maybe instead of chrome?  browser.bookmarks.onMoved.addListener(onMoved);

Kent Brewster

unread,
Oct 22, 2021, 1:12:25 PM10/22/21
to Inter Net, Chromium Extensions
Yes, that's exactly right.

--Kent

Carlos

unread,
Oct 22, 2021, 1:46:24 PM10/22/21
to Chromium Extensions, Kent Brewster, Chromium Extensions, zela...@gmail.com
That is not going to work. If chrome is undefined, you will end up with this error:
"Uncaught ReferenceError: chrome is not defined"

So you will have to use typeOf. In addition, chrome might be defined but not include extension APIs. So you will also have to check if chrome.runtime is present.
Op vrijdag 22 oktober 2021 om 19:12:25 UTC+2 schreef Kent Brewster:

Cuyler Stuwe

unread,
Oct 22, 2021, 3:11:22 PM10/22/21
to Carlos, Chromium Extensions, Kent Brewster, zela...@gmail.com
All chromium-based browsers work w/ the chrome.* namespace.

Firefox polyfills the Chrome namespace and is mostly compatible.

The Browser namespace holds an API that can be subtly different in some places.

Safari is so different from the others that it's not even worth trying to make a Safari extension from one shared codebase at all.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Cuyler Stuwe

unread,
Oct 22, 2021, 3:12:43 PM10/22/21
to Carlos, Chromium Extensions, Kent Brewster, zela...@gmail.com
You also have the issue that at the current time (AFAIK), various browsers might be at various states of implementing MV3 (including possibly not implementing it whatsoever).

hrg...@gmail.com

unread,
Oct 22, 2021, 3:40:58 PM10/22/21
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, Kent Brewster, zela...@gmail.com, Carlos
Other than Chromium and Firefox, is there any other browser implementing MV3?

Inter Net

unread,
Oct 22, 2021, 4:19:29 PM10/22/21
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, Kent Brewster, Inter Net, Carlos
I'm not so afraid of this when MV3 is ready and safe to use this doesn't mean v2 will stop working I will just make the needed changes and update the plugin.

Kent Brewster

unread,
Oct 22, 2021, 4:50:03 PM10/22/21
to Carlos, Chromium Extensions, zela...@gmail.com
Can you show me an instance within an extension's background process where chrome is undefined? Chrome and Firefox both seem to have it; not sure about Safari.

--Kent

Cuyler Stuwe

unread,
Oct 22, 2021, 5:09:42 PM10/22/21
to hrg...@gmail.com, Chromium Extensions, Kent Brewster, zela...@gmail.com, Carlos
IIRC I think I saw reps from Apple for Safari existing within the WECG for standardizing WebExtensions around MV3.

Carlos

unread,
Oct 23, 2021, 4:27:26 AM10/23/21
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, Kent Brewster, zela...@gmail.com, Carlos, hrg...@gmail.com
Safari also has chrome defined. The only situation when chrome was not having the extension APIs is in Microsoft Edge.

As for MV3, this is very likely to be implemented in all browsers. Tho there are some APIs which won't be phased out in some browsers. For example, Mozilla Firefox will keep the webRequest API in MV3.

Op vrijdag 22 oktober 2021 om 23:09:42 UTC+2 schreef salem...@gmail.com:
Reply all
Reply to author
Forward
0 new messages