2012-02-02 126 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 
    ] 
} 

祝你好運!