简体   繁体   English

How to get window.browser object for firefox in JavaScript or Angular?

[英]How to get window.browser object for firefox in JavaScript or Angular?

I am developing an Angular application to check whether my extension is installed or not in Firefox.我正在开发一个 Angular 应用程序来检查我的扩展是否安装在 Firefox 中。 For this I used window['browser'].runtime.sendMessage() .为此,我使用了window['browser'].runtime.sendMessage() It will give a window.browser is not defined error, so I used it through a JavaScript file, and got the same error.它会给出一个window.browser is not defined错误,所以我通过一个 JavaScript 文件使用它,得到了同样的错误。

Code:代码:

function sendMessagePromise(tabId, item) {
    return new Promise((resolve, reject) => {
        window.browser.runtime.sendMessage(tabId, item, response => {
            if (response != undefined) {
                resolve(true);
            } else {
                reject('Something wrong');
            }
        });
    });
}

Did you mean that you want an web service to know whether the extension is installed to the visitor's browser or not?您的意思是您想要一个 web 服务来了解访问者的浏览器是否安装了扩展程序?

The object browser (and also chrome ) is available only in a script bundled to your extension. object browser (以及chrome )仅在捆绑到您的扩展程序的脚本中可用。 You need to use generic Web API to communicate between extension's script and webpage's script.您需要使用通用的 Web API 在扩展脚本和网页脚本之间进行通信。 See also: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#Communicating_with_the_web_page另请参阅: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#Communicating_with_the_web_page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM