猿问

chrome.runtime.onMessage侦听器永远不会触发

我正在尝试为Chrome中的每个标签设置特定的徽章文本。


我一直遵循这个答案https://stackoverflow.com/a/32168534/8126260来执行此操作,尽管chrome.runtime.onMessage事件处理程序从未触发过。


// tab specific badges https://stackoverflow.com/questions/32168449/how-can-i-get-different-badge-value-for-every-tab-on-chrome

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {

  console.log('runtime message');

  if (message.badgeText) {

    console.log('runtime message with badge text');

      chrome.tabs.get(sender.tab.id, function(tab) {

          if (chrome.runtime.lastError) {

              return; // the prerendered tab has been nuked, happens in omnibox search

          }

          if (tab.index >= 0) { // tab is visible

              chrome.browserAction.setBadgeText({tabId:tab.id, text:message.badgeText});

              console.log('set message');

          } else { // prerendered tab, invisible yet, happens quite rarely

              var tabId = sender.tab.id, text = message.badgeText;

              chrome.webNavigation.onCommitted.addListener(function update(details) {

                  if (details.tabId == tabId) {

                      chrome.browserAction.setBadgeText({tabId: tabId, text: text});

                      chrome.webNavigation.onCommitted.removeListener(update);

                  }

              });

          }

      });

  }

});


(完整的源代码在https://github.com/bcye/Hello-Goodbye上)


查看我的后台脚本的控制台,会出现发送消息,这意味着chrome.runtime.sendMessage({badgeText: "HELP"});应该已经执行了。


尽管onMessage侦听器中的console.log语句均未执行。


四季花海
浏览 972回答 1
1回答

素胚勾勒不出你

这是不可能的。尽管webRequest在详细信息字典中传递了tabId。这可以用来复制它。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答