Cam*_*ton 6 javascript browser google-chrome google-chrome-extension google-chrome-devtools
我正在制作chrome扩展,扩展有两种模式:always on(alwaysOn),或者只有当用户点击它时(onClick).我想根据用户的模式将图标更改为蓝色或红色,以便他们可以一目了然地看到它.但是,添加该chrome.browserAction.setIcon()行后,图标仍然不会在需要时更改.它只是保留在默认徽标上.
这是我的background.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){});
});
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
其他一切运行完美,console.log()不会显示任何错误.是否有任何理由javascript将"跳过"这些特定的代码行?("这些特定的代码行"是chrome.browserAction.setIcon({path: "blue-logo.png"});和chrome.browserAction.setIcon({path: "red-logo.png"});)有问题的图像与我的background.js,content_script.js等在同一个文件夹中.所以我不确定路径是否只是被错误读取或是什么.
编辑:我打开控制台,我收到消息"运行browserAction.setIcon时未检查runtime.lastError:图标无效." 这是什么意思?如果我指定从C:\ Users ...\blue-logo开始的完整路径"我收到错误消息无法找到.
Ela*_*lad 17
我不知道为什么,但是你不能使用带有大于190px宽度和高度的图标的setIcon函数.
我不知道的错误或功能.文档没告诉我们......
奇怪的是,您可以在manifest.json文件中使用相同的图标.
你的代码没问题!
为使其正常工作,图标尺寸应为 16x16、48x48 或 128x128 像素,如官方文档中所推荐:
https://developer.chrome.com/extensions/manifest/icons
| 归档时间: |
|
| 查看次数: |
2223 次 |
| 最近记录: |