Javascript 更改chrome扩展名';s图标在某些网站上
当当前选项卡的url中包含某些字符串时,我想更改我的chrome扩展图标。我是chrome扩展设计的新手,所以我先测试一些基本的消息发送功能。但是setIcon函数不起作用,并且扩展中没有显示错误消息。 这是我的清单文件:Javascript 更改chrome扩展名';s图标在某些网站上,javascript,google-chrome-extension,Javascript,Google Chrome Extension,当当前选项卡的url中包含某些字符串时,我想更改我的chrome扩展图标。我是chrome扩展设计的新手,所以我先测试一些基本的消息发送功能。但是setIcon函数不起作用,并且扩展中没有显示错误消息。 这是我的清单文件: {"permissions" ["identity","identity.email","tabs","activeTab","http://34.204.12.200:5000/"], "short_name": "React App", "name": "React Ex
{"permissions" ["identity","identity.email","tabs","activeTab","http://34.204.12.200:5000/"],
"short_name": "React App",
"name": "React Extension",
"manifest_version": 2,
"browser_action": {
"default_popup": "index.html",
"default_title": "React Ext",
"default_icon" : {
"512": "icon1.png",
"512": "icon2.png"
}
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["contentScript.js"]
}
],
"version": "1.0"
}
my background.js:
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.action === "updateIcon") {
if (msg.value===false) {
chrome.pageAction.setIcon({
tabId: sender.tab.id,
path: {"512":"icon2.png"}
});
//alert(msg received)
} else {
//tabId: sender.tab.id,
chrome.pageAction.setIcon({
tabId: sender.tab.id,
path: {"512":"icon1.png"}});
}
}
});
我的icon1和icon2都是512*512,它们与manifest.json位于同一目录中。
我的chrome扩展图标总是显示icon2,似乎我的background.js和contentScript都不工作。有人知道原因吗?请参阅:tabId是必需的属性。已修改但仍不工作。清单仅定义browserAction,您也可以使用它更改图标,请参阅chrome.browserAction.setIcon的文档。
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.action === "updateIcon") {
if (msg.value===false) {
chrome.pageAction.setIcon({
tabId: sender.tab.id,
path: {"512":"icon2.png"}
});
//alert(msg received)
} else {
//tabId: sender.tab.id,
chrome.pageAction.setIcon({
tabId: sender.tab.id,
path: {"512":"icon1.png"}});
}
}
});