在查看了有关 stackoverflow 的几个相关问题后,我提出了这个问题。我从如何检测是否安装了扩展开始。我选择了在某些页面上使用内容脚本将 div 添加到正文的方法。这就是我是如何做到的......
清单.json
{
"name": "Install Check",
"content_scripts": [
{
"matches": ["http://host.com/*"],
"js" : ["insert_node.js"]
}
],
"permissions": [
"tabs", "host.com/*"
]
}
insert_node.js(内容脚本)
var insert_node = document.createElement('div');
insert_node.id = "HOST_SITE";
document.body.appendChild(insert_node);
主机页面
<html>
<head>
</head>
<body>
<h1>This is a host site. Welcome!!!</h1>
<script src="jquery.js"></script>
<script src="notification.js"></script>
</body>
</html>
扩展安装脚本
$(document).ready(function() {
if ($('#HOST_SITE').length > 0) {
alert("you have our extension installed");
} else {
alert("not installed");
}
});
not_installed
我的问题是,在 chrome 可以在 DOM 中注入节点之前,总是会弹出带有消息的警报。我在这里阅读了manifest.json 中的run_at
属性。但这也没有解决问题。我尝试了所有三个, ,值。我在这里做错了什么?document_start
document_idle
document_end