javascript Chrome 扩展:内容脚本和 background.html 之间的通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11752341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:12:50  来源:igfitidea点击:

Chrome extension: Communication between content script and background.html

javascriptgoogle-chrome-extensioncontent-script

提问by Chandeep

I am new to Chrome extensions. I am trying to communicate between the content script and the background.htmlpage. The background.htmlsends 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.htmlcode is:

我是 Chrome 扩展程序的新手。我正在尝试在内容脚本和background.html页面之间进行通信。该background.html发送一个请求,“你好”,以内容脚本和内容脚本应该以“进行回应你好背景”警报。但它只是没有发生。我的background.html代码是:

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

content.jscode:

content.js代码:

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

popup.htmlcode:

popup.html代码:

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

manifest.json:

清单.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"
}

Please help!

请帮忙!

回答by Rob W

sendRequest/onRequestis replaced with sendMessage/onMessagein Chrome 20. *Messageis not just an alias for *Request, it's a different API.

sendRequest/onRequest替换sendMessage/onMessage在Chrome 20*Message不只是一个别名*Request,这是一个不同的API。

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

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



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()

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

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.

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