将图像从后台页面传递给内容脚本javascript

passing an image from background page to content script javascript

本文关键字:脚本 javascript 图像 后台      更新时间:2023-09-26

有没有一种方法可以将chrome扩展的背景页中的图像发送到内容脚本,这样内容脚本就不会从外部网页中检索要显示的图像,而是使用与扩展一起本地存储的图像?

也许使用sendMessage API?

您不需要"传递"已在打包资源中的映像。

你只需要使用它:

  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"
    ],