6

I have an extension that implements a browser action. Of course, the browser action is allways visible, but it has a special significance in certain urls. So, I use filtered events to listen to those urls and set the proper badge

chrome.webNavigation.onDOMContentLoaded.addListener(
    function(tab){
        chrome.browserAction.setBadgeText({
            text:'bdge',
            tabId: tab
        });
    },
    {'url':[{hostSuffix: 'somedomain.com', pathPrefix: 'somePath/'}]}
);

Is there some "elegant" way to reset the badge when the user navigates out from that page, without listening every single tab navigation? Should I execute a content script to hang on some exiting event and send a message?

Thank you very much,

4

2 回答 2

4

在我看来,一个好的解决方案是使用chrome.tabs.onUpdated.

在您的背景页面中,您将拥有类似的内容:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    // using a regex or however else you want to test the URL
    if (/somedomain\.com\/somePath\//.test(changeInfo.url)) {
        chrome.browserAction.setBadgeText({
            text: 'bdge',
            tabId: tabId
        });
    } else {
        chrome.browserAction.setBadgeText({
            text: '',
            tabId: tabId
        });
    }
});

我知道你写了“没有听每一个标签导航”,但我不确定你为什么要避免这种情况。

于 2013-08-30T16:11:59.280 回答
3

这是文档没有告诉您的内容:Chrome 实际上会在用户离开时自动重置徽章。

当您将浏览器操作的标记仅设置到特定选项卡时,例如

chrome.browserAction.setBadgeText({
  text: 'ABCD', // My badge's text should be only 4 characters long
  tabId: 1234  // Any tab, ussually a var here, not a constant
});

仅当该选项卡是窗口中的活动选项卡时,Chrome 才会在浏览器操作按钮上显示标记。当用户在该选项卡中导航离开时,其文本将重置为 ''。无需特殊操作即可重置。

于 2013-09-14T11:47:19.950 回答