'How to determine in which browser your extension background script is executing?
I'm talking about Chrome extensions, Firefox WebExtensions, Edge extensions...
In a background script, not a content script, is there a clear way to know which browser I am using? I need to do different operations for different browsers.
Yes, navigator.userAgent can be useful, but it's not very clear.
Is there any extension API that can be used to do this? Something like, chrome.extension.browserType. (Of course, this one doesn't really exist..)
Solution 1:[1]
Here's another technique. The browser.identity.getRedirectUrl function will return a URL post-fixed with:
.extensions.allizom.org/when running from Firefox.chromiumapp.org/when running from Chrome/Chromium
It's simple enough to call this upon startup and store it in the runtime state of your background script. Edge and Opera also support this function.
Now usually Web Extension API calls start with chrome when running in Chrome, but by this point cross-browser extension writers should be used to the Web Extension Polyfill JS, and you'll need to use this too if you want the call to browser.identity.getRedirectUrl to work on any browser.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Colin Woodbury |
