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.
At the moment when the user has zero tasks I end up with this:
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.
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.
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.
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