消息传递不起作用



bacground.js

chrome.tabs.create({url: "http://www.google.com", "active":true}, function(tab) {
console.log(tab.id);// 315
chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});

contentscript.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });
});

日志:

Port: Could not establish connection. Receiving end does not exist. 

如何修复?

发生此错误的原因似乎是后台脚本发送消息时,内容脚本尚未注入页面。因此,"接收端不存在。"

我假设(因为我没有超过50名代表能够对你的问题发表评论并首先澄清这一点,所以如果我错了,请纠正我),在你的manifest.json文件中,你以以下方式指定它:

"content_scripts": [{
    "matches": ["*://xyz.com/*"],
    "js": ["contentscript.js"]
}]

如果这确实是注入内容脚本的方式,那么您需要知道内容脚本只有在DOM完成渲染之后才会被注入。(在以下链接上搜索"run_at":http://developer.chrome.com/extensions/content_scripts.html)这意味着,当您从后台脚本发送该消息时,内容脚本仍在"加载"

好消息是,您可以通过向manifest.json文件中的content_scripts参数添加第三个键值对来指定何时加载内容脚本,如下所示:

"content_scripts": [{
    "matches": ["*://xyz.com/*"],
    "js": ["contentscript.js"],
    "run_at": "document_start"
}]

这告诉扩展,您希望在构造DOM或运行任何其他脚本之前(即尽早)注入contentscript.js。

如果上面的技术仍然给你同样的错误,那就表明即使是document_start也不够早。在这种情况下,让我们完全考虑另一种方法。您目前要做的是让后台脚本连接到内容脚本。当内容脚本成功注入页面时,为什么不将其连接到后台脚本?后台页面始终运行,因此保证能够从内容脚本接收消息,而不会抱怨"接收端不存在"

在background.js:中

chrome.runtime.onConnect.addListener(function(port) {
    console.log("background: received connection request from 
                 content script on port " + port);
    port.onMessage.addListener(function(msg) {
        console.log("background: received message '" + msg.action + "'");
        switch (msg.action) {
            case 'init':
                console.log("background script received init request 
                             from content script");
                port.postMessage({action: msg.action});
                break;
        }
    });
});

在contentscript.js:中

var port_to_bg = chrome.runtime.connect({name: "content_to_bg"});
port_to_bg.postMessage({action: 'init'});
port_to_bg.onMessage.addListener(function(msg) {
    switch (msg.action) {
        case 'init':
            console.log("connection established with background page!");
            break;
    }
}

请随时提出更多问题进行澄清!我很想知道第一种方法是否有效。如果没有,第二种方法是肯定的胜利。

相关内容

最新更新