Chrome 扩展:内容脚本和 background.html 之间的通信 [英] Chrome extension: Communication between content script and background.html

查看:22
本文介绍了Chrome 扩展:内容脚本和 background.html 之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Chrome 扩展程序的新手.我正在尝试在内容脚本和 background.html 页面之间进行通信.background.html 向内容脚本发送请求hello",内容脚本应以hello background"警报响应.但它只是没有发生.我的 background.html 代码是:

I am new to Chrome extensions. I am trying to communicate between the content script and the background.html page. The background.html sends a request, "hello", to the content script and the content script should respond back with "hello background" alert. But it's just not happening. My background.html code is:

function testRequest() {        
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {greeting: "hello"});
    });    
}

content.js 代码:

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.greeting == "hello")
        alert("hello background");
    }
);

popup.html 代码:

<!doctype html>
<html>
    <head></head>
    <body>
        <form>
            <input type="button" value="sendMessage" onclick="testRequest()" />
        </form>    
    </body>
</html>

manifest.json:

{
    "browser_action": {
        "default_icon": "icon.png",
        "popup": "popup.html"
    },
    "background": {
        "page": "background.html"
    },
    "permissions": [
        "tabs",
        "http://*/*",
        "https://*/*",
        "notifications",
        "contextMenus"
    ],
    "content_scripts": [
        {
            "matches": ["http://*/*","https://*/*"],
            "js": ["content.js"]
        }
    ],
    "name": "FirstExtension",
    "version": "1.0"
}

请帮忙!

推荐答案

sendRequest/onRequest 替换为 sendMessage/onMessage 在 Chrome 20 中.*Message 不仅仅是 *Request 的别名,它是一个不同的 API.

sendRequest/onRequest is replaced with sendMessage/onMessage in Chrome 20. *Message is not just an alias for *Request, it's a different API.

如果你想支持 Chrome <20(很多 Ubuntu 用户还在 Chromium 18,因为 PPA 没有更新),使用 onRequestsendRequest.否则,使用 *Message 方法.

If you want to support Chrome <20 (many Ubuntu users are still at Chromium 18, because the PPA is not updated), use onRequest and sendRequest. Otherwise, use the *Message methods.

还有一个问题是你的函数位于后台页面,调用是在弹窗中进行的.这些是不同的范围,如果您想从弹出窗口调用后台页面方法,请使用 chrome.extension.getBackgroundPage():

Another problem is that your function is located in the background page, and the call is made in the pop-up. These are different scopes, if you want to call a background page method from the popup, use chrome.extension.getBackgroundPage():

chrome.extension.getBackgroundPage().testRequest();

最后一点:您使用的是清单版本 1 和内联事件处理程序.此做法已弃用,有关详细信息,请参阅 http://code.google.com/chrome/extensions/manifestVersion.html.

Final note: You're using manifest version 1 and inline event handlers. This practice is deprecated, for more information, see http://code.google.com/chrome/extensions/manifestVersion.html.

这篇关于Chrome 扩展:内容脚本和 background.html 之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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