When adding a listener for various events (e.g.
webNavigation.onCommitted), it's possible to supply an filter that prevents the event from being dispatched if it doesn't meet the filter criteria. This can improve performance by avoiding the overhead of dispatching events not of interest to the extension.
Are there any types of filters available other than UrlFilter? In particular, it would be very useful to have a TabFilter that enables filtering events based on the tab to which the event belongs.
By way of example, consider the Chrome team's "
Suspicious Site Reporter" extension, which aims to swap the Browser Action's icon shown in Chrome's toolbar based on the top-level URL loaded in the tab.
The extension has a background page which monitors chrome.tabs.onActivated and
chrome.tabs.onUpdated to ensure that as the user switches tabs or navigates, the icon properly reflects data from the current page.
The problem is that chrome.tabs.onUpdated fires constantly for every tab in the browser, even those that aren't active. The current code:
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (tab.active && (changeInfo.url || changeInfo.status)) {
setBrowserActionAndIcon(tab);
}
});
...checks if the tab is active, and if it's not, it bails out immediately. The problem is that by doing this bailout in the background page (instead of as a filter), the background page must first be activated, and that is especially expensive for this extension, which fetches parses hundreds of kilobytes of JSON.
If addListener filtering could support other types of filters, to wit:
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
setBrowserActionAndIcon(tab);
}, {tab: {active:true}, changes: {["url", "status"]}} );
...the filtering step could be moved out and Chrome could avoid waking the background page for events we don't care about.
Is there some other way to achieve this today?