0

I am developing a very simple extension for Google Chrome which sets a badge text when the user presses the browser action icon. Here is the background.js:

chrome.browserAction.onClicked.addListener(function() {
        chrome.browserAction.setBadgeText({text: "Ko"});});

When I load the extension for the first time in chrome://extensions there's no problem and works properly, but if I close and open the browser and then I go to a webpage the Badge text appears automatically even when I have not pressed the browser action icon as you can see in the image:

enter image description here

This is my manifest.json:

{
"name": "Hello Extensions",
"description": "Base level extension",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
    "default_icon": "check-circle-green-512.png"
},

"background": {
    "scripts":["background.js"]
  },

  "permissions": ["storage", "alarms", "notifications"]

}

Thanks for the help and greetings.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Danubio
  • 93
  • 8
  • 1
    Obviously the browser preserves the previously set text. – wOxxOm Mar 22 '18 at 20:28
  • Have you tried to reset the badge text on window close event ? i.e. chrome.windows.onRemoved – Moti Korets Mar 23 '18 at 20:37
  • Thanks for the answer, but in the end elegant-user was right and the problem was solved using the tab.id. That is to say: chrome.browserAction.setBadgeText({text: "Ex", tabId: tab.id}); However I think that your solution would work too. – Danubio Mar 28 '18 at 15:02

1 Answers1

0

The browserAction button is common for all opened tabs and windows. When you set/change the badge text using setBadgeText this effect displayed on all tabs in all chrome windows. If you want a separate badge text for each window you will need to manage it at your own. See a simple example below that changes the badge text on tab onActivated event:

chrome.tabs.onActivated.addListener(function(activeInfo) {
    console.log(activeInfo.tabId);
    chrome.browserAction.setBadgeText({text: "T"+activeInfo.tabId});
});
Aefits
  • 3,399
  • 6
  • 28
  • 46
  • Hi, thanks for the answer. Yes, I know, but according to the API the browserAction.onClicked fires when a browser action icon is clicked. The problem is that the event is activated even when the icon is not clicked and it is fired when I go to a website. – Danubio Mar 23 '18 at 08:34
  • The `chrome.browserAction.onClicked` event is firing automatically is just unbelievable. – Aefits Mar 23 '18 at 10:16