Jac*_*lom 18 html javascript css google-chrome
我是Chrome Extension开发的新手.我知道可以注入CSS.但是可以为特定的URL注入它.例如,每当我访问google.com时,都会注入CSS.
谢谢您的帮助!:)
Max*_*aer 33
好吧,你有两个选项,程序注入和内容脚本.名字可能听起来非常复杂和令人恐惧,但不要担心;)
内容脚本将在加载页面时自动注入.除了编写脚本之外,您需要做的就是在manifest.json中指定类似的内容:
{
"name": "My extension",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": ["http://www.google.com/"], //where your script should be injected
"css": ["css_file.css"] //the name of the file to be injected
}
]
}
Run Code Online (Sandbox Code Playgroud)
这应该在每次加载google.com时注入CSS
您的另一个选择是使用Programmatic Injection.如果您希望有时仅通过后台页面注入代码,这将非常有用.为此,您可以使用insertCSS().在这种情况下,您需要在清单中拥有主机权限:
{
"name": "My extension",
"version": "1.0",
"manifest_version": 2,
"background_page": "myBackground.html", //if you want to inject it from a background page
"permissions": [
"background", //if you want to inject it from a background page
"http://www.google.com/" // host permission to google
]
}
Run Code Online (Sandbox Code Playgroud)
祝好运!
归档时间: |
|
查看次数: |
18575 次 |
最近记录: |