从manifest.json中的content_scripts中排除域不适用于CSS文件?

时间:2013-12-26 12:14:36

标签: css json google-chrome google-chrome-extension google-chrome-devtools

我想写一个chrome扩展程序,强制所有网站使用除Gmail页面之外的给定CSS样式。但是,manifest.json中内容脚本中的以下代码无法使用(Gmail页面仍将使用font.css中提供的样式)。

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

即使采用建议here的策略,将 exclude_matches 替换为 exclude_globs ,也无法修复。

我知道这个错误存在了一段时间,即所谓的Bug #100106。有关如何解决此问题的任何想法?或者我有什么其他方法可以用来实现我的目标?

1 个答案:

答案 0 :(得分:1)

如果从某些内容JavaScript代码中注入CSS,则可以手动过滤页面。我刚做了一个快速测试,以下内容适用于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;
}

这样就可以将font.css脚本注入所有非Google页面。