css - 从 manifest.json 中的 content_scripts 中排除域对 CSS 文件不起作用?

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

我想写一个 chrome 扩展来强制所有网站使用给定的 CSS 样式,除了 Gmail 页面。但是,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 .关于如何解决这个问题的任何想法?或者我可以使用其他方法来实现我的目标吗?

最佳答案

如果您从某些内容 JavaScript 代码中注入(inject) CSS,则可以手动过滤页面。我刚刚做了一个快速测试,以下内容适用于 Chrome 31.0.1650.63:

list .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"]
}

字体.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);
}

字体.css

body {
    color: red;
}

通过这种方式,font.css 脚本被注入(inject)所有非 Google 页面。

关于css - 从 manifest.json 中的 content_scripts 中排除域对 CSS 文件不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784654/

相关文章:

javascript - 查看 Chrome 控制台是否打开

css - Chrome 中的高度 100%

javascript - 更改 CSS 中的制表符空格长度

css - 在 CSS 中设置全屏图像背景

javascript - Angular $resource.save 到 json 文件不起作用

java - 如何使用 jax -rs 创建 POST 请求

html - 使用 CSS 显示/隐藏内容。我如何隐藏默认值

html - word-wrap:break-word css 属性在 IE8 标准模式下不起作用

java - 使用 Java 从 (geo)json 数组中删除对象

javascript - 有没有人能够使用 CSS3 在 Chrome 中以横向模式成功打印?