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

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

本文介绍了Chrome扩展程序:如何使用内容脚本检测是否安装了扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看了一些关于stackoverflow的相关问题后问了这个问题。我开始使用。我选择了在某些页面上使用内容脚本将正文添加到正文的方法。这是我是如何做到的......



manifest.json

  {
name:Install Check,
content_scripts:[
{
matches:[http://host.com/*] ,
js:[insert_node.js]
}
],
permissions:[
tabs,host.com/* b




insert_node.js(内容脚本)

  var insert_node = document.createElement('div'); 
insert_node.id =HOST_SITE;
document.body.appendChild(insert_node);

主页

 < HTML> 
< head>
< / head>
< body>
< h1>这是一个主机网站。欢迎!< / H1>
< script src =jquery.js>< / script>
< script src =notification.js>< / script>
< / body>
< / html>

扩展程序安装脚本

  $(document).ready(function(){
if($('#HOST_SITE')。length> 0){
alert(您有我们的扩展程序 );
} else {
alert(not installed);
}
});

我的问题是消息 not_installed 在Chrome可以在DOM中注入节点之前总会弹出。我阅读了的 run_at 属性。但是这也没有解决问题。我尝试了所有三个 document_start document_idle document_end 值。我在这里做错了什么?

解决方案

它看起来像你自己的扩展和网站,在这种情况下,它会很多更易于使用。

  if(typeof chrome!==undefined&& typeof chrome.app!==undefined&&& chrome.app.isInstalled){ 
//扩展程序已安装。
}


I am asking this question after looking at several related questions on stackoverflow. I started with how to detect if an extension is installed. I opted for the method where I add a div to body using content scripts on some pages. Here is how I did it...

manifest.json

{
    "name": "Install Check",
    "content_scripts": [
        {
            "matches": ["http://host.com/*"],
            "js" : ["insert_node.js"]
        }
    ],
    "permissions": [
        "tabs", "host.com/*"
    ]
}

insert_node.js (content script)

var insert_node = document.createElement('div');
insert_node.id = "HOST_SITE";
document.body.appendChild(insert_node);

host page

<html>
<head>
</head>
<body>
    <h1>This is a host site. Welcome!!!</h1>
    <script src="jquery.js"></script>
    <script src="notification.js"></script>
</body>
</html>

extension install script

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

My problem is that the alert with message not_installed always pops up before the chrome can inject the node in DOM. I read about run_at attribute in manifest.json over here. But that didn't solve the problem either. I tried all the three document_start, document_idle, document_end values. What am I doing wrong over here?

解决方案

It looks like your own the extension and website, in which case it would be much easier to use Inline Installation.

if (typeof chrome !== "undefined" && typeof chrome.app !== "undefined" && chrome.app.isInstalled) {
  // extension is installed.
}

这篇关于Chrome扩展程序:如何使用内容脚本检测是否安装了扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:43