为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

四季花海 2021-04-12 16:19:20
我正在尝试为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-chromechrome.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语句均未执行。
查看完整描述

1 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

这是不可能的。

尽管webRequest在详细信息字典中传递了tabId。这可以用来复制它。


查看完整回答
反对 回复 2021-04-22
  • 1 回答
  • 0 关注
  • 971 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信