use*_*276 17 css google-chrome-extension content-script
任何人都可以向我解释这个.我正在尝试使用带有Google扩展的content_script将CSS文件注入网页,但我的css文件永远不会添加到网页中.有人能告诉我我做错了什么并帮我解决了吗?谢谢
表现:
{
"name": "Extension",
"version": "0",
"description": "",
"permissions": ["tabs", "http://*/*", "https://*/*", "file:///*/*"],
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*", "file:///*/*"],
"css": ["myStyles.css"],
"js": ["myScript.js"],
"all_frames": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
myStyles.css
#test {
margin: 0 10px;
background: #fff;
padding: 3px;
color: #000;
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*b W 39
样式表实际上已注入,但未应用,因为其他样式会覆盖规则.要使规则起作用,您有一些选择:
将每条规则都添加到!important:
#test {
margin: 0 10px !important;
background: #fff !important;
padding: 3px !important;
color: #000 !important;
}
Run Code Online (Sandbox Code Playgroud)通过内容脚本注入CSS:
myScript.js:
var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = chrome.extension.getURL('myStyles.css');
(document.head||document.documentElement).appendChild(style);
Run Code Online (Sandbox Code Playgroud)
manifest.json
{
"name": "Extension",
"version": "0",
"description": "",
"manifest_version": 2,
"permissions": ["tabs", "http://*/*", "https://*/*", "file:///*/*"],
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*", "file:///*/*"],
"js": ["myScript.js"],
"all_frames": true
}
],
"web_accessible_resources": ["myStyles.css"]
}
Run Code Online (Sandbox Code Playgroud)
web_accessible_resources当清单版本 2处于活动状态时,最后一个密钥是必需的,以便可以从非扩展页面读取CSS文件.