Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript sendResponse工作时chrome.tabs.sendMessage不工作 以下是chrome扩展的清单: chrome扩展的背景页: 以下是调用扩展名的网站代码:_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript sendResponse工作时chrome.tabs.sendMessage不工作 以下是chrome扩展的清单: chrome扩展的背景页: 以下是调用扩展名的网站代码:

Javascript sendResponse工作时chrome.tabs.sendMessage不工作 以下是chrome扩展的清单: chrome扩展的背景页: 以下是调用扩展名的网站代码:,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,响应已正确发送,但我收到chrome.native.lastError.message: 每个chrome.runtime.onMessage不能发送一次以上的响应 每个文档的侦听器(消息由URL的扩展发送 未定义) 但当我这么做的时候: port.onMessage.addListener(function(msg) {sendResponse(msg);}); port.onMessage.addListener( onNativeMessage ); port.onMessage.ad

响应已正确发送,但我收到chrome.native.lastError.message:

每个chrome.runtime.onMessage不能发送一次以上的响应 每个文档的侦听器(消息由URL的扩展发送 未定义)

但当我这么做的时候:

port.onMessage.addListener(function(msg) {sendResponse(msg);});
port.onMessage.addListener( onNativeMessage );
port.onMessage.addListener(function(msg) {sendResponse(msg);});
port.onMessage.addListener( onNativeMessage );
这个调用onNativeMessage的方法使用chrome.tabs.sendMessage方法将消息发送回网站(据称是?)。但该网站从未得到回应

此外,我的扩展需要实例化一个端口,以便它可以使用与我的本机主机应用程序的长期连接

我做错什么了吗

当我这样做时:

port.onMessage.addListener(function(msg) {sendResponse(msg);});
port.onMessage.addListener( onNativeMessage );
port.onMessage.addListener(function(msg) {sendResponse(msg);});
port.onMessage.addListener( onNativeMessage );
响应已正确发送,但我收到一条
chrome.runtime.lastError.message

对于每个文档,每个
chrome.runtime.onMessage
listener不能发送一次以上的响应(消息是通过URL未定义的扩展发送的)

这是因为每次收到外部消息时都要添加一个侦听器。因此,在收到第二条消息后,
sendResponse()
尝试运行两次(新消息和旧消息)


但当我这么做的时候:

port.onMessage.addListener(function(msg) {sendResponse(msg);});
port.onMessage.addListener( onNativeMessage );
port.onMessage.addListener(function(msg) {sendResponse(msg);});
port.onMessage.addListener( onNativeMessage );
此方法调用
onNativeMessage
使用方法
chrome.tabs.sendMessage
将消息发送回网站(据称?)。但该网站从未得到回应

你不能那样做
chrome.tabs.sendMessage
将消息发送到,而不是网页


您应该修改代码,只添加一次侦听器,或者至少确保一次只有一个侦听器

当然,诀窍在于
sendResponse
引用每次都会更改。我想你可以通过一个自动注销的侦听器来完成:

if(!port) {
    port = chrome.runtime.connectNative(hostName);

    port.onDisconnect.addListener(function() {
      console.log("Disconnected from native App");
      port = null;
    });
}

var callback = function(msg) {
  sendResponse(msg);
  port.onMessage.removeListener(callback); // The power of closures!
};

port.onMessage.addListener(callback);

这是一个扩展,不是一个应用程序。相应地修改问题。非常感谢您快速而详尽的回答。亲爱的@Xan,我有一个扩展(作为我的背景)可以与外部页面(作为我的内容脚本)通信。如何在内容脚本端终止侦听?我正在使用port.onMessage.removeListener(此),然而,它不工作@Aqjn提出更详细的新问题,不要试图在评论中提出后续问题。