45

I want to know how to insert an image in a Chrome extension.

<img id="image" src="logo.png" />

I'm inserting that html tag correctly into a website, but naturally can't load that logo.png image.

Any ideas on how to modify manifest.json?

Nathan
  • 1,080
  • 7
  • 16
jacktrades
  • 7,224
  • 13
  • 56
  • 83

3 Answers3

112

There are two possible causes for the problem.

  1. You're injecting an image with src="logo.png". The inserted image element becomes a part of the page, so the browser does not try to load the image from the extension.
    To fix this problem, use chrome.extension.getURL('logo.png'); to get the absolute URL of the resource.

  2. "manifest_version": 2 is enabled in the manifest file. That disables all resources for external use, by default. When this error occurs, the following message appears in the console:

Not allowed to load local resource: chrome://gbmfhbpbiibnbbgjcoankapcmcgdkkno/logo.png
To solve the problem, add the resource to a whitelist, namely [`"web_accessible_resources"`][3] in the manifest file:
      ...,
      "web_accessible_resources": ["logo.png"]
    }

UPDATE: chrome.extension.getURL('logo.png')

Deprecated since Chrome 58. Please use runtime.getURL.

Gangula
  • 5,193
  • 4
  • 30
  • 59
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 2
    Thanks. I keep getting the Not allowed... error. Still checking if I'm doing something wrong. – jacktrades Aug 04 '12 at 13:30
  • @jacktrades After whitelisting the resource, have you correctly reloaded the extension? Are you using the full path relative to the extension's root? Eg. `images/logo.png` when the image is located in a subdirectory. – Rob W Aug 04 '12 at 13:33
  • 1
    Yes, I have correctly reloaded. logo.png is located in the root directory, not subdirectory. – jacktrades Aug 04 '12 at 13:34
  • If it's still not working, please publish the relevant part of your extension's source code (if you don't want it to be public, mail it to me, contact details are at my profile). – Rob W Aug 04 '12 at 13:37
  • you should have checked ur e-mail earlier :) – jacktrades Aug 04 '12 at 13:39
  • 8
    @jacktrades You've placed `web_accessible_resources` **inside `"content_scripts"`. That's wrong, it has to be a key of the root object. I attempted to make that clear in my answer, by adding an unindented trailing `}`. – Rob W Aug 04 '12 at 13:46
  • Is it possible to use this directly in the `` tag without the need of JS? – fguillen Jun 22 '21 at 11:56
  • Any idea on how to make it work with safari-web-extension. It doesnt load citing security error – Arjun Nair Jul 15 '21 at 18:18
3

The only practical solution I have found is:

my.html

<img id="icon" src="./icon.png">

content.js

let icon = document.getElementById("icon");
icon.src = chrome.runtime.getURL("icon.png");

manifest.js

"web_accessible_resources": [{
    "matches": ["<all_urls>"],
    "resources": ["icon.png"]
}]
fguillen
  • 36,125
  • 23
  • 149
  • 210
0

This solution works for me.

  • Create a folder named "images" in the extension directory.
  • Kepp all the image files in that directory.
  • Add "web_accessible_resources": ["logo.png"] in the manifest.json file
  • Access it by using src='chromeextension://[ID]/images/logo.png' from content.js