Firefox WebExtension:检查我的扩展是否存在

Firefox WebExtension: Check if MY extension exists

将扩展从 Chrome 移植到 FF

遵循本教程(在 Chrome 中运行良好):http://www.codingscripts.com/check-whether-user-has-a-chrome-extension-installed/

正在从网页向扩展程序发送消息: 在 (web)pagescript.js 中有:

function IsExist(extensionId,callback){
chrome.runtime.sendMessage(extensionId, { message: "installed" },
function (reply) {
   if (reply) {
      callback(true);
   }else{
      callback(false);
   }
});
}

IsExist("Your extension id",function(installed){
if(!installed){
   alert("Please install extension ");
}
});

在分机中接收来自网页的消息:

chrome.runtime.onMessageExternal.addListener(
function(req, sender, callback) {
if (req) {
if (req.message) {
   if (req.message == "installed") {
    callback(true);
   }
 }
}
return true;
});

我想要实现的目标

我网站上的几个 html 页面需要在未安装扩展程序时重定向回主页。因此,这些页面需要能够(自行)确定是否安装了扩展程序。

打开网页时出现错误

ReferenceError:chrome 未定义。 (我也试过 browser.runtime.onMessageExternal 但它抛出 "browser" 未定义)。 没有办法像 Chrome 中那样做吗?

感谢所有评论,这就是我想出的。 (我不得不选择 document_end(尽管评论建议 document_start)因为我在 content_script.js

中还有其他事情要做

在我的插件中 content_script.js

document.body.classList.add("plugininstalledblabla");

在我的插件中 manifest.json

 "content_scripts":
  [
    {
      "matches": ["*://*/*"],
      "all_frames": true,
      "js": ["content_script.js"],
      "run_at": "document_end"
    }
  ]

在我网站的 main.js 脚本中

$(window).on("load", function() { // !! Window onLoad cause : document_end -> DOM should be loaded here

    // Set
    $body = $('body');
    if(document.body.classList.contains("plugininstalledblabla")){
       console.log('addon is installed');
    } 
});