2012-02-02 133 views
14

我对Chrome扩展开发颇为陌生。我知道可以注入CSS。但有可能为特定的URL注入它。例如,每当我访问google.com时,CSS都会被注入。为Chrome扩展注入CSS

感谢您的帮助! :)

+0

直到现在你的代码是什么? – machineaddict 2012-02-02 19:27:15

回答

28

那么,你有2个选项,程序化注入和内容脚本。这些名字听起来真的很复杂和可怕,但不用担心;)

Content Scripts会在页面加载时自动注入。所有你需要(从写剧本开)做的,是在你的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 
    } 
    ] 
} 

这应该每次加载时间google.com

您的其他注入CSS选项是使用Programmatic Injection。 如果您想仅在有时通常从背景页面注入代码,这可能很有用。为此,您可以使用insertCSS()。在这种情况下,您需要在清单中输入host permission

{ 
    "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 
    ] 
} 

祝你好运!