最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Content Scripts CSS doesn't overwrite the original - Stack Overflow

programmeradmin4浏览0评论

My problem that I want to modify a style of a site with my custom settings. I tried with Content Scripts, but this dosent work, because they can't overwrite the original css files. Here is an example:

foo/manifest.json

{
  "name": "test",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["file://*/*test.html"],
      "css": ["main.css"]
    }
  ]
}

foo/main.css

body {
  background: #0f0;
}

test.html

<html>
  <head>
    <title>foobar</title>
  </head>

  <body style="background:#f00;">

  </body>
</html>

Then i loaded the the extension foo folder into my google chrome, and opened the test.html but the background color still red. I inspected the element and i saw that:

Element Style

body {
background: #f00;
}

user stylesheet

body {
background: #0f0;
}


How can I modify an existing css file with my custom css with Content Scripts?
If this not possible, how can i automatically modify an existing css with my custom when page loads in google chrome specially.

My problem that I want to modify a style of a site with my custom settings. I tried with Content Scripts, but this dosent work, because they can't overwrite the original css files. Here is an example:

foo/manifest.json

{
  "name": "test",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["file://*/*test.html"],
      "css": ["main.css"]
    }
  ]
}

foo/main.css

body {
  background: #0f0;
}

test.html

<html>
  <head>
    <title>foobar</title>
  </head>

  <body style="background:#f00;">

  </body>
</html>

Then i loaded the the extension foo folder into my google chrome, and opened the test.html but the background color still red. I inspected the element and i saw that:

Element Style

body {
background: #f00;
}

user stylesheet

body {
background: #0f0;
}


How can I modify an existing css file with my custom css with Content Scripts?
If this not possible, how can i automatically modify an existing css with my custom when page loads in google chrome specially.

Share Improve this question edited May 20, 2011 at 21:37 serg 111k78 gold badges325 silver badges337 bronze badges asked May 20, 2011 at 20:53 Gergely FehérváriGergely Fehérvári 7,9516 gold badges52 silver badges77 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

An inline style rule has higher precedent over any rules imported from a stylesheet. You could use the !important directive to subvert this behavior:

body {
  background: #0f0 !important;
}

Using javascript, add an ID to the body tag, or some other tag that enpasses everything. Then you can do this:

// original rule
.someclass li a{
    color: blue;
}
// your rule
#cool-extension .someclass li a{
    color: red;
}

If you use the original full selector, and prepend it with your ID, your rule will always take precedence.

发布评论

评论列表(0)

  1. 暂无评论