gpt4 book ai didi

javascript - chrome.browserAction.setIcon 没有做任何事情

转载 作者:数据小太阳 更新时间:2023-10-29 05:42:37 28 4
gpt4 key购买 nike

我正在制作一个 chrome 扩展程序,该扩展程序有两种模式:始终打开 (alwaysOn),或者仅当用户单击它时 (onClick)。我想根据用户的模式将图标更改为蓝色或红色,以便他们一眼就能看到。但是,在添加 chrome.browserAction.setIcon() 行之后,图标在需要时仍然没有改变。它只是保留在默认 Logo 上。

这是我的 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"开始的完整路径,我会收到错误消息找不到。

最佳答案

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

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

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

关于javascript - chrome.browserAction.setIcon 没有做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599144/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com