I need that if I focus on another tab (not the one where the extension worked) there was no text above the icon. We return the focus - the number (text) that was. That is, to make Badge Text work only there.

  • can you reformulate? Or paint in more detail. I honestly do not quite understand what you want. - UserName
  • On one tab, we found something, the number has changed, for example, by 3 - click on the other, previously opened, there this triple remains. A must be 0, respectively, on this other. - Timur Musharapov

1 answer 1

On one tab, we found something, the number has changed

Since nothing else is said, a check for the equality of the tab URL specific value was used as a condition.


We use the chrome.tabs.onActivated event, which is triggered when the active tab is chrome.tabs.onUpdated , and the chrome.tabs.onUpdated event, which is triggered by the update.

In addition, you need a method chrome.tabs.get , which extracts information about a tab by its identifier.


Example of use:

 function changeBadgeText(tabId, tabUrl) { // Если условие выполняется, установим значение равное 3 иначе 0 var value = (tabUrl == "http://ru.stackoverflow.com/") ? "3" : "0"; // Устанавливаем значение бейджу. Идентификатор указаывается в качестве ограничителя. // Т.е для вкладок подходящих под условие значение 3, а для всех остальных 0 chrome.browserAction.setBadgeText({text: value, tabId: tabId}); } function changeBadgeTextOnActivated(tabInfo) { chrome.tabs.get(tabInfo.tabId, function (tab) { // Если поле url имеет значение, т.е вкладка загружена. // В ином случае данные будут получены из метода changeBadgeTextOnUpdate if (tab.url) changeBadgeText(tab.id, tab.url); }); } function changeBadgeTextOnUpdate(tabId, changeInfo, tab) { // Если страница загруженна. Допустим мы не переключали вкладки, а обновили открытую if (changeInfo.status == "complete") changeBadgeText(tabId, tab.url); } chrome.tabs.onActivated.addListener(changeBadgeTextOnActivated); chrome.tabs.onUpdated.addListener(changeBadgeTextOnUpdate); 

Pay attention to the chrome.tabs.onActivated event chrome.tabs.onActivated :

Note that the URL is not allowed.


chrome.tabs.get | chrome.tabs.onActivated | chrome.tabs.onUpdated

  • Thank you, almost what you need. The only negative - it works only after switching to another tab. That is, switching, returning - and the value applies. - Timur Musharapov
  • one
    @TimurMusharapov, it’s difficult to talk without specific conditions. Based on the above, I prepared a response. I understand that the problem with setting values ​​when switching, ie, some initial is already worth it. I do not know by what condition you set the values. And about the update, I added - that in the case of the newly opened tabs or updated, you need to use chrome.tabs.onUpdated . I will add more about this - UserName
  • @TimurMusharapov, updated. Now it works even if the tabs did not switch, for example when updating or opening a tab. Now everything is as it should? - UserName
  • Yes, thank you - what you need! - Timur Musharapov