【发布时间】:2017-11-15 23:26:35
【问题描述】:
我怎样才能使我的 chrome 扩展更改图标(不点击它)。我有脚本检查页面是否有特定的字符串,如果有,我希望我的扩展图标从灰色变为彩色。
【问题讨论】:
我怎样才能使我的 chrome 扩展更改图标(不点击它)。我有脚本检查页面是否有特定的字符串,如果有,我希望我的扩展图标从灰色变为彩色。
【问题讨论】:
内容脚本在设置图标时需要发送消息,例如
chrome.runtime.sendMessage({
action: 'updateIcon',
value: false
});
然后在后台脚本中:
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"});
}
}
});
【讨论】:
您可以在后台执行以下操作:
const updateIcon = tabId => {
const icon = isDisabled() ? icons.disabled : icons.enabled;
chrome.pageAction.setIcon({ tabId, path: icon });
};
chrome.tabs.onUpdated.addListener(updateIcon);
参考:https://github.com/gbleu/opteamissed/blob/master/background.js#L38
【讨论】: