如何删除浏览器操作徽章?

时间:2022-06-16 18:29:49

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.

我正在使用chrome.browserAction.setBadgeText为我的扩展程序的浏览器图标添加徽章,该图标显示用户的待办事项列表中未完成的任务的数量。

如何删除浏览器操作徽章?

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.

什么是理想的是像chrome.browserAction.clearBadge,我可以在任务计数为0时运行以删除徽章。

1 个解决方案

#1


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(task_count === 0)永远不会是真的,因为你使用的是===而不是==。任务计数是一个字符串,因此永远不会=== 0(一个数字)。您可以使用chrome调试器轻松找到此问题。一个断点,如果永远不会打,所以你会去嗯,看到它。

#1


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(task_count === 0)永远不会是真的,因为你使用的是===而不是==。任务计数是一个字符串,因此永远不会=== 0(一个数字)。您可以使用chrome调试器轻松找到此问题。一个断点,如果永远不会打,所以你会去嗯,看到它。