How can I save chrome.browserAction.setBadgeText for each tab?

My extension receives data using tab.url and puts it in chrome.browserAction.setBadgeText. When I open a new tab, it is reset. How can I update BadgeText only for a new tab? and keep it unchanged for the old?

expansion:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ function(tabId, changeInfo, tab){ //using tab.url and XMLHttpRequest() i get newText for: chrome.browserAction.setBadgeText({text: newText}); }; }); 
+6
source share
2 answers

Two key points should help you with your problems.

1) chrome.browserAction.setBadgeText has an optional tabId parameter that binds the value to the tab.

2) You must filter the chrome.tabs.onUpdated changeInfo fields events.

So change your code to:

 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ function(tabId, changeInfo, tab){ if(!changeInfo.url) return; // URL did not change // Might be better to analyze the URL to exclude things like anchor changes /* ... */ chrome.browserAction.setBadgeText({text: newText, tabId: tab.id}); }; }); 

This may not cause the creation of new tabs; if not, also listen onCreated

+7
source
  chrome.browserAction.setBadgeText({text: newText}, tab.id); //<<this is not working to me chrome.browserAction.setBadgeText({text: "Phish", tabId: tab.id}); //<<This is working to me 
+2
source

Source: https://habr.com/ru/post/969441/


All Articles