Chrome扩展程序:如何使用内容脚本检测是否安装了扩展程序

vik*_*tra 2 google-chrome inject google-chrome-extension content-script

在查看stackoverflow上的几个相关问题后,我问这个问题.我从如何检测是否安装了扩展开始.我选择了在某些页面上使用内容脚本向主体添加div的方法.我是这样做的......

的manifest.json

{
    "name": "Install Check",
    "content_scripts": [
        {
            "matches": ["http://host.com/*"],
            "js" : ["insert_node.js"]
        }
    ],
    "permissions": [
        "tabs", "host.com/*"
    ]
}
Run Code Online (Sandbox Code Playgroud)

insert_node.js(内容脚本)

var insert_node = document.createElement('div');
insert_node.id = "HOST_SITE";
document.body.appendChild(insert_node);
Run Code Online (Sandbox Code Playgroud)

主页

<html>
<head>
</head>
<body>
    <h1>This is a host site. Welcome!!!</h1>
    <script src="jquery.js"></script>
    <script src="notification.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

扩展安装脚本

$(document).ready(function() {
    if ($('#HOST_SITE').length > 0) {
        alert("you have our extension installed");
    } else {
        alert("not installed");
    }
});
Run Code Online (Sandbox Code Playgroud)

我的问题是,在not_installedchrome可以在DOM中注入节点之前,始终会弹出带消息的警报.我在这里读了manifest.json中的run_at属性.但这也没有解决问题.我尝试了所有三,,值.我在这里做错了什么?document_startdocument_idledocument_end

abr*_*ham 7

它看起来像您自己的扩展和网站,在这种情况下,使用内联安装会更容易.

if (typeof chrome !== "undefined" && typeof chrome.app !== "undefined" && chrome.app.isInstalled) {
  // extension is installed.
}
Run Code Online (Sandbox Code Playgroud)

  • 据我所知chrome.app.isInstalled指的是apps而不是扩展(我的测试证实了这一点) (7认同)