是否可以根据徽章文本更改徽章颜色?

问题描述 投票:0回答:1

我是否可以根据文本更改徽章颜色?例如:如果我的扩展程序阻止20件事,则可能为黄色,如果阻止50件事,则可能为红色。

javascript google-chrome-extension firefox-addon firefox-webextensions
1个回答
1
投票

是,您可以根据其中的值更改标志的颜色。由于您正在设置标志的值,因此您已经拥有该值,并且可以动态确定要使用的颜色。

之后,您可以使用browserAction.setBadgeBackgroundColor设置颜色。

// First, define badge colors with the minimum threshold.  
// For the sake of simplicity, keep the minimums in order.
const badge_colors = [
    {min: 0, color:  [255, 255, 0, 255] },
    {min: 11, color:  [255, 255, 0, 255] },
    {min: 200, color:  [255, 255, 0, 255] }
];

const badge_counter = 34; // This should already be avaliable, but can also be fetched using browserAction.getBadgeText()


const new_badge_color = badge_colors.reverse() // Reverse the list so the min is in descending order
                                .find(color=>color.min < badge_counter) // Find the first minimum value that is less than the badge counter.

// new_badge_color is now 
// {min: 11, color:  [255, 255, 0, 255] }

一旦有了新颜色,就可以使用setBadgeBackgroundColor设置颜色

© www.soinside.com 2019 - 2024. All rights reserved.