英文:
Google Chrome Extension chrome.browserAction.setIcon not working
问题
以下是您要翻译的内容:
我希望图标从一张图片变成另一张图片(基本上根据页面上是否存在链接来改变颜色)。
content.js
// 获取页面上的所有链接
var links = document.getElementsByTagName("a");
// 循环遍历链接并查找 LinkedIn 链接
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.href.indexOf("linkedin.com") !== -1) {
// 找到 LinkedIn 链接,进行相关操作
console.log(link.href);
// 更改 LinkedIn 按钮的颜色
chrome.browserAction.setIcon({
path: "present128.png",
});
}
}
manifest.json
{
"manifest_version": 3,
"name": "LinkedIn Link Finder",
"version": "1.0",
"description": "在当前网页上查找 LinkedIn 链接。",
"action": {
"default_icon": {
"128": "notpresent128.png"
},
"default_title": "点击我",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
]
}
我在这里缺少什么?
英文:
I want the icon to change from one image to another (basically have the color change depending on the presence of a link on the page).
content.js
// Get all the links on the page
var links = document.getElementsByTagName("a");
// Loop through the links and look for a LinkedIn link
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.href.indexOf("linkedin.com") !== -1) {
// Found a LinkedIn link, do something with it
console.log(link.href);
//change color of LinkedIn button
chrome.browserAction.setIcon({
path : "present128.png",
})
}
}
manifest.json
{
"manifest_version": 3,
"name": "LinkedIn Link Finder",
"version": "1.0",
"description": "Finds a LinkedIn link on the current webpage.",
"action": {
"default_icon": {
"128": "notpresent128.png"
},
"default_title": "Click Me",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
]
}
What am I missing here?
Looked into the docs for different solutions. Tried manifest V2 - tried switching images so can get the default image to start off as a different image, but cant get it to switch.
答案1
得分: 0
manifest.json
{
"manifest_version": 3,
"name": "LinkedIn Link Finder",
"version": "1.0",
"action": {
"default_icon": {
"128": "notpresent128.png"
}
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
],
"background": {
"service_worker": "background.js"
}
}
contens.js
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.href.indexOf("linkedin.com") !== -1) {
console.log(link.href);
chrome.runtime.sendMessage({});
}
}
background.js
chrome.runtime.onMessage.addListener(() => {
chrome.action.setIcon({
path: "present128.png",
})
});
英文:
Please try this.
manifest.json
{
"manifest_version": 3,
"name": "LinkedIn Link Finder",
"version": "1.0",
"action": {
"default_icon": {
"128": "notpresent128.png"
}
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
],
"background": {
"service_worker": "background.js"
}
}
contens.js
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.href.indexOf("linkedin.com") !== -1) {
console.log(link.href);
chrome.runtime.sendMessage({});
}
}
background.js
chrome.runtime.onMessage.addListener(() => {
chrome.action.setIcon({
path: "present128.png",
})
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论