【发布时间】:2016-10-02 14:54:02
【问题描述】:
我正在制作一个 chrome 扩展,该扩展有两种模式:始终打开 (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){});
});
}
});
}
});
其他一切运行完美,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" 开始的完整路径,则会收到错误消息找不到。
【问题讨论】:
标签: javascript google-chrome browser google-chrome-extension google-chrome-devtools