我正在尝试为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语句均未执行。
素胚勾勒不出你
相关分类