[英]Browser tab query “Receiving end does not exist” which works on chrome
Background script 后台脚本
browser.runtime.onMessage.addListener(function (event) {
if( event.type === 'authenticate' ) {
browser.tabs.query({active: true}, function(tab) {
browser.tabs.sendMessage(tab[0].id, {
method: 'main',
auth0: "test"
}, function() {});
});
}
});
Content script 内容脚本
browser.runtime.sendMessage({
type: "authenticate"
});
browser.extension.onMessage.addListener(function(request, sender, sendResponse) {
if( request.method == 'main' ) {
login();
} else if( request.method == 'logout' ) {
logout();
}
sendResponse({});
});
The event goes through and the tab id is correct, but the debug info from firefox shows "Error: Could not establish connection. Receiving end does not exist." 事件进行了并且选项卡ID是正确的,但是来自firefox的调试信息显示“错误:无法建立连接。接收端不存在。”
On chrome the event callback goes through. 在chrome上,事件回调通过。 Anyone know what the issue is?
有人知道这个问题是什么吗? Looking through google I found something about the id not being correct but I'm not sure what the issue is here.
通过谷歌浏览,我发现有关ID的信息不正确,但是我不确定这里的问题是什么。
Using browser.runtime.onMessage
and changing 使用
browser.runtime.onMessage
并进行更改
browser.tabs.query({active: true}, function(tab) {
browser.tabs.sendMessage(tab[0].id, {
method: 'main',
auth0: "test"
}, function() {});
});
to 至
browser.tabs.query({active: true, currentWindow: true}, function(tab) {
chrome.tabs.sendMessage(tab[0].id, {
method: 'main',
auth0: "test"
});
});
fixed it for me. 为我修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.