Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist

02-27 1677阅读 0评论

通过以下代码向当前页面发送 “start” 消息:

chrome.tabs.query({active: true,currentWindow: true}, tabs => {
    let tab = tabs[0];
    chrome.tabs.sendMessage(tab.id, "start");
});

报错:

Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.

可能的原因:

接收端, 也就是说目标页面必须有 chrome.runtime.onMessage 监听消息, 如果 “content-script” 没有注入到页面中, 那么这个页面就无法接收消息

Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist,Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,设置,插件,第1张
(图片来源网络,侵删)

如果你的插件刚刚加载, 并且在一个已经加载完毕的页面中使用它, 则会出这个问题.

因为这个页面已经加载完了, 它并没有被注入脚本, 你需要刷新页面, 使脚本注入到页面中, 然后才可以发送消息

平台不允许文章内容太少, 下面是水

sendMessage

chrome.tabs.sendMessage(integer tabId, any message, function responseCallback)

向指定标签页中的内容脚本发送一个消息,当发回响应时执行一个可选的回调函数。当前扩展程序在指定标签页中的每一个内容脚本都会收到 runtime.onMessage 事件。

参数类型
tabIdinteger
messageany
responseCallbackoptional function

如果您指定了 responseCallback 参数,它应该指定一个如下形式的函数:

Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist,Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,设置,插件,第2张
(图片来源网络,侵删)

function(any response) {…};

response ( any )

请求处理程序发出的 JSON 响应对象。如果连接到指定标签页的过程中发生错误,将不传递参数调用回调函数,并将 runtime.lastError 设置为错误消息。

Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist,Google Chrome 插件开发: 无法建立连接, 接收端不存在. Could not establish connection. Receiving end does not exist,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,设置,插件,第3张
(图片来源网络,侵删)

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1677人围观)

还没有评论,来说两句吧...

目录[+]