chrome.browserAction.onClicked fires when I load a new page

573 Views Asked by At

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.

1

There are 1 best solutions below

2
Aefits On

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});
});