首页 > 解决方案 > 检测用户是否安装了 Chrome 扩展程序

问题描述

我尝试了这个问题的所有解决方案

检查用户是否安装了 Chrome 扩展程序

我做了什么:

1-我添加了后台 js 文件,并在清单中添加了以下内容

 "background": {
       "scripts": ["js/background.js"],
       "persistent": true
   },

   "permissions": [ "https://*.mydomain.com/*" ],

在 background.js 我添加

chrome.runtime.onMessageExternal.addListener(function(msg, sender, sendResponse) {
    if ((msg.action == "id") && (msg.value == id))
    {
        sendResponse({id : id});
    }
});

在我的网站上我添加了这段代码

<script>
    var id = "madbfblbpcoiddlankhkdbagjeemnlof";
    chrome.runtime.sendMessage(id, {action: "id", value : id}, function(response) {
        if(response && (response.id == id)) //extension installed
        {
            console.log(response);
        }
        else //extension not installed
        {
            console.log("Please consider installig extension");
        }

    });
</script>

我总是得到请考虑安装扩展

我该如何解决这个问题?

标签: javascriptgoogle-chromegoogle-chrome-extension

解决方案


id在你喜欢的开头定义你background.jsvar id = chrome.runtime.id;.

您还需要将您的网站添加到externally_connectable而不是permissionsmanifest.jsonas 中:

"externally_connectable": {
    "matches": ["https://*.mydomain.com/*"]
}

推荐阅读