我正在制作一个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开始的完整路径“,我会收到错误消息找不到。
发布于 2016-06-03 05:29:11
我不知道为什么,但你不能使用setIcon函数的图标大于190px的宽度和高度。
Bug或功能我不知道。文档不会告诉我们...
奇怪的是,您可以在manifest.json文件中使用相同的图标。
https://stackoverflow.com/questions/37599144
复制相似问题