Chrome扩展动态更改图标(无需点击) [英] chrome extension dynamically change icon (without clicking)

查看:446
本文介绍了Chrome扩展动态更改图标(无需点击)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使我的Chrome扩展更改图标(无需点击它)。我有脚本检查页面是否有特定的字符串,如果有,我希望我的扩展名图标从灰色变为彩色。

  chrome.runtime.sendMessage({
操作:'updateIcon',
值:false
});

然后在后台脚本中:

 chrome.runtime.onMessage.addListener(函数(msg,sender,sendResponse){
if(msg.action ===updateIcon){
if(msg .value){
chrome.browserAction.setIcon({path:/assets/tick.png});
} else {
chrome.browserAction.setIcon({path:/ assets / cross.png});
}
}
});


how can I make my chrome extension change icon (without clicking on it). I've got script that's checking if page has certain string and if it has I want my extension icon change from grey to colored one.

解决方案

The content script will need to send a message when it wants to set the icon e.g.

chrome.runtime.sendMessage({
    action: 'updateIcon',
    value: false
});

Then in the background script:

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    if (msg.action === "updateIcon") {
        if (msg.value) {
            chrome.browserAction.setIcon({path: "/assets/tick.png"});
        } else {
            chrome.browserAction.setIcon({path: "/assets/cross.png"});
        }
    }
});

这篇关于Chrome扩展动态更改图标(无需点击)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆