Reputation: 183
I am developing a chrome extension that shows notifications.
I am showing the notifications using these functions chrome.browserAction.setBadgeBackgroundColor
and chrome.browserAction.setBadgeText
.
This is what it looks like:
After the user have seen the notifications I want to remove this badge.
I have tried to make it transparent but this was the result:
Can anyone help me how to remove the badge, or if there is another way that makes what I want?
Solution
When writing an empty text, the badge goes away.
Upvotes: 8
Views: 4814
Reputation: 11
If you stumble upon this (in 2022) use action instead of browserAction:
chrome.action.setBadgeText({text: ""});
as seen in the API documentation.
If you are also wondering where you should place the function call to trigger the badge text going away when opening the popup, just put in your popup.js (or however your content script is named).
If you want to trigger some extra logic in your background script when removing the badge text you could send a message from your popup.js via
chrome.runtime.sendMessage("clearBadge", (response) => console.log("Badge
cleared"));
and in your backgroundworker.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if(message === "clearBadge"){
chrome.action.setBadgeText({text: ""});
//do something else
}
});
Upvotes: 0
Reputation: 1165
Be aware if you set badge for especially tab to unset the text for this one:
chrome.tab.query({currentWindow: true, active: true}, function(tabs) {
var tab = tabs[0];
chrome.browserAction.setBadgeText({
text: ''
tabId: theTabId
});
});
Upvotes: 1
Reputation: 3011
To remove the badge count, simply set the text as an empty string:
chrome.browserAction.setBadgeText({
'text': '' //an empty string displays nothing!
});
Upvotes: 11