Chrome扩展端口错误:无法建立连接。接收结束不存在 [英] Chrome Extention Port error: Could not establish connection. Receiving end does not exist

查看:3378
本文介绍了Chrome扩展端口错误:无法建立连接。接收结束不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从弹出窗口向我的内容发送消息时出现此错误。我想要做的是从我的content.js中获取当前选项卡的文档并将其发送到弹出窗口。

I am getting this error when trying to send a message from popup to my contentscript. What I am trying to do is get the document of the current tab from my content.js and send it to the popup. How can I fix this erro?

{
  "manifest_version": 2,
  "name": "Chrome Snapshot",
  "description": "Save images and screenshots of sites to Dropbox.",
  "version": "1.0",
  "permissions": [
    "<all_urls>",
    "tabs"
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "html/popup.html"
  },
  "background": {
    "scripts": [
      "vendor/dropbox.min.js",
      "vendor/jquery-2.0.2.min.js"
    ],
    "persistant": false
  },
  "content_scripts" : [{
    "all_frames": true,
    "matches" : ["*://*/*"],
    "js" : ["js/content.js"],
    "run_at": "document_end"
  }]
}

js / popup.js



js/popup.js

chrome.runtime.sendMessage({message: 'hi'}, function(response) {
  console.log(response);
});

js / content.js

js/content.js

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  console.log('message', message);
  sendResponse({farewell: 'goodbye'}); 
});

编辑#1 仍然收到相同的错误端口错误:无法建立连接。接收结束不存在。 miscellaneous_bindings:235
chromeHidden.Port.dispatchOnDisconnect

Edit #1 Still getting the same error Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:235 chromeHidden.Port.dispatchOnDisconnect

在清单中固定错误的'persistent'

已更新js /popup.js

fixed mispelling 'persistent' in manifest
updated js/popup.js

chrome.tabs.query({'active': true,'currentWindow':true}, function(tab){
    console.log('from tab', tab[0]);
    chrome.tabs.sendMessage(tab[0].id, {message: 'hi'}, function(response){
      console.log(JSON.stringify(response));
    });
  });


推荐答案

您需要使用 chrome.tabs.sendMessage 将消息发送到内容脚本。来自Chrome开发人员网站上的 chrome.runtime.sendMessage 规范:

You'd need to use chrome.tabs.sendMessage to send messages to content scripts. From the chrome.runtime.sendMessage specification on Chrome's developer site:


请注意,扩展程序无法使用此方法将消息发送到内容脚本。要将消息发送到内容脚本,请使用tabs.sendMessage。

Note that extensions cannot send messages to content scripts using this method. To send messages to content scripts, use tabs.sendMessage.

如果这不是您的理想选择,您可以让每个内容脚本为您的后台页面打开一个端口(可能需要持久化),然后让弹出页面向您的后台页面发送消息,后台页面将通过每个端口转发消息到所有内容脚本以告诉他们发送消息回到弹出页面。 (使用chrome.runtime.connect。)

If this isn't a good option for you, you could have each content script open a port to your background page (which would probably need to be persistent) and then have your popup page send a message to your background page, which would relay the message through each of the ports to all of the content scripts to tell them to send a message back to the popup page. (Use chrome.runtime.connect.)

另外,您在清单文件中拼错了持久性。我不希望你在发现问题之前半小时内通过代码挖掘。

Also, you've misspelled "persistent" in your manifest file. I don't want you to have to dig through code for half an hour before you find out that that's been causing problems.

这篇关于Chrome扩展端口错误:无法建立连接。接收结束不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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