Using packaged images in a content-script

1,456 views
Skip to first unread message

Daniel Kantor

unread,
Oct 10, 2010, 5:16:58 PM10/10/10
to Chromium-extensions
I'd like to use images that I've included within my packaged extension inside a content-script. In CSS if I reference these images with relative paths - 

images/foo.png

then the path is relative to the document it is inserted in and not my extension. I think hardcoding these to my extension URL is a bad idea and would make testing very difficult. Does anyone have any ideas on how this can be made easier? Right now I am referencing images on my server, but this means I have duplicate images in my extension and the server and you can notice a small lag in load times as they are not local. 

thanks,
Dan

str...@yahoo.com

unread,
Oct 10, 2010, 5:21:00 PM10/10/10
to Chromium-extensions
have you tried:

chrome.extension.getURL("images/foo.png"); ?

just use that if you want to use an image within your extension

Mohamed Mansour

unread,
Oct 11, 2010, 2:15:44 AM10/11/10
to Daniel Kantor, Chromium-extensions
Hey Daniel,

You would need to set the images via JavaScript (content script) not from your CSS while using chrome.extension.getURL('/img/logo.png'). I was hoping there was a better way doing this within content scripts. But I couldn't figure out how :)

-
Mohamed Mansour


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

PhistucK

unread,
Oct 11, 2010, 3:02:58 AM10/11/10
to Daniel Kantor, Chromium-extensions
If you set them with JavaScript, you have to use chrome.extension.getURL(images/foo.png).
If you set them with CSS (meaning, somefile.css), you can use -

body {
  background-image:url('chrome-extension://__MSG_@@extension_id__/images/foo.png');
}

See this page for more information.

PhistucK



--

Daniel Kantor

unread,
Oct 11, 2010, 11:13:34 AM10/11/10
to PhistucK, Chromium-extensions
I tried putting in __MSG_@@extension_id__ but that did not work. I am not supposed to substitute in my ID correct?

PhistucK

unread,
Oct 11, 2010, 11:27:06 AM10/11/10
to Daniel Kantor, Chromium-extensions
Not entirely sure what you mean.

PhistucK

Daniel Kantor

unread,
Oct 11, 2010, 11:28:42 AM10/11/10
to PhistucK, Chromium-extensions
It worked within CSS that is on a packaged html page but not within CSS that was inserted using chrome.tabs.insertCSS

Daniel Wagner-Hall

unread,
Oct 11, 2010, 1:49:54 PM10/11/10
to Daniel Kantor, PhistucK, Chromium-extensions

Mohamed Mansour

unread,
Oct 11, 2010, 1:58:35 PM10/11/10
to Daniel Wagner-Hall, Daniel Kantor, PhistucK, Chromium-extensions
That solves my mystery, thanks for those bug report links :)  In the meantime, you can set the images via JavaScript, that is what I did for an overlay HTML I did last night (was using injectCSS and executeScript)

-
Mohamed Mansour

Daniel Kantor

unread,
Oct 11, 2010, 5:24:40 PM10/11/10
to Mohamed Mansour, Daniel Wagner-Hall, PhistucK, Chromium-extensions
Wow those 2 bugs are from April and May. Any chance they will be fixed?
Reply all
Reply to author
Forward
0 new messages