【问题标题】:How to access a Chrome Extension's icon from a content script如何从内容脚本访问 Chrome 扩展程序的图标
【发布时间】:2019-03-28 06:06:16
【问题描述】:

在我的 Chrome 扩展程序的内容脚本中,我正在尝试修改网站的 favicon 以(暂时)将其设置为我的扩展程序的图标。但是,我无法找出从内容脚本中引用扩展程序图标的正确方法。我试过了:

    favicon.href ='/images/icon-38.png';
    console.log("set href of favicon to " +favicon.href);

但 favicon.href 的值最终与我所在的任何网站相关,例如:set href of favicon to https://twitter.com/images/icon-38.png

来自我的 manifest.json:

"icons": {
  "16": "images/icon-16.png",
  "38": "images/icon-38.png"
},

在我的后台脚本中,我当然可以使用它们的相对路径来引用我的图标...但是如何从内容脚本中做到这一点?

【问题讨论】:

标签: google-chrome-extension


【解决方案1】:

这应该适合你

chrome.extension.getURL('images/icon-38.png')

【讨论】:

  • 要使这个答案完整:该文件还必须在 web_accessible_resources 下的 manifest.json 中声明
  • chrome.extension.getURL 已弃用,正如@Katie 所指出的,您还需要指定web_accessible_resources。见我的answer
【解决方案2】:

您需要将图标指定为web_accessible_resources:

"icons": {
  "16": "images/icon-16.png",
  "38": "images/icon-38.png"
},
"web_accessible_resources": [
  "images/icon-16.png",
  "images/icon-38.png"
],

然后使用chrome.runtime.getURLchrome.extension.getURL 自 Chrome 58 起已弃用)

chrome.runtime.getURL('images/icon-16.png');
chrome.runtime.getURL('images/icon-38.png');

【讨论】:

    猜你喜欢
    • 2011-04-25
    • 2012-07-04
    • 1970-01-01
    • 2016-11-19
    • 2013-05-20
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多