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

时间:2017-06-16 15:11:07

标签: google-chrome-extension firefox-webextensions

将扩展程序从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但是它抛出“浏览器”没有定义)。 有没有办法像Chrome中可以做到的那样?

1 个答案:

答案 0 :(得分:1)

感谢所有评论,这就是我想出来的。 (我不得不去找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');
    } 
});
相关问题