10

I'm developing a "content script" Chrome extension. I'm trying to use a CSS files to style some existing elements on a page and to style elements that I create dynamically with JavaScript.

I've specified my CSS file in the manifest.json file:

{
    ...
    "content_scripts": [
        {
            "matches": [ ... ],
            "js": [ ... ],
            "css": [ "style.css" ]
        }
    ]
    ...
}

And then if I put something like below into my style.css nothing happens:

body {
    background-color: red;
}

I've also put some rules in the form of:

#MyInsertedEl {
    color: red;
}

And then inserted (using jQuery) a element with the id of MyInsertedEl (anchor link) into the page but it's colour is not red (the element is inserted and visible in the page).

So what gives? What am I doing wrong? According to the docs I've got everything as I should, and google does not return anything useful.

edit: this code is run on facebook's pages if that is relevant in any way ...

3 Answers 3

11

Well, apparently, there's a bug in Chrome. If you have a ? in your matches expression CSS files will not be loaded. I've filed a bug report with more details here.

0
1

Is your javascript getting injected (match rule is correct)? Try adding !important to your css rules:

color: red !important;
3
  • The rule is correct. JS files under the same rule are executing. !important does not help. Commented Sep 29, 2010 at 21:19
  • @Jan have you tried to open debug console and inspect your element to see what kind of stylesheets are applied to it? Extensions's css should be displayed in "user stylesheet" section.
    – serg
    Commented Sep 29, 2010 at 21:48
  • no styles are applied (apart from what the page defines) and there is no "user stylesheet" section :\ Commented Sep 29, 2010 at 22:11
1

see edit below

You need to have at least one stylesheet file included on your page in order to have your content_script styles applied:

<link rel="stylesheet" type="text/css" href="#" />

So if you don't have any stylesheets included just add a blank link tag like the above.

I know it sounds funny but it actually worked for me.

EDIT

Two things are specific to my scenario

  1. The bug occur only on file protocol
  2. The files are non .html (in my case .md)

Q: Why do I need stylesheets on .md files?

A: The content_script matches on .md files and inserts a js file that compiles the markdown to html. The styles are for the generated html.

manifest.json

{
    "manifest_version": 2,
    "name"            : "bug",
    "version"         : "1.0",
    "description"     : "bug",

    "content_scripts": [
        {
            "matches": [
                "*://*/*.*",
                "file:///*/*.*"
            ],
            "css": ["style.css"],
            "js": ["script.js"],
            "run_at": "document_start",
            "all_frames": true
        }
    ]
}

script.js

window.addEventListener('load', function (e) {
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.type = 'text/css';
    link.href = '#';
    document.head.appendChild(link);
}, false);

style.css

body { background: gray; }

Steps

  1. open up some file:///1.md file
  2. comment out the code inside script.js
  3. reload the extension
  4. go back to the previously opened .md file - initially the styles are loaded
  5. refresh the page - the styles are not being applied

I understand that this is an edge case, but it's possible scenario for my extension and I just wanted to point out my solution.

2
  • This sounds extremely unlikely. If it really happens, create a report at crbug.com/new
    – Rob W
    Commented Sep 3, 2013 at 16:51
  • Thanks for the suggestion @RobW but I'm not entirely sure that this is an actual bug or not, although in my case it looks like a bug. See the edit above.
    – simo
    Commented Sep 3, 2013 at 22:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.