改变页面样式表在chrome扩展

changing page style sheet in chrome extension

本文关键字:chrome 扩展 样式 改变      更新时间:2023-09-26

我试图做一个扩展来改变页面样式表。我可以改变一些样式,比如背景色但我不能改变整个样式表

清单。json 文件

{
  "name": "change stylesheet",
  "version": "1.0",
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
      "default_title": "Set this page's style",
      "default_icon": "icon.png",
      "popup": "popup.html"
  }
}

部分,Java脚本调用在popup.htm

function click() {
  chrome.tabs.executeScript(null,
      {code:"document.getElementById('stylesheet').href = 'style1.css'"});
  window.close();
}

Susantha -你只需要在清单中包含你的css文件:

http://code.google.com/chrome/extensions/content_scripts.html

{
"name": "My extension",
 ...
  "content_scripts": [
{
  "matches": ["http://www.google.com/*"],
  "css": ["mystyles.css"],
  "js": ["jquery.js", "myscript.js"]
}
 ],
  ...
 }

您将需要用!important覆盖现有的样式,并遵循样式的先例,因为您通常必须这样做。如果有内联样式,那么你可能需要用js代码重写它。