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

问题描述 投票:0回答:1

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

我跟着这个答案qazxsw poi这样做,通过qazxsw poi事件处理程序永远不会被解雇。

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); } }); } }); } }); // block outgoing requests for help widgets chrome.webRequest.onBeforeRequest.addListener( function(details) { //send message console.log('send message'); chrome.runtime.sendMessage({badgeText: "HELP"}); if (isDisabled) { return { cancel: false } // this should return from the function (details) level } else { return { cancel: true } } }, {urls: [ "a bunch of urls irrelevant to this question" ]}, ["blocking"]);

但是onMessage侦听器中的console.log语句都没有被执行。

javascript google-chrome-extension
1个回答
0
投票

解决了它,因为@wOxxOm说这是不可能的。

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

© www.soinside.com 2019 - 2024. All rights reserved.