How to remove browser action icon?

I use chrome.browserAction.setBadgeText to add an icon to my extension browser icon that shows the number of tasks in the user's task list.

The badge when the user has tasks

At that moment, when the user has zero tasks, I get the following:

The badge when the user has zero tasks

However, I would prefer not to show the icon at all when the user has zero tasks.

Here is my code:

setBrowserActionBadge: function(allTasks) { var task_count; task_count = allTasks.filter(function(task) { task.isDone === false; }).length; task_count = task_count.toString(); if (task_count === 0) { task_count = ''; } chrome.browserAction.setBadgeText({ 'text': task_count }); chrome.browserAction.setBadgeBackgroundColor({ 'color': '#333333' }); }; 

This method runs every time tasks are checked or added, so it is updated in real time.

What would be ideal is something like chrome.browserAction.clearBadge , which I can run when the number of tasks is 0 to remove the icon.

+7
javascript google-chrome google-chrome-extension
source share
1 answer

You were close. You want to pass an empty string, but your if (task_count === 0) test will never be right because you use === instead of ==. The task counter is a string, so never === 0 (number).
You can easily find this problem using a chrome debugger. The breakpoint in this if would never hit, so you would go hmmmm and watch it.

+1
source share

All Articles