【问题标题】:Port error: Could not establish connection. Receiving end does not exist端口错误:无法建立连接。接收端不存在
【发布时间】:2013-02-28 06:51:42
【问题描述】:

我一直在谷歌搜索,试图解决这个问题,但似乎找不到解决方案。我正在尝试在我的 Chrome 扩展程序中设置侦听器和发送者的简单任务。

我的清单

{
  "manifest_version": 2,

  "name": "my app",
  "description": "text",
  "version": "0.1",
  "background":{
    "scripts":["background.js"]
  },

  "content_scripts": [
    {
      // http://developer.chrome.com/extensions/match_patterns.html
      "matches": ["http://myurl.com/*"],
      "js": ["jquery-1.9.1.min.js", "myapp.js"],
      "all_frames": true
    }
  ], 
  "browser_action": {
    "default_icon": "/icons/icon-mini.png",
    "default_popup": "popup.html"
  }
}

在我的后台JS

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

在我的 popup.js 中(由 coffeescript 渲染,请原谅那种奇怪的语法)

(function() {

  $(function() {});

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

}).call(this);

在我的 myapp.js 中

chrome.extension.sendMessage({
      greeting: "hello"
    }, function(response) {
      return console.log(response.farewell);
    });

我关注了the tutorial。不知道为什么这不起作用。我对 JS 很满意,但不清楚为什么这会表现得很奇怪。任何帮助将不胜感激!

【问题讨论】:

  • 你的代码的结果是什么?有什么错误吗?
  • 这篇文章的标题有错误

标签: javascript google-chrome google-chrome-extension coffeescript google-chrome-devtools


【解决方案1】:

这段代码有不止一个问题,所以让我分解一下。

据我所见,您正试图从您的内容脚本向您的弹出窗口发送一条消息,并且有一个背景页面没有做任何事情。

问题 #1

popup.js 中的代码,除了奇怪的复杂之外,不是背景页面。它仅在popup 打开时运行,因此无法收听消息。

问题 #2

后台页面中的代码正在使用已弃用的getSelected 方法向内容脚本发送消息。内容脚本没有监听器。

这两件事的结果是这样的:

Background page -> content script (no listener)
Content Script -> extension pages (no listener)

我建议让您的背景页面成为您交流的中心。如果您需要在弹出窗口和内容脚本之间进行通信,请使用popup -> content script 并使用sendResponse() 进行回复。

编辑:这是您想要的消息传递示例。只需替换为您的变量即可。

内容脚本

...
//get all of your info ready here

chrome.extension.onMessage.addListener(function(message,sender,sendResponse){
  //this will fire when asked for info by the popup
  sendResponse(arrayWithAllTheInfoInIt);
});

弹出窗口

...
chrome.tabs.query({'active': true,'currentWindow':true},function(tab){
  //Be aware 'tab' is an array of tabs even though it only has 1 tab in it
  chrome.tabs.sendMessage(tab[0].id,"stuff", function(response){
    //response will be the arrayWithAllTheInfoInIt that we sent back
    //you can do whatever you want with it here
    //I will just output it in console
    console.log(JSON.stringify(response));
  });
});

【讨论】:

  • 感谢 BeardFist。我的内容脚本中有一个数组,我想在弹出窗口中显示。这就是我最终想要做的。对此有何建议?
  • @ZackShapiro 我建议使用弹出窗口中的chrome.tabs.sendMessage 来启动与内容脚本的连接。在您的内容脚本中添加chrome.extension.onMessage.addListener。然后使用sendResponse(stuff) 将数组发送回弹出窗口。如果你愿意,我可以写一个例子。
  • 感谢@BeardFist!一个例子会很棒!
  • @ZackShapiro 我在答案中添加了一个简短的示例。我没有包含背景页面,但您可以执行类似的 popup -> background 消息传递设置。
  • 非常感谢,@BeardFist!
【解决方案2】:

我在后台页面中遇到了类似的问题,我的解决方案是确保标签已完成加载,然后再尝试向其发送消息。

如果选项卡尚未完全加载,内容脚本将不会启动,也不会等待消息。

这里有一些代码:

 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
   if (changeInfo.status === 'complete') {
     // can send message to this tab now as it has finished loading
   }
 }

所以如果你想向活动标签发送消息,你可以先确保它已经完成加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多