如何通过Web扩展弹出窗口JavaScript知道浏览器是Chrome还是Firefox? [英] How can I know if browser is Chrome vs Firefox from web extension popup JavaScript?

查看:109
本文介绍了如何通过Web扩展弹出窗口JavaScript知道浏览器是Chrome还是Firefox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Chrome和Firefox使用chrome命名空间,但想知道哪个浏览器正在运行Web扩展.

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

推荐答案

  • 检查Firefox中不存在的chrome.app:

    const isFirefox = !chrome.app;
    

  • 检查Chrome中不存在的browser:

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

    (额外的检查是避免在具有id="browser"元素的页面上创建误报,该元素在window对象上为此元素创建命名属性)

    (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)

    使用异步 browser.runtime.getBrowserInfo .

    P.S.切换到设备模式或通过Firefox中的about:config选项时,可能会在devtools中的调试过程中更改navigator.userAgent,因此它是不可靠的来源.

    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.

    这篇关于如何通过Web扩展弹出窗口JavaScript知道浏览器是Chrome还是Firefox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆