javascript - 检测一个扩展是否是从另一个扩展安装的

标签 javascript google-chrome-extension

是否可以从第二个扩展中找出一个扩展是否存在?

首先,一些背景知识:我有一个覆盖书签管理器的扩展。客户还想要一个历史管理器,我知道这不可能在一次扩展中完成。

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

我可以发送一条消息并捕获 chrome.extension.lastError,但是有没有更优雅的方式来做到这一点?

我想同步消息在这里也是一个福音....

最佳答案

使用 Cross-extension messaging用于从扩展 2 向扩展 1 发送消息的 API。如果扩展 1 没有响应,那么您可以提示用户安装扩展 1。我构建了两个 Chrome 扩展来测试这个想法并且效果很好。

如果您想下载并亲自试用,这里是压缩文件。这是一个截屏视频,显示了 YouTube 上的工作示例 http://youtu.be/6u4tIH6Xfcg

分机 2(下级)

list .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"]
    }
}

背景.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>

弹窗.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(主/同伴)

list .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"]
    }
}

背景.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'
            });
        }
    }
});

关于javascript - 检测一个扩展是否是从另一个扩展安装的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990511/

相关文章:

javascript - 如何从另一个模式打开 Bootstrap 模式中的滚动功能?

javascript - Jquery mobile在更改页面后无法加载样式

javascript - 获取 URL 来源和参数

javascript - 如何从 chrome API 函数为外部变量赋值?

google-chrome - 在 Google Chrome 扩展程序中使用 Stripe 信用卡表单——无法避免 "Payment not secure?"

javascript - 允许字符的 jquery select2 插件正则表达式?

javascript - Bootstrap Carousel 动画重叠

javascript - ScriptCalendar 的任何问题

google-chrome-extension - Chrome 扩展程序 : Multiple tabs in popup window

javascript - 在扩展程序中阅读 Chrome 浏览历史记录