How to display a image in content script by Chrome extension?

22,451 views
Skip to first unread message

Amanda

unread,
Jan 18, 2011, 3:18:14 AM1/18/11
to Chromium-extensions
I'm working on a extenstion to display a 16x16 image above the
original web page.

In my manifest.json, I specified the image file.
"icons": {
"16": "images/menu.png",
....}

And I use content script to create a div and set its backgroud image
by CSS.
Like this:

background:url(images/prompt_bg.jpg);

But there is no image show on the division.

Could someone tell me what's wrong? Many thanks!




Amanda

unread,
Jan 18, 2011, 3:21:54 AM1/18/11
to Chromium-extensions
CSS setting is:
background:url(images/menu.png);

But it does not work.


PhistucK

unread,
Jan 18, 2011, 3:24:47 AM1/18/11
to Amanda, Chromium-extensions
Content scripts run within the scope of the web page (only in a 'different world', so global variables and events are not shared).
Use chrome.extension.getURL("images/menu.png") to generate the URL you need through JavaScript.

Within a CSS file (somefile.css), unfortunately, you will have to use the full URL with an hard coded ID -
chrome-extension://EXTENSION_ID/images/menu,png


PhistucK







--
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.


The Awesome One

unread,
Jan 18, 2011, 10:53:55 AM1/18/11
to Chromium-extensions
I say, use chrome.extension.getURL() and set the CSS properties
through JavaScript.

On Jan 18, 1:24 pm, PhistucK <phist...@gmail.com> wrote:
> Content scripts run within the scope of the web page (only in a 'different
> world', so global variables and events are not shared).
> Use chrome.extension.getURL("images/menu.png") to generate the URL you need
> through JavaScript.
>
> Within a CSS file (somefile.css), unfortunately, you will have to use the
> full URL with an hard coded ID -
> chrome-extension://EXTENSION_ID/images/menu,png
>
> ☆*PhistucK*
>
>
>
>
>
>
>
> On Tue, Jan 18, 2011 at 10:18, Amanda <mengdong_...@hotmail.com> wrote:
> > I'm working on a extenstion to display a 16x16 image above the
> > original web page.
>
> > In my manifest.json, I specified the image file.
> >  "icons": {
> >      "16": "images/menu.png",
> >      ....}
>
> > And I use content script to create a div and set its backgroud image
> > by CSS.
> > Like this:
>
> > background:url(images/prompt_bg.jpg);
>
> > But there is no image show on the division.
>
> > Could someone tell me what's wrong? Many thanks!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .

Amanda

unread,
Jan 21, 2011, 2:21:17 AM1/21/11
to Chromium-extensions
Thanks a lot! It works!

myImage = document.createElement('img');
...
iconUrl = chrome.extension.getURL("images/menu.png");
myImage.src = iconUrl;
Reply all
Reply to author
Forward
0 new messages