检测是否从另一个分机安装了分机

时间:2012-08-16 15:23:16

标签: javascript google-chrome-extension

是否可以确定第二个分机是否存在分机?

首先,有点背景知识:我有一个覆盖书签管理器的扩展程序。客户还需要一位历史经理,我知道这不能在一个扩展中完成。

我的建议是创建两个扩展,其中一个也充当“核心”,使用客户端的API处理身份验证。我已经让两个人在一起谈话了,auth工作正常,但我正在努力应对未安装核心扩展的情况。我想显示安装核心的提示,如果不存在的话。

我可以发送消息并捕获chrome.extension.lastError,但有更优雅的方式吗?

同样的消息在我想象中也是一个福音......

1 个答案:

答案 0 :(得分:2)

使用Cross-extension messaging API将邮件从分机2发送到分机1.如果分机1没有响应,则可以提示用户安装分机1.我构建了两个Chrome扩展来测试这个想法,它工作得很好。

如果您想要下载并自行试用,请将以下文件压缩起来。以下是在YouTube http://youtu.be/6u4tIH6Xfcg

上显示工作示例的截屏视频

分机2(下属)

的manifest.json

{
    "name": "Subordinate Chrome Extension Example",
    "manifest_version": 2,
    "version": "0.1",
    "description": "This is an example extension for StackOverflow that requires a master/companion Google Chrome extension to be installed for it to work",
    "browser_action": {
        "default_popup": "popup.html"
    },
    "background": {
        "scripts": ["background.js"]
    }
}

background.js

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        switch (request.directive) {
        case "popup-click":
            // check to see if master extension 1 is installed
            chrome.extension.sendMessage('jfhngkelgcildagdkgenelgaaaphlghb', {directive: "ping"}, function(extensionResponse) {
                if (extensionResponse && extensionResponse.data == 'pong') {
                    console.log("The master extension 1 is installed!");
                } else {
                    console.log("The master extension 1 is not installed");
                }
                sendResponse({});
            });
            return true; // required to return true if we want to sendResponse() later since the cross chrome extension message passing is asynchronus
            break;
        default:
            // helps debug when request directive doesn't match
            alert("Unmatched request of '" + request + "' from script to background.js from " + sender);
        }
    }
);

popup.html

<html>
    <head>
        <script src="popup.js"></script>
        <style type="text/css" media="screen">
            body { min-width:250px; text-align: center; }
            #click-me { font-size: 20px; }
        </style>
    </head>
    <body>
        <button id='click-me'>Click Me!</button>
    </body>
</html>

popup.js

function clickHandler(e) {
    chrome.extension.sendMessage({directive: "popup-click"}, function(response) {
        this.close(); // close the popup when the background finishes processing request
    });
}

document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('click-me').addEventListener('click', clickHandler);
})

分机1(Master / Companion)

的manifest.json

{
    "name": "Master Chrome Extension Example",
    "manifest_version": 2,
    "version": "0.1",
    "description": "This is an example extension for StackOverflow that is required for a subordinate/companion Google Chrome extension to work",
    "background": {
        "scripts": ["background.js"]
    }
}

background.js

chrome.extension.onMessageExternal.addListener(function(request, sender, sendResponse) {
    if (sender.id == 'ikofjngppooeeendkfenaiedmlmfjmkb') { // restricting cross extension api to known extension
        if (request.directive == 'ping') {
            sendResponse({
                data: 'pong'
            });
        }
    }
});