StaticanimateStart animating the badge with the given characters or predefined character set
Either an array of characters to cycle through or a key from the predefined charsets
Delay between updates in milliseconds (default: 500)
Error If chars is empty or invalid
static animate(chars: string[] | keyof typeof BadgeAnimator.charsets, delay: number = 500): void {
const characterSet = typeof chars === "string" ? this.charsets[chars] : chars;
if (!characterSet || characterSet.length < 1) {
throw new Error("At least one character is required for badge animation");
}
this.clear(); // Clear any existing animation
this.#chars = characterSet;
this.#delay = delay;
this.#charIndex = 0;
this.#updateAnimation();
}
StaticclearStop the badge animation and optionally show a final message
Optional text to display before clearing the badge
How long to show the final text before clearing (in milliseconds)
static clear(finalText: string = "", duration: number = 5000): void {
if (this.#timeoutId) {
clearTimeout(this.#timeoutId);
this.#timeoutId = null;
}
// If there's a final status to set, create a timeout to clear it afterwards
if (finalText) {
chrome.action.setBadgeText({ text: finalText }, () => {
setTimeout(() => {
chrome.action.setBadgeText({ text: "" });
}, duration);
});
} else {
chrome.action.setBadgeText({ text: "" });
}
}
StaticsetSet the colors of the badge
OptionalfgColor: stringThe foreground (text) color of the badge (hex color code)
OptionalbgColor: stringThe background color of the badge (hex color code)
static setColor(fgColor?: string, bgColor?: string): void {
if (fgColor) {
chrome.action.setBadgeTextColor({
color: fgColor,
});
}
if (bgColor) {
chrome.action.setBadgeBackgroundColor({
color: bgColor,
});
}
}
StaticsetSet the text of the badge. This also clears the animation
The text to display on the badge
static setText(text: string): void {
this.clear();
chrome.action.setBadgeText({ text });
}
Private Static#updateUpdate the badge to the next character in the sequence
static #updateAnimation(): void {
if (!this.#chars.length) return;
chrome.action.setBadgeText(
{
text: this.#chars[this.#charIndex],
},
() => {
this.#charIndex = (this.#charIndex + 1) % this.#chars.length;
this.#timeoutId = setTimeout(() => this.#updateAnimation(), this.#delay);
},
);
}
A utility class for managing Chrome extension badge animations and styling. Provides methods to animate the badge with different character sets, set colors, and control the animation timing. The badge can be used to show loading states, progress indicators, or other status information in the Chrome extension icon.
Example
Source