从manifest.json中的content_scripts中排除域不适用于CSS文件? [英] Excluding domains from content_scripts in manifest.json doesn't work for CSS files?

查看:221
本文介绍了从manifest.json中的content_scripts中排除域不适用于CSS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Chrome扩展程序来强制所有网站使用给定的CSS样式,除了Gmail页面。然而,以下来自 manifest.json 内容脚本的代码不起作用(Gmail页面仍将使用 font.css

 content_scripts:[{
matches:[http:// c $ c>) * / *,https:// * / *],
exclude_matches:[*://mail.google.com/*],
css:[ font.css]
}]

即使采用建议的策略< 此处通过将 exclude_matches 替换为 exclude_globs



我知道这个bug存在一段时间,所谓的 Bug#100106 。任何想法如何解决这个问题?或者是否有其他方法可以用来实现我的目标?

解决方案

内容JavaScript代码。我刚做了一个快速测试,在Chrome 31.0.1650.63中有以下工作:



manifest.json:

  {
manifest_version:2,
name:我的风格,
description:插入自定义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页面。 / p>

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?

解决方案

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.

这篇关于从manifest.json中的content_scripts中排除域不适用于CSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆