My extension gets data using tab.url and puts it in chrome.browserAction.setBadgeText. When i open a new tab it resets. How can i update BadgeText only for a new tab? and keep it unchanged for an old one?
extension layout:
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});
};
});
Description. Use browser actions to put icons in the main Google Chrome toolbar, to the right of the address bar. In addition to its icon, a browser action can have a tooltip, a badge, and a popup.
A browser action is a button that your extension adds to the browser's toolbar. The button has an icon, and may optionally have a popup whose content is specified using HTML, CSS, and JavaScript.
Two key points should help you with your troubles.
1) chrome.browserAction.setBadgeText has an optional parameter, tabId, that binds the value to the tab.
2) You should filter chrome.tabs.onUpdated events by changeInfo's fields.
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 might not catch new tabs' creation; if it doesn't, also listen to onCreated
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With