【问题标题】:chrome.browserAction.setIcon not doing anythingchrome.browserAction.setIcon 没有做任何事情
【发布时间】: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


    【解决方案1】:

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

    我不知道的错误或功能。文档没有告诉我们...

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

    【讨论】:

    【解决方案2】:

    你的代码没问题!

    要使其正常工作,图标尺寸应为 16x16、48x48 或 128x128 像素,如官方文档中建议的那样:

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

    【讨论】:

    • 这不能回答问题
    • 他的代码很好。由于它的尺寸,该图标没有被复制。请检查您是否投反对票。
    • OK 仔细看看。不过,您的回答中没有什么新内容,因为@wodka 已经说过了。干杯。
    • 这也是正确的 - 我只是遇到了同样的问题,并在输入 48 x 48px 后使其工作。这可能对其他人有所帮助 - 并节省他们的时间。
    猜你喜欢
    • 2012-09-07
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多