角 - 如何检查是否安装了Chrome扩展程序 [英] Angular - How check if chrome extension is installed

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

问题描述

我需要一个外部的角度脚本能够如果用户已经安装了我的扩展来检测。

I need an external Angular script to be able to detect if a user has my extension installed.

例如:用户安装我的插件,然后去与我的剧本是一个网站。网站检测我的安装扩展并相应地更新页

For example: A user installs my plugin, then goes to a website with my script on it. The website detects that my extension is installed and updates the page accordingly.

我的code:

$.detectChromeExtension('[[id_chrome_extension]]', 'images/logo.png', myCallbackFunction);

$.detectChromeExtension = function(extensionId, accesibleResource, callback) {
    if (typeof(chrome) !== 'undefined'){
        var testUrl = 'chrome-extension://' +extensionId +'/' +accesibleResource;
        console.log(testUrl);
        $.ajax({
            url: testUrl,
            timeout: 1000,
            type: 'HEAD',
            success: function(){
                if (typeof(callback) === 'function') {
                    callback.call(this, true);
                }
            },
            error: function() {                
                if (typeof(callback) === 'function') {
                    callback.call(this, false);
                }
            }
        });
    }
    else {
        if (typeof(callback) === 'function') {
            callback.call(this, false);
        }
    }
};

我收到以下错误:

I receive the following error:

HEAD铬扩展:// [id_chrome_extension] /images/logo.png网:: ERR_FAILED的jquery.js:86235
  未捕获的错误:Chrome网上应用店安装已暂停

我怎样才能解决这个问题?

How can I resolve this?

推荐答案

尝试的信息 API,它也适用于应用程序。

Try the Messaging api, which also works for apps.

添加到您的清单:

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

然后将消息发送给您的分机从页面:

Then send a message to your extension from the page:

// The ID of the extension we want to talk to.
var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";

// Make a simple request:
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
  function(response) {
    if (!response.success)
      handleError(url);
  });

和倾听你的推广信息:

    chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (sender.url == blacklistedWebsite)
      return;  // don't allow this web page access
    if (request.openUrlInEditor)
      openUrl(request.openUrlInEditor);
  });

这篇关于角 - 如何检查是否安装了Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆