Ad

Excluding Domains From Content_scripts In Manifest.json Doesn't Work For CSS Files?

I want to write a chrome extension to force all websites to use given CSS style except Gmail page. However the following code from content scripts in manifest.json doesn't work (Gmail page will still use the style given in font.css).

"content_scripts":  [{
    "matches":  ["http://*/*", "https://*/*"],
    "exclude_matches": ["*://mail.google.com/*"],
    "css":  ["font.css"]
}]

This cannot be fixed even adopting the strategy advised here by replacing exclude_matches with exclude_globs.

I know this bug existing for a while, so-called Bug #100106. Any idea on how to fix this? Or are there any alternative ways I can use to achieve my goal?

Ad

Answer

You can filter pages manually if you inject CSS from some content JavaScript code. I've just did a quick test, and the following works in Chrome 31.0.1650.63:

manifest.json:

{
    "manifest_version": 2,
    "name": "My Style",
    "description": "Insert custom CSS",
    "version": "0.1",
    "content_scripts": [{
        "matches":  ["http://*/*", "https://*/*"],
        "js": ["font.js"],
        "run_at":"document_start"
    }],
    "web_accessible_resources": ["font.css"]
}

font.js

if (location.hostname.indexOf(".google.com") == -1) {
    var link = document.createElement("link");
    link.href = chrome.extension.getURL("font.css");
    link.type = "text/css";
    link.rel = "stylesheet";
    document.documentElement.insertBefore(link);
}

font.css

body {
    color: red;
}

This way font.css script is injected into all non-Google pages.

Ad
source: stackoverflow.com
Ad