谷歌 Chrome 扩展程序 chrome.browserAction.setIcon 不起作用

huangapple go评论108阅读模式
英文:

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(&quot;a&quot;);

// Loop through the links and look for a LinkedIn link
for (var i = 0; i &lt; links.length; i++) {
  var link = links[i];
  if (link.href.indexOf(&quot;linkedin.com&quot;) !== -1) {
    // Found a LinkedIn link, do something with it
    console.log(link.href);
    //change color of LinkedIn button
    chrome.browserAction.setIcon({
      path : &quot;present128.png&quot;,
    })
  }
}

manifest.json

{
  &quot;manifest_version&quot;: 3,
  &quot;name&quot;: &quot;LinkedIn Link Finder&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;description&quot;: &quot;Finds a LinkedIn link on the current webpage.&quot;,
  &quot;action&quot;: {
    &quot;default_icon&quot;: {              
      &quot;128&quot;: &quot;notpresent128.png&quot;    
    },
    &quot;default_title&quot;: &quot;Click Me&quot;,
    &quot;default_popup&quot;: &quot;popup.html&quot;
  },
  &quot;content_scripts&quot;: [
    {
      &quot;matches&quot;: [&quot;http://*/*&quot;, &quot;https://*/*&quot;],
      &quot;js&quot;: [&quot;content.js&quot;]
    }
  ]
}

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

{
  &quot;manifest_version&quot;: 3,
  &quot;name&quot;: &quot;LinkedIn Link Finder&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;action&quot;: {
    &quot;default_icon&quot;: {
      &quot;128&quot;: &quot;notpresent128.png&quot;
    }
  },
  &quot;content_scripts&quot;: [
    {
      &quot;matches&quot;: [
        &quot;&lt;all_urls&gt;&quot;
      ],
      &quot;js&quot;: [
        &quot;content.js&quot;
      ]
    }
  ],
  &quot;background&quot;: {
    &quot;service_worker&quot;: &quot;background.js&quot;
  }
}

contens.js

var links = document.getElementsByTagName(&quot;a&quot;);

for (var i = 0; i &lt; links.length; i++) {
  var link = links[i];
  if (link.href.indexOf(&quot;linkedin.com&quot;) !== -1) {
    console.log(link.href);
    chrome.runtime.sendMessage({});
  }
}

background.js

chrome.runtime.onMessage.addListener(() =&gt; {
  chrome.action.setIcon({
    path: &quot;present128.png&quot;,
  })
});

huangapple
  • 本文由 发表于 2023年5月13日 12:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定