Tutorial: Getting Started (Hello, World!) - Google Chrome Extensions - Google Code
ãåèã«Extensionãä½æãã¾ããã
ä¸çªåããããããã¥ã¼ããªã¢ã«ãªããããªããã¨æãã¾ãã
ãã¥ã¼ããªã¢ã«ã§ã¯ãmanifest.jsonã¨popup.htmlã®ã¿ãä½æããã·ã³ãã«ãªãã®ã«ãªã£ã¦ãã¾ãã
èªåã¯ããã«å ãã¦ããPage ActionããBackground PageããOptions Pageãã®APIã使ç¨ãã¦ãç°¡åãªExtensionãä½æãã¾ããã
ãã®ä½æ¥ã®æµãã®ã¡ã¢ã§ãã
æåã®manifest.jsonã®è¨å®ã¨ãããã¢ãããã¼ã¸ã®ä½æ
Extensionã§ã¯ãã©ããã£ãAPIã使ããã©ã®ã¹ã¯ãªãããã©ã®ã¿ã¤ãã³ã°ã§å®è¡ããã®ããã¨ãã£ãäºãè¨å®ãããã¡ã¤ã«ãããã¾ãã
ãããmanifest.jsonã§ãã
å
容ã¯ä¸è¨ã®ãããªãã®ã«ãªã£ã¦ãã¾ãã
{ "name": "Insert Hello", "version": "1.0", "description": "Insert Hello message.", "browser_action": { "default_icon": "icon.png", "popup": "popup.html" } }
ä¸è¨ã®è¨å®ã§ã¯ãã¢ããªã®ååããã¼ã¸ã§ã³ãªã©ãè¨è¿°ãã¦ãã¾ãã
ã¾ãããbrowser_actionãã¨ããAPIã«ã¤ãã¦ãè¨å®ãè¨è¿°ãã¦ãã¾ãã
è¨å®ã¯ãä¸è¨ã®äºé
ç®ã§ãã
- ãã©ã¦ã¶ã®å·¦ä¸ã«è¡¨ç¤ºããã¢ã¤ã³ã³ã®ç»å(default_icon) (DLã¯こちら)
- ã¯ãªãã¯ããæã®popupãã¼ã¸(popup.html)
ããã§è¨å®ãã¦ããpopup.htmlãä½æãã¾ããã
<!DOCTYPE html> <html> <meta charset="utf-8"> <style> body { min-width:357px; overflow-x:hidden; } </style> <div id="hello"> Popup Area </div> </html>
ãããã®ãã¡ã¤ã«ãä¸ã¤ã®ãã£ã¬ã¯ããªã«å
¥ãã¾ãã
ãã¹ãåä½
chromeãã©ã¦ã¶ãèµ·åãã¦ä¸è¨ã®æé ã§ãä½æãããã¡ã¤ã«ãèªã¿è¾¼ã¾ãã¦å®è¡ãã¾ãã
- ãã¼ã«ã¢ã¤ã³ã³ããã¦ã£ã³ãã¦ãã¡ãã¥ã¼ãããæ¡å¼µæ©è½ããé¸æ
- ããã£ããããã¼ã¢ã¼ãããã¯ãªãã¯
- ãããã±ã¼ã¸åããã¦ããªãæ¡å¼µæ©è½ãèªã¿è¾¼ã¿ã¾ãããã¯ãªãã¯ããè¨å®ãã¡ã¤ã«ãªã©ãå ¥ã£ã¦ãããã£ã¬ã¯ããªãé¸æ
以ä¸ã®æé ãè¡ãªãã¨ãã¡ãã¥ã¼ã«è¿½å ããã¾ãã
ã¢ãã¬ã¹ãã¼ã®æ¨ªã«ã¢ã¤ã³ã³ç»åã表示ãããã¯ãã§ãã
ãã®ã¢ã¤ã³ã³ãã¯ãªãã¯ããã¨ããPopup Areaãã¨ããæåã表示ããããããã¢ããã¡ãã¥ã¼ã表示ãããã¨æãã¾ãã
å ¨ãã¼ã¸ã«ãHello, ââããæ¿å ¥ããExtension
ãã©ã¦ã¶ã§éãããã¼ã¸ã®ä¸çªä¸ã«ãHello, ââãã¨ããæè¨ãæ¿å ¥ããExtensionãä½æãã¾ãã
è¨å®ãã¼ã¸
æ©è½ã¨ãã¦ãExtensionã®è¨å®ç»é¢ã§ãââã«ãªã«ãå
¥ããã®ããè¨å®ã§ããããã«ãã¾ãã
ããããå®ç¾ããããã«ãä¸è¨ã®ãã®ãä½æãã¾ãã
- è¨å®ç»é¢(options_page.html)
- è¨å®ç»é¢ã§è¨å®ãããå¤ãåç §ããããã®ãã¼ã¸(background.html)
- ãã¼ã¸ãéããæã«ããHello,ââããæ¿å ¥ããããã®ã¹ã¯ãªãã(insert_hello.js)
è¨å®ç»é¢(options_page.html)
ââã«å
¥ããå¤ãè¨å®ããç»é¢ãä½æãã¾ããä½æã«ã¯ãæ®éã®Webãã¼ã¸ã¨åæ§ã«HTML+javascriptã§ä½æãã¾ãã
http://github.com/memememomo/insert_hello/blob/master/options_page.html
å
¥åãã©ã¼ã ã¯æ®éã«inputã¿ã°ã§ä½æãã¾ããã
ãã¿ã³ãæ¼ããããjavascriptã§ã¤ãã³ããå¦çãã¾ãã
<table class="account"> <tr> <td class="name"> Username: <input id="name" type="text" name="name" onkeypress="onChange();"/> </td> <td class="submit"> <input id="submit" type="submit" name="" value="save" disabled onclick="setName();"/> </td> <td class="cancel"> <input id="clear" type="submit" name="" value="clear" disabled onclick="clearName();"/> </td> </tr> </table>
è¨å®ãããå¤ã¯ãwindow.localStorageãã¨ããã¨ããã«ä¿åãã¾ãã
ãã®é åã¸ã®ä¿åï¼åç
§ï¼åé¤ãªã©ã®æä½ã¯ä¸è¨ã®ããã«è¡ãªãã¾ãã
function getStorage() { return window.localStrage; } // åç § function getItem(key) { return getStorage().getItem(key); } // ä¿å function setItem(key, value) { return getStorage().setItem(key, value); } // åé¤ function removeItem(key) { return getStorage().removeItem(key); }
ã覧ã®éããkey-valueã®å½¢ã«ãªã£ã¦ãã¾ãã
è¨å®ç»é¢ã§è¨å®ãããå¤ã®ããã¨ã
ãã¼ã¸æ¯ã«å®è¡ããã¹ã¯ãªãããContent Scriptã¨å¼ã³ã¾ãã
Content Scriptãããè¨å®ç»é¢ã§è¨å®ãããå¤ãåç
§ããããã«ã¯ãBackground Pageãä»ãå¿
è¦ãããã¾ãã
ã¤ã¾ããContent Scriptã¨Background Pageãããã¨ããè¡ãªãã¾ãã
ããã¨ãã¯ããchrome.extension.onRequestãã¨ãchrome.extension.sendRequestãã§è¡ãªãã¾ãã
Background Page(background.html)ã§ã¯ãä¸è¨ã®ããã«è¨è¿°ãã¾ãã
<html> <script> chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { switch(request.action) { case 'getName': sendResponse(getNameFromStorage()); break; } } ); function getNameFromStorage() { return getItem('name'); } function getItem(key) { return getStorage().getItem(key); } function setItem(key, map) { return getStorage().setItem(key, map); } function removeItem(key) { return getStorage().removeItem(key); } function getStorage() { return window.localStorage; } </script> </html>
ãchrome.extension.onRequest.addListenerãã®é¨åã§ãContent Scriptããã®ãªã¯ã¨ã¹ãã«å¯¾å¿ããå¦çãè¨è¿°ããã¦ãã¾ãã
Content Script(insert_hello.js)ã§ã¯ãä¸è¨ã®ããã«è¨è¿°ãã¾ãã
chrome.extension.sendRequest({"action": "getName"}, function(response) { var name = response; if (name == '' || name == null) { name = 'guest'; } var hello = document.createElement('div'); hello.innerText = "Hello, " + name; var d = document.getElementsByTagName('body')[0]; var dp = d.parentNode; dp.insertBefore(hello, d); });
ãchrome.extension.sendRequestãã§ãBackground Pageã«ãªã¯ã¨ã¹ããéã£ã¦ãã¾ããã³ã¼ã«ããã¯é¢æ°ã§ãè¨å®ç»é¢ã§è¨å®ãããå¤ãåãåããDOMæä½ã§ã¡ãã»ã¼ã¸ãæ¿å
¥ãã¦ãã¾ãã
Content Scriptå ã®window.localStorage
Content Script(insert_hello.js)å
ã§ãwindow.localStorageã«ã¢ã¯ã»ã¹ã§ãã¾ããããããè¨å®ç»é¢ã§è¨å®ããå¤ã¯ãããã«ã¯ããã¾ããã
window.localStorageã¯ãåãã¼ã¸æ¯ã«æããããã®ã ããã§ãã
extensionå
¨ä½ã§å
±éã®window.localStrageã¯ãBackground Pageã«ãããã®ã«ãªãã¾ãã
manifest.jsonã®è¨å®
æ°ãããã¼ã¸ãä½æããã®ã§ãmanifest.jsonã«ããã®æ¨ãè¨è¿°ãã¾ãã
ä»åãä½æããåã«ã¤ãã¦ã¯ãä¸è¨ã®ããã«è¨è¿°ãã¾ããã
"options_page" : "options_page.html", "background_page": "background.html", "content_scripts": [ { "js" :[ "insert_hello.js" // ã¹ã¯ãªãããã¡ã¤ã«å ], "matches": [ "http://*/*", "https://*/*" ], "run_at": "document_end" // ã¹ã¯ãªããå®è¡ã¿ã¤ãã³ã° } ],
以ä¸ã§ãä½æãããã¡ã¤ã«ãä¸ã¤ã®ãã£ã¬ã¯ããªã«ç½®ããChromeã«èªã¿è¾¼ã¾ããã°åä½ã確èªã§ãã¾ãã