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..)
There is no specific API to detect which browser is currently being used. One of the benefits of the major browsers moving to support a common extension framework is being able to have a single codebase which supports multiple browsers. While the set of functionality which is available from all applicable browsers is growing, there will always be some differences. These differences are not just in what is supported, but in some cased are in the specifics of the effects for a particular API, or how the API must be used.1,2 Thus, for some things, it is necessary to be able to determine which browser the code is currently running.
There is some good code available from the top-voted answer to "How to detect Safari, Chrome, IE, Firefox and Opera browser?". However, it needs some modification to work in an extension environment.
Based on the code in that answer, the following will detect:
chrome.*
/browser.*
APIs. One way that this is being done is that there is a mechanism called WebExtensions Experiments which is intended for non-Mozilla developers to implement additional functionality for WebExtensions. The intent is that such functionality, if approved, will be migrated into stock Firefox builds.