简体   繁体   English

适用于Chrome的浏览器标签查询“接收端不存在”

[英]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.

相关问题 Chrome 扩展程序“接收端不存在”。 错误 - Chrome Extension "Receiving end does not exist." Error browser.tabs.sendMessage():错误:接收端不存在 - browser.tabs.sendMessage(): Error: Receiving end does not exist chrome.tabs.sendMessage 显示错误无法建立连接。 接收端不存在 - chrome.tabs.sendMessage showing error Could not establish connection. Receiving end does not exist Google Chrome扩展程序:端口:无法建立连接。 接收端不存在 - Google Chrome Extension : Port: Could not establish connection. Receiving end does not exist Chrome扩展程序:端口错误:无法建立连接。 接收端不存在。 - Chrome Extension: Port error: Could not establish connection. Receiving end does not exist. 未选中 runtime.lastError: 无法建立连接。 接收端不存在。 Chrome 扩展程序 - Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. Chrome Extension Chrome扩展程序-“端口错误:无法建立连接。 接收端不存在” - Chrome extension - “Port error: Could not establish connection. Receiving end does not exist” Chrome扩展程序端口错误:无法建立连接。 接收端不存在 - Chrome Extention Port error: Could not establish connection. Receiving end does not exist Chrome 扩展开发:不断获取 Unchecked runtime.lastError:无法建立连接。 接收端不存在 - Chrome Extension development: keep getting Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist 未经检查的 runtime.lastError:无法建立连接。 接收端不存在。 使用我自己的 Chrome 扩展程序 - Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. With my own Chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM