Excluding domains from content_scripts in manifest.json doesn't work for CSS files?

半腔热情 提交于 2019-12-25 04:15:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20784654/excluding-domains-from-content-scripts-in-manifest-json-doesnt-work-for-css-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!