Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a browser action badge?

I'm using chrome.browserAction.setBadgeText to add a badge to my extension's browser icon that shows the number of uncompleted tasks in the user's todo list.

The badge when the user has tasks

At the moment when the user has zero tasks I end up with this:

The badge when the user has zero tasks

However what I'd prefer is to not show the badge at all when the user has zero tasks.

Here's 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 is run each time tasks are checked off or added, so it updates in real time.

What would be ideal is something like chrome.browserAction.clearBadge which I can run when the task count is 0 to remove the badge.

like image 311
Benjamin Humphrey Avatar asked May 09 '15 03:05

Benjamin Humphrey


People also ask

What is browser action icon?

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.


1 Answers

You were close. You do want to pass an empty string however your test if (task_count === 0) will never be true because you are using === instead of ==. Task count is a string thus never === 0 (a number).
you can easily find this issue by using the chrome debugger. A breakpoint in that if would never hit so you would go hmmmm and see it.

like image 136
Zig Mandel Avatar answered Sep 28 '22 05:09

Zig Mandel