chrome.browserAction.setIcon什么都没做



我正在制作一个chrome扩展,该扩展有两种模式:始终打开(alwaysOn)或仅当用户单击它时(onClick)。我想根据用户的模式将图标更改为蓝色或红色,这样他们就可以一目了然。然而,添加chrome.browserAction.setIcon()行后,图标仍然不会在需要时更改。它只是停留在默认的徽标上。

这是我的背景.js:

// Get the behavior of the plugin; the default is set to "onClick", the other option is "alwaysOn"
chrome.storage.sync.get({
extensionBehavior: 'onClick'
}, function(items) {
if(items.extensionBehavior == 'onClick'){
chrome.browserAction.setIcon({path: "blue-logo.png"});
chrome.browserAction.onClicked.addListener(function() {
// When the extension icon is clicked, send a message to the content script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {"message": tabs[0].url}, function(response){});
});
});
}
else {
chrome.browserAction.setIcon({path: "red-logo.png"});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {          
if (changeInfo.status == 'complete') {
// When the HTML loads, send a message to the content script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {"message": tabs[0].url}, function(response){});
});
}
});
}
});

其他一切都运行得很好,console.log()没有显示任何错误。javascript"跳过"这些特定的代码行有什么原因吗?("这些特定的代码行"是chrome.browserAction.setIcon({path: "blue-logo.png"});chrome.browserAction.setIcon({path: "red-logo.png"});)有问题的图像与我的background.js、content_script.js等在同一个文件夹中,所以我不确定路径是不是读错了。

编辑:我打开控制台,收到消息"Unchecked runtime.lastError which run browserAction.setIcon:Icon invalid."这是什么意思?如果我指定从C:\Users开始的完整路径。。。\蓝色徽标"我收到错误消息找不到。

我不知道为什么,但不能将setIcon函数用于宽度和高度大于190px的图标。

Bug或功能我不知道。文档没有告诉我们…

奇怪的是,您可以在manifest.json文件中使用相同的图标。

您的代码很好!

要使其工作,图标尺寸应为16x16、48x48或128x128像素,如官方文件所建议:

https://developer.chrome.com/extensions/manifest/icons

最新更新