javascript - 检测用户是否安装了 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>
我总是得到请考虑安装扩展
我该如何解决这个问题?
解决方案
id
在你喜欢的开头定义你background.js
:var id = chrome.runtime.id;
.
您还需要将您的网站添加到externally_connectable
而不是permissions
在manifest.json
as 中:
"externally_connectable": {
"matches": ["https://*.mydomain.com/*"]
}
推荐阅读
- angular - @types/lodash/common/array.d.ts(483,22): 错误 TS1005: ';'
- swift - 创建一个 if 语句,其中一次只有一个按钮可以有边框
- javascript - 我想知道这个问题是否有其他解决方案。JAVASCRIPT
- javascript - JavasSript(无 jQuery)查找多个类并向每个类添加新类
- vba - 循环列表和运行报告
- javascript - 动态地将随机数值添加到 HTML wp-block(WordPress 编辑器)中的内联脚本标签集
- networking - kubernetes ingress - 将 neo4j 端点暴露给内部网络
- android - AppCompat 库还需要吗?
- buildfire - Buildfire:允许用户上传照片的最佳方式
- python - 有没有办法让文本框自行保存?