Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome Extensions - Can't load local images with CSS

I have a simple Chrome extension that uses the content script feature to modify a website. More specifically, the background-image of said website.

For some reason I can't seem to be able to use local images, even though they are packed in the extension.

body {     background: #000 url('image.jpg') !important;     background-repeat: repeat !important; } 

That's it, the simplest CSS... but it won't work. The browser doesn't load the image.

like image 263
Bjarki Jonasson Avatar asked Aug 24 '10 18:08

Bjarki Jonasson


People also ask

Why are images not loading in Chrome HTML?

Chrome could've stopped loading images because of corrupted files in its data folder. The easiest fix is to rename the current data folder so that Chrome will create a new one. If you're using Windows 10, Press Windows key + R to bring up a Run dialog. Type %appdata% and press Enter.

How do I force an image in Chrome?

Here's how it works: open Chrome, go to a website that contains a video and play it fullscreen. From there, press the Home button to go to your Android Home Screen and the playing video will automatically transition to Picture-in-Picture. That's all!


2 Answers

Chrome has i18n support that provides the ability to reference your extension in your CSS. I keep my images in an image folder in the extension, so reference assets in the CSS like so:

background-image:url('chrome-extension://__MSG_@@extension_id__/images/main.png'); 
like image 189
David Lemphers Avatar answered Sep 20 '22 18:09

David Lemphers


Your image URL should look like chrome-extension://<EXTENSION_ID>/image.jpg

You would be better off replacing css through javascript. From docs:

//Code for displaying <extensionDir>/images/myimage.png: var imgURL = chrome.extension.getURL("images/myimage.png"); document.getElementById("someImage").src = imgURL; 
like image 38
serg Avatar answered Sep 18 '22 18:09

serg