Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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 chrome.runtime.onMessage.addListener未在创建的选项卡中注册_Javascript_Google Chrome Extension - Fatal编程技术网

Javascript chrome.runtime.onMessage.addListener未在创建的选项卡中注册

Javascript chrome.runtime.onMessage.addListener未在创建的选项卡中注册,javascript,google-chrome-extension,Javascript,Google Chrome Extension,下面的脚本是在我创建一个新选项卡时从my background.js注入的。新选项卡加载一个站点,即Google.com $(function(){ //this fires when tab is created and script injected chrome.runtime.sendMessage({title: window.document.title}, function(){}); //this fires when clicking on element in c

下面的脚本是在我创建一个新选项卡时从my background.js注入的。新选项卡加载一个站点,即Google.com

$(function(){
  //this fires when tab is created and script injected
  chrome.runtime.sendMessage({title: window.document.title}, function(){});
  //this fires when clicking on element in created tab
  document.getElementById("Username").addEventListener("click", function(){
    chrome.runtime.sendMessage({someMessageFromLiveSite: 'Clicked in live site!'});
  });

  chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) {
    //this never fires
    chrome.runtime.sendMessage({title: 'This is the created tab responding to a msg'}, function(){});
    //code continues...
如上所述,chrome.runtime.sendMessage和vanilla JS事件监听器一样工作正常,但chrome.runtime.onMessage监听器似乎没有添加/没有响应从扩展背景的其他部分或其他创建页面发送的消息。

感谢,我意识到我试图使用chrome.runtime.sendMessage从后台向我创建的所有选项卡广播一条消息,但如前所述,这不会广播,因此chrome.tabs.sendMessageTAB\u ID。。。。是必需的,以便使侦听器在“我创建的内容”选项卡上响应


虽然标题在技术上对我遇到的问题不准确,但在我看来它是正确的症状,因此此答案可能对其他人仍有帮助。

如何从其他PAR发送消息?chrome.runtime.sendMessage无法运行。啊。。。我只是在使用chrome.runtime.sendMessage{someMessageFromMothership:message};在背景和其他地方。我刚刚看到了,我将尝试下一个可能的副本,我不认为这是一个副本,因为我肯定可以将消息从我的内容脚本发送到我的背景,但不能创建侦听器。此标题仍然适用于特定问题。我将尝试使用连接和端口来解决这一问题,以便在两者之间进行长时间的消息传递,如果成功,我将在此处发布答案。请仔细阅读我对该问题的回答。这不是你不能做的监听器,而是你需要不同的函数来发送内容脚本。