Angular - 如何检查是否安装了chrome扩展

时间:2014-09-08 18:54:32

标签: angularjs google-chrome google-chrome-extension

我需要一个外部Angular脚本才能检测用户是否安装了我的扩展程序。

例如:用户安装我的插件,然后转到我的脚本网站。该网站检测到我的扩展程序已安装并相应地更新页面。

我的代码:

$.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);
        }
    }
};

我收到以下错误:

  

HEAD chrome-extension://[[id_chrome_extension]]/images/logo.png net::ERR_FAILED jquery.js:86235   Uncaught Error: A Chrome Web Store installation is already pending.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

尝试Messaging api,这也适用于应用。

将此添加到您的清单:

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

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

// 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);
  });