将图像从背景页面传递到内容脚本 javascript

passing an image from background page to content script javascript

有没有办法从 chrome 扩展程序的背景页面发送图像到内容脚本,这样内容脚本就不会从外部网页检索图像进行显示,但在本地使用与扩展一起存储的图像?

也许使用 sendMessage API?

您不需要 "pass" 打包资源中已有的图像。

你只需要使用它:

  1. 使用chrome.runtime.getURL()函数,可以在内容脚本中使用:

    // Use relative path in the package directory
    img.src = chrome.runtime.getURL("images/image.png")
    
  2. 将清单中 "web_accessible_resources" 中的图像列入白名单:

    "web_accessible_resources": [
      "images/*.png"
    ],