How can I know if browser is Chrome vs Firefox from web extension popup JavaScript?

后端 未结 3 1547
盖世英雄少女心
盖世英雄少女心 2021-01-25 06:05

I am using the chrome namespace for both Chrome and Firefox, but would like to know which browser is running the web extension.

3条回答
  •  没有蜡笔的小新
    2021-01-25 06:45

    • Check chrome.app which is absent in Firefox:

      const isFirefox = !chrome.app;
      
    • Check for browser which is absent in Chrome:

      const isFirefox = window.browser && browser.runtime;
      

      (the additional check is to avoid false positives on pages that have an element with id="browser" that creates a named property on window object for this element)

    • Use the asynchronous browser.runtime.getBrowserInfo.

    P.S. navigator.userAgent may be changed during debugging in devtools when switching to device mode or via about:config option in Firefox so it's an unreliable source.

提交回复
热议问题