æããšãããã£ãŠä»åŸã¡ãããšåœ¢ã«ããããšæã£ãŠãããã ãã©ããã®åã«äžæžããšãããå©ãå°ãããã®æãã§äžåºŠäžã«åºããŠã¿ãŸãã
æŠèŠ
- ãããããããïŒ
- ãã«ãã·ã¹ãã é¡ã®ãªãç§æçæ§æã§ãããã
- APIäºææ§ã¯ãããããããªãã£ã«å ¥ããŠFirefoxã«å¯ããŠäœãã®ãåã
- Edgeã¯éãæãã
- Safariã¯æªèª¿æ»
åºæ¬çãªäœãæ¹
- ãã©ã«ããäœã
manifest.json
ãçšæ- ããã«åãããŠåçš®ã¹ã¯ãªããçãçšæ
- ãã©ãŠã¶ãŒã§éçºäžã®ãã®ãèªã¿èŸŒã¿éçº
- å ¬é
åãå Žæ
- ããŒã«ããŒã®ãã¿ã³
- ä»»æã®ãµã€ãå
- ããã¯ã°ã©ãŠã³ã
- ç¬èªããŒãž
ä»ã«éçºè ããŒã«ã«ã¿ããè¿œå ããããFirefoxãªããµã€ãããŒãäœã£ããããããšãã§ããŸãã
ã§ããããš
æ¡åŒµçšã®APIããã£ã±ãããã®ã§ããããé§äœ¿ããã
ãã¡ããæ®éã®ãŠã§ãéçºã®ç¥èŠãåå©çšã§ãããã
ææ°ã®JavaScriptãæžãã
IE察å¿ã®å¿ èŠããªãïŒãããã¶ãEdgeãç¡èŠãããã ããïŒã®ã§ãChromeã ãããããã¯ChromeãšFirefoxã ãã察象ã«ãªããŸãããããšææ°ã®JavaScriptã®æžãæ¹ããããã䜿ããŸãã
class
ãšã async
/await
ãšããé¢æ°åŒæ°ã®åæå€ãšãåå²ä»£å
¥ãšãã
ããã蟺ã®å匷ãå ŒããŠææŠããŠã¿ãã®ã¯ãã£ãšè¯ãéžæã ãšæããŸãã
Hello Worldãã€ã³ã¹ããŒã«ããŠã¿ã
æåã®ãã©ãŠã¶ãŒæ¡åŒµãèªäœãããŸãã«ãäžåºŠãµã³ãã«ãã€ã³ã¹ããŒã«ããŠã¿ãŸããããã©ããèªäœãããããäœæ¥ã§ãã
ãµã³ãã«ã¯ãã¡ããzipãã¡ã€ã«ã®äžèº«ãã©ããã«å±éããŠãããŠãã ããã
Chrome
- ã¡ãã¥ãŒïŒãŠã£ã³ããŠå³äžã®äžã€ã®ç¹ïŒ â ãã®ä»ã®ããŒã« â æ¡åŒµæ©èœ
- å³äžãããããããŒã¢ãŒããããªã³ã«
- ãããã±ãŒãžåãããŠããªãæ¡åŒµæ©èœãèªã¿èŸŒãããã
manifest.json
ããããã£ã¬ã¯ããªãŒãéžæ
ãã®åŸäžèŠ§ã«è¡šç€ºãããã«ãŒããã以äžã®æäœãå¯èœã§ã
- åèªã¿èŸŒã¿ïŒããã£ãšãããã¿ã³ïŒ
- åé€
- ããã¯ã°ã©ãŠã³ãã¹ã¯ãªããçšéçºè ããŒã«ãéã
éçºçšã«èªã¿èŸŒãã æ¡åŒµæ©èœã¯ã次åãã©ãŠã¶ãŒèµ·åæã«ãæå¹ã§ãããèŠåã衚瀺ãããŸãã
Firefox
æ®éã®æ¡åŒµæ©èœäžèŠ§ãšã¯éãå Žæã§æäœããŸãã
about:debugging
ãéãïŒã¢ãã¬ã¹ããŒã«å ¥åïŒ- å³äž “Load Temporary Add-on” çãªãã¿ã³ãã
manifest.json
ãéã
ãã®åŸäžèŠ§ã«è¡šç€ºãããã«ãŒããã以äžã®æäœãå¯èœã§ãã
- åèªã¿èŸŒã¿
- åé€
- æ¡åŒµæ©èœçšã³ã³ãœãŒã«ãéã
éçºè ããŒã«ã¯ããããã¢ããã§å°ãããã “Incoming Connection” ãåãå ¥ããå¿ èŠããããŸãã
ãããšãããã¢ããããããã°ããå Žåãéçºè ããŒã«å³äžã®ãåè§ãåã€äžŠãã “Disable popup auto hide” ããªã³ã«ããŸãã
éçºçšã«èªã¿èŸŒãã æ¡åŒµæ©èœã¯ã次åãã©ãŠã¶ãŒèµ·åæã«ã¯åé€ããããããéœåºŠåãæé ã§èªã¿èŸŒãå¿ èŠããããŸãã
Edge
about:flags
ãéãïŒã¢ãã¬ã¹ããŒã«å ¥åïŒ- éçºè åãèšå®ã®ãæ¡åŒµæ©èœã®éçºè åãæ©èœãæå¹ã«ããïŒããã«ããâŠïŒãããªã³ã«
- ã¡ãã¥ãŒïŒãŠã£ã³ããŠå³äžã®äžã€ã®ç¹ïŒ â æ¡åŒµæ©èœ
- ãæ¡åŒµæ©èœã®èªã¿èŸŒã¿ããã
manifest.json
ããããã£ã¬ã¯ããªãŒãéžæ
ãã®åŸäžèŠ§ãã詳现ãéããŠä»¥äžã®æäœãå¯èœã§ãã
- åèªã¿èŸŒã¿
- åé€
- ããã¯ã°ã©ãŠã³ãã¹ã¯ãªããçšéçºè ããŒã«ãéã
éçºçšã«èªã¿èŸŒãã æ¡åŒµæ©èœã¯ã次åãã©ãŠã¶ãŒèµ·åæã«èŠåã衚瀺ãããç¡å¹åãããŸãã
Safari
ãã®ãã¡èª¿ã¹ãŸãã
Hello Worldãäœã
èªäœã®ãã®ã®ã€ã³ã¹ããŒã«ã®ããæ¹ãããã£ããšããã§ãå®å¿ããŠèªåã®Hello Worldãäœãå§ããŸãã
ããäœãå§ããåã«ããã£ãã€ã³ã¹ããŒã«ãããµã³ãã«ã¯ããæ¶ãã¡ãã£ãŠãã ããã
ãµã³ãã«ãšåããã®ããã¿ã³ãæŒããšãããŒãããã€ããå§ããŸãããã以äžã®ããã« hello
ãã£ã¬ã¯ããªãšããã®äžã«ãã¡ã€ã«ãäœããŸãã
hello/
manifest.json
popup.html
æ¬ç« ã§åŠã¹ãããš
- ãã©ãŠã¶ãŒæ¡åŒµã®åºæ¬çãªäœãæ¹ãéçºã®æ¹æ³
manifest.json
å ¥é- ãããã¢ãã
manifest.json
ã®çšæ
{ "manifest_version": 2, "name": "Hello World!", "version": "1.0.0", "author": "Ginpei", "browser_action": { "default_popup": "popup.html" } }
HTMLã®çšæ
éçHTMLã§ããã奜ã¿ã§ã¡ãããšãããã€ã«æžãæããŠãã ãã£ãŠçµæ§ã
<!doctype html> <html lang=en> <title>Hello World!</title> <h1>Hello World!</h1>
ã§ãããã
ãšãããããããŸã§ã§ã§ãããããããã€ã³ã¹ããŒã«ããŸããããïŒåé åèïŒ
Hello Worldã«ããã¡ãã£ãšè¶³ã
ã€ã³ã¹ããŒã«ã§ããŸããïŒãåããŸãããïŒ
ãšããããåããããå°ããã€è¶³ããŠãããŸãããã
æ¬ç« ã§åŠã¹ãããš
- ãããã¢ããé¢ä¿ã®èšå®ãå°ã
- ãããã¢ããHTMLã§CSSãèªã¿èŸŒã
- ãã©ãŠã¶ãŒæ¡åŒµã§ã®ãã¹ã®æ±ã
ã¢ã€ã³ã³ã足ã
ãŸãã¯ç°¡åãªãšããããããã¿ã³ã®ã¢ã€ã³ã³ãèšå®ããŸãã
manifest.json
ã«ã以äžã®ããã« "default_icon"
ã足ããŸããã¢ã€ã³ã³ç»åã¯é©åœã«ãçšæãã ããã
"browser_action": { "default_icon": "icon-90.png", "default_popup": "index.html" }
æžã足ããŠäžæžãä¿åãããããã©ãŠã¶ãŒã®æ¹ã§æ¡åŒµãåèªã¿èŸŒã¿ããŸããããªããšåæ ãããŸãããåœç¶ã
ã¿ã€ãã«ã足ã
åæ§ã« "default_title"
ã足ããŸãã
ããŠã¹ã«ãŒãœã«ããã¿ã³ã«ä¹ããéã«ããŒã«ããããšããŠè¡šç€ºãããããã§ãã
"browser_action": { "default_icon": "icon-90.png", "default_title": "Hello World!", "default_popup": "index.html" }
CSSã足ã
ãã HTMLããèªã¿èŸŒãã ãã§ããããŒãããŒãã
ãŸããä»ãã hello
é
äžã« popup.css
ã以äžã®å
容ã§äœæããŸãã
body { align-items: center; display: flex; justify-content: center; margin: 0; min-height: 300px; min-width: 300px; }
ããã€ãèªã¿èŸŒããããæ¢åã® index.html
ã« <link>
ã足ããŸãã
<!doctype html> <html lang=en> <title>Hello World!</title> <link rel="stylesheet" href="/popup.css"> <h1>Hello World!</h1>
ãããã¢ãããéããªãããšãèŠãç®ãå€ãã£ãŠããã¯ãã§ãã
ã¡ãªã¿ã«ä»åå€æŽããã®ã¯ãããã¢ããã®äžèº«ã ããªã®ã§ãåèªã¿èŸŒã¿ã¯äžèŠã§ããïŒäžå®ãªãããŠãã ãããïŒ
ãã¹ã«ã€ããŠ
manifest.json
ã®ããäœçœ®ãã«ãŒãã«ãªããŸãããªã®ã§ /
ããå§ããããšãã§ããã
../../../../../
ãšãæžããŠãããããžã§ã¯ãã®ãã£ã¬ã¯ããªããäžãèŠãããã¯ããªãã¿ããã
ïŒä»æ§ã¯èª¿ã¹ãŠããªããïŒ
ç°¡åãªJavaScriptãæžããŠã¿ã
æ¬æ Œçãªæ¡åŒµã®æ©èœãäœãå§ããåã«ããããã¢ããå ã§å®çµããããç°¡åãªã¹ã¯ãªããããå§ããããšæããŸããããã人ã¯ããã£ãšæµããŠãã ããã
æ¬ç« ã§åŠã¹ãããš
- ãããã¢ããHTMLã§JSãèªã¿èŸŒã
äœããã®
äœã«ãããããªãŒããã£ã±ãæåã¯ãã¿ã³ãæŒãããäœããããã€ããã§ããããŒã
ãšããããã§ããã¿ã³ãæŒããã “Hello World!” ãåºãŠãããã€ã«ããŸãã
HTMLã®çšæ
JSãã¡ã€ã«ãCSSãšåæ§ããã HTMLããèªã¿èŸŒãã ãã§ãã
index.html
ã¡ãã£ãšæžãæããŠãããã« <script>
ãè¿œå ããŸãããŸã èªã¿èŸŒãå
ã®JSãã¡ã€ã«ã¯äœã£ãŠãªããã©ã
<!doctype html> <html lang=en> <title>Hello World!</title> <link rel="stylesheet" href="/popup.css"> <div> <h1 id="yabaiMessage" style="display: none">Hello World!</h1> <button id="sugoiButton">Push me!</button> </div> <script src="/popup.js"></script>
ã¹ã¯ãªãããå®è£ ãã
hello
é
äžã« popup.js
ãäœæããŸãããã£ãHTMLã«è¿œå ãã <script>
ã§èªã¿èŸŒããã€ã§ãã
const sugoiButton = document.querySelector('#sugoiButton'); sugoiButton.addEventListener('click', () => { sugoiButton.style.display = 'none'; yabaiMessage.style.display = ''; });
ããã§ã©ãã§ããããããããã¢ãããéããš “Push me!” ã®ãã¿ã³ã衚瀺ããããããæŒããš “Hello World!” ã®æåã«çœ®ãæããã¯ãã§ãã
ããã§ããŸãã
ãšããããã§ããããã¢ããå ã§å®çµãããã®ãäœãåã«ã¯ãæ®éã®HTMLãæ®éã®CSSããããŠæ®éã®JavaScriptãæžãã ãã ãšããããšãããããé ããããšæããŸãã
ç°¡åã§ãããïŒ
ãšãã£ãŠãããã ãããäœãé¢çœãã¯ãªãã®ã§ãããã¡ãã£ãšäœãããŠã¿ãŸãããã
jQueryãæããïŒ
ãããã¢ããã«ã€ããŠã¯åè¿°ã®éããæ®éã®HTMLããªã®ã§ãæ®éã«jQueryãèªã¿èŸŒãã§äœ¿ãããšãã§ããŸãããŸããã¡ããè¯ããã°ãã¡ãã§ãæ§ããŸããã
ããjQueryããé¢ããã³ãŒããæžãå§ãããã®ã§ããããè¯ãæ©äŒãããããŸããããã ãè±jQueryããŽãŒã«ãªã®ãããšã«ãããã©ãŠã¶ãŒæ¡åŒµãäœãããã®ããç®æšã決ããŠãããŸããããå¢ãä»ããŠäºã€åæã«ç®æããšãã¹ã転ãã ãšãã«çãã§ãã
jQuery䜿ãã«ãã䜿ããªãã«ãããé£ãããšããã¯ããã蟺ãããªãã§ãã
ãã©ãŠã¶ãŒæ¡åŒµã®APIãã²ãšã€äœ¿ã£ãŠã¿ã
â»æ¬ç« ã®å 容ã¯ChromeãšFirefoxã§åããŸããEdgeã§ã¯åããŸããã
ãããã¢ããã¯åºãŠããããã©ãæžããã®ã¯æ®éã®HTMLãæ®éã®CSSããããŠæ®éã®JavaScriptã§ããã次ã¯ãã©ãŠã¶ãŒæ¡åŒµã ãã®æ©èœã䜿ã£ãŠã¿ãŸãããã
äœã«ãããããªãé°å²æ°ã«æ £ããããã®ç°¡åãªãã®ãè¯ããªã
notifications APIã䜿ã£ãŠã¿ãŸããããã
æ¬ç« ã§åŠã¹ãããš
- ãã©ãŠã¶ãŒæ¡åŒµçšã®APIã®æŠèŠ
- browser.notifications
- ãã©ãŠã¶ãŒæ¡åŒµçšã®ããŒããã·ã§ã³ã®èšå®
manifest.json
– permissions- APIä»æ§ã®èª¿ã¹æ¹
- APIäºææ§ã®èª¿ã¹æ¹
ãã©ãŠã¶ãŒæ¡åŒµçšã®API矀 chrome
ChromeãFirefoxå
±ã«ã°ããŒãã«ã® chrome
ãªããžã§ã¯ã以äžã«API矀ãçšæãããŠããŸããäŸãã°ä»åå©çšããAPIã ãš chrome.notifications.create()
ãšãããã®ãå©çšããŸãã
Edge㯠browser
ãªããžã§ã¯ãã«ãããå®è£
ãããŠããããã§ãããªã chrome
ãªããžã§ã¯ããååšããŠããŠããããããã¯å
šãå¥ç©ã®ããã§ããïŒãã¿ãŸããã詳现ã¯ããããããŸãããïŒãEdgeã§åããããå Žåã¯ã³ãŒãäŸã® chrome
ã browser
ã«çœ®ãæããŠãã ãããããããããåããããããŸãããåããªããããããŸãããä»åïŒnotifications APIïŒã¯åããŸããã
äºææ§ã¯çŸæç¹ã§ã¯ãŸã äžå®å šã§ãæšæºåãå®äºããŠããŸãããæçšãªAPIã§ãäžéšç°å¢ã§ãã䜿ããªããã®ããã£ããããŸãã
APIã«ã€ããŠã¯ããã¡ãã£ãšãäºææ§ãšãFirefoxã® browser
ãªããžã§ã¯ããšãã®è©±ããããã ãã©ãåŸåãã«ããŸãã
ãç¥ããAPIã®äœ¿ãæ¹
ãšãŸãããããããã§ããŠãnotification APIã䜿ããŸãããŠã§ãæšæºã®æ¹ã®Notification APIãšã¯å¥ç©ã§ãèš±å¯ãæ±ãããããã¢ããã衚瀺ãããŸãããïŒãã©ãŠã¶ãŒæ¡åŒµã€ã³ã¹ããŒã«æã«æ å ±ã¯è¡šç€ºãããŸããïŒ
ãã£ããããã€ããããã¢ããã§è©ŠããŠã¿ãŸããããã³ãŒãã¯ç°¡åã§ãã hello
å
ã® popup.js
ãéããŠã以äžã®å
容ã«æžãæããŸãã
const sugoiButton = document.querySelector('sugoiButton'); sugoiButton.addEventListener('click', () => { chrome.notifications.create(null, { type: chrome.notifications.TemplateType.BASIC, iconUrl: '/icon-90.png', title: 'Hello World!', message: 'ãå æ°ã§ãã', }); });
ãã®ã³ãŒãã¯æ£ããã®ã§ãã³ããã§äžåºŠè©ŠããŠã¿ãŠé ãããã®ã§ãããå®ã¯ãåããŸãããç¹°ãè¿ããŸããããã®ã³ãŒãã¯æ£ãããã©ãåããªããã§ãã
ã€ãŸããã®ã³ãŒããããªããšãããæ£ãããªããšããããšã§ããã
ãšããããããŸãåããªããšãã¯ããã³ã³ãœãŒã«ã«ãšã©ãŒãåºãŠããªãã確èªããŠã¿ãŠãã ãããïŒãããã¢ããã®äžã§å³ã¯ãªãã¯âèŠçŽ ã®æ€èšŒãšãããããã®ã§åºãŠããŸããïŒ
Chromeã§ããã°ãããªãšã©ãŒãåºãŠããã¯ãã§ãã
popup.js:3 Uncaught TypeError: Cannot read property 'create' of undefined at HTMLButtonElement.sugoiButton.onclick (popup.js:3)
Firefoxãªãããã
TypeError: chrome.notifications is undefined [Learn More] popup.js:3:5
è¬ã§ãããæ£ããã³ãŒãã®ãªããªã®ã« chrome.notificatiions
ã undefined
ã ããšèšãããŠããŸãã
å®ã¯ãã®ãªããžã§ã¯ãã¯ãäºåã«ããŒããã·ã§ã³ã®èšå®ãããŠãããªããšäœ¿ããªãã®ã§ãã
ããŒããã·ã§ã³ã®èšå®
ãšããããã§ãèšå®ããŸãã
manifest.json
ãéããŠã "permissions"
ãè¿œå ããŸãã
{ "manifest_version": 2, "name": "Hello World!", "version": "1.0.0", "author": "Ginpei", "permissions": [ "notifications" ], "browser_action": { "default_icon": "icon-90.png", "default_title": "Hello World!", "default_popup": "index.html" } }
äœæ¥ãšããŠã¯ããã ãã§ããæŽæ°åŸããã©ãŠã¶ãŒã®æ¹ã§ã¯æ¡åŒµãåèªã¿èŸŒã¿ããŠãã ããã manifest.json
ãæŽæ°ããã®ã§ããããã¢ãããéããªããã ããã足ããªãã§ãã
äžèšã®ããã« "permissions"
ã®èšå®ãé©åã«è¡ããåèªã¿èŸŒã¿ããŠãããäžåºŠãã¿ã³ãæšãã°ãã»ãã¡ãããšäœããåºãŠããã¯ãã§ãã
APIã®äœ¿ãæ¹ã調ã¹ã
åããŠè¯ãã£ãè¯ãã£ããšãããšããã§ãããããã¡ãã£ãšãããããããããªãŒãšãæããããããŸãããä»æããªããŠããå°æ¥æããããããŸããããªã®ã§ãAPIã®ä»æ§ã軜ã調ã¹ãŠãããŸãããã
Firefoxã®è£œé å ã§ããMozillaã管çãããMDNãšãããµã€ãã«ãªãã¡ã¬ã³ã¹ããããŸããçŸæç¹ã§ã¯ããŸãæ¥æ¬èªçããªãããã§ããããŸãAPIã«ã€ããŠèª¿ã¹ãããããªãè±èªããJavaScriptèªãèªãã°è¯ãã®ã§ããªããšããªãã§ãããã
ä»åã®notification APIã«ã€ããŠã¯ããJavaScript API 矀ãã«äžŠãã§ããã®ãèŠã€ããããŸãã
æžåŒã¯ãããªã£ãŠããããã§ãã
var creating = browser.notifications.create( id, // optional string options // NotificationOptions )
åŒæ°ãæ»ãå€ãã©ããªã£ãŠãã®ãããããŸãããè¯ãã£ãè¯ãã£ãã
ããŒãžã®äžã®æ¹ã«ã¯ããã©ãŠã¶äºææ§ãã®é ããããŸãããããèŠãã°ãèªåã§å®è£ ããŠè©Šãããšãªã確èªããããšãã§ããŸãã
ã©ãããEdgeã§ã¯å®è£ ãããŠããªãããã§ãããŠã§ãæšæºã®Notificationã®æ¹ã¯ããçšåºŠå®è£ ãé²ãã§ãããããªã®ã§ããã¡ãã§ãã©ãŒã«ããã¯ããå¿ èŠããããããããŸããããŸã察象å€ã«ãã¡ãã£ãŠãè¯ããã ãããã©ã
ã¿ã€ããŒãäœã
ãç¥ããã§ããããã«ãªã£ãã®ã§ãäœããç¥ãããããã§ãããäœããç¥ããããŸãããããæéã§ãããããäžåã§ãããããã«ããã©ãŒã¡ã³ã®ã§ããããã§ããããã
ãããã¿ã€ããŒãäœããŸãããã
æ¬ç« ã§åŠã¹ãããš
- ããã¯ã°ã©ãŠã³ãåŠç
manifest.json
– background- ãã©ãŠã¶ãŒæ¡åŒµã®éšåéã®éä¿¡
- browser.runtime.sendMessage()
- browser.runtime.onMessage
äœããã®
ãšããããç°¡åãªæ¹ãè¯ãããããŸããããã¢ããã䜿ããšããŠãHTMLã«ã¯åãããã«ãã¿ã³ãã²ãšã€ã ããæŒããš3ååŸã«ãç¥ãããããã§ãããŸãããã
æžããŠã¿ã
ãŸãããããåããªããã ãã©ããã£ããå®è£ ããŸãã
sugoiButton.onclick = () => { const delay = 3 * 1000; // 3ç§ setTimeout(() => { chrome.notifications.create(null, { type: chrome.notifications.TemplateType.BASIC, iconUrl: '/icon-90.png', title: 'Hello World!', message: 'ãå æ°ã§ãã', }); }, delay); };
äœã®ã²ããããªã setTimeout()
ã§ããããã3åã ãšéçºäžã¯é·ãããã®ã§ããã£ãã3ç§ã«ããŠãããŸããã¯ãã
ã§ããããã¢ãããéããŠããã¿ã³ãæŒããŠããã®ãŸãŸ3ç§åŸ ã£ãŠãã ãããåºãŸãããïŒ
ã§ããïŒ
âŠâŠåºãŸããããä»åã¯ã
ãããïœãããããã§çµããããªïœïŒãããããããããé§ç®ã§ãã
次ã¯ãã¿ã³ãæŒããããããã«ãããã¢ãããéããŠãã ããããããã¢ããã®å€ãã¯ãªãã¯ããã°æ¶ããŸãã
ãããããšãâŠâŠãç¥ãããåºãŠããªããªããŸãããªããŠãã£ãïŒ
ãããã¢ããã®ã©ã€ããµã€ã¯ã«
ã©ã€ããµã€ã¯ã«ãšããè¡šçŸã§è¯ãã®ãããããªããã ãã©ããããã¢ããã®äžèº«ã¯æ®éã®HTMLã§ããæ®éã®HTMLãªã®ã§ãéãããããããå§ãŸã£ãŠãéãããå šãŠçµãããŸãã
æ®éã®ãŠã§ãããŒãžã§ setTimeout()
ã䜿ã£ãŠå®è£
ããå Žåãæ³åããŠã»ãããã§ããããã®å ŽåãããŒãžãéãããããã¿ã€ããŒã¯åããªãã§ãããããããã¢ãããåæ§ãéããæç¹ã§ã¿ã€ããŒã¯æ¶ããŠããŸããŸãã
3ååŸã«ãç¥ããããããšãããã3åéãã£ãšãããã¢ãããéãã£ã±ãªãã«ããªããšãããªããªããŸããããŒã
ãã£ãšåãç¶ããã¹ã¯ãªãã
ããã§äœ¿ãã®ãããã¯ã°ã©ãŠã³ãã§ãããã©ãŠã¶ãŒæ¡åŒµã®èªã¿èŸŒã¿ãšåæã«éå§ããããã®åŸãã£ãšåãç¶ããã¹ã¯ãªããã§ãã
æžããŠã¿ãŸãããããŸã㯠manifest.json
ã§ãããããã¢ããã®èšå®ã®ããã«ãããã¯ã°ã©ãŠã³ãã®èšå® "background"
ãèšè¿°ããŸãã
{ "manifest_version": 2, "name": "Hello World!", "version": "1.0.0", "author": "Ginpei", "permissions": [ "notifications" ], "browser_action": { "default_icon": "icon-90.png", "default_title": "Hello World!", "default_popup": "index.html" }, "background": { "scripts": [ "background.js" ] } }
ä¹
ãã¶ããªã®ã§å
šéšèŒããŸãããè¿œå ãã "background"
ã¯æåŸã§ãã
ç¶ã㊠background.js
ãæ°èŠäœæããŸãã
const delay = 3 * 1000; // 3ç§ setTimeout(() => { chrome.notifications.create(null, { type: chrome.notifications.TemplateType.BASIC, iconUrl: '/icon-90.png', title: 'Hello World!', message: 'ãå æ°ã§ãã', }); }, delay);
ããæžããŠæ°èŠäœæããŠãåèªã¿èŸŒã¿ããŠã3ç§åŸ ã€ãšâŠâŠããããã¢ããã觊ããªããŠããç¥ãããåºãŠããŸãïŒããã£ãïŒ
ãã£ãŠããïŒ
èµ·åã®3ç§åŸã«ãç¥ããããŠãä»æ¹ããªãã§ããããããã¢ãããéããŠãåãããããšã¯ããã£ãã®ã§ã次ã®èª²é¡ã¯ãããã€ããããã¢ããããæäœããŠã¿ã€ããŒèµ·åããããšã§ãã
ãããã¢ããããããã¯ã°ã©ãŠã³ããžéä¿¡
é信㯠chrome.runtime.sendMessage()
ãš chrome.runtime.onMessage
ã§ãããšãã§ããŸãã
ãŸãéãæ¹ããã㯠popup.js
ã§ãã
const sugoiButton = document.querySelector('#sugoiButton'); sugoiButton.onclick = () => { const message = { delay: 3 * 1000, // 3ç§ title: 'Hello World!', message: 'ãå æ°ã§ãã', }; chrome.runtime.sendMessage(message); };
ç¶ããŠåããæ¹ããã¡ãã background.js
ã§ãã
chrome.runtime.onMessage.addListener((message) => { setTimeout(() => { chrome.notifications.create({ type: chrome.notifications.TemplateType.BASIC, iconUrl: '/icon-90.png', title: message.title, message: message.message, }); }, message.delay); });
ããã£ãšãããŸãããã message
ãªããžã§ã¯ããéããŠæ
å ±ãéãããšãã§ããŸãã
ããŠããã¯ã°ã©ãŠã³ãã®ã¹ã¯ãªããã¯ãåè¿°ã®éãåãã£ã±ãªãã§ããã®ã§ãå€æŽãããäžåºŠãã©ãŠã¶ãŒæ¡åŒµãåèªã¿èŸŒã¿ããå¿ èŠããããŸãããå¿ããªãã
ãšããããã§
æžãæããŠãåèªã¿èŸŒã¿ããŠããããã¢ããã®ãã¿ã³ãæŒããŠãéããŠã3ç§åŸ ã€ãšããç¥ãããåºãŠããã¯ãã§ãã
ãã£ããïŒ
äºææ§
æè¿ãã¡ãã«æžããã®ã§ã©ããã
ããŠæ¬¡ã¯
ã¿ã€ããŒèªäœã¯åãããã«ãªã£ãã®ã§ãããã¡ãã£ãšãããã¢ããã®èŠãç®ãæŽãããããŸããããããã£ãšã
ããšæ©èœè¿œå ãšããŠãæéã3åã§åºå®ã§ã¯ãªãä»»æã«å ¥åã§ããããã«ããããã©ãã«ãããŠæ®ãæéã衚瀺ããããéäžã§ããããåéããããªãããã§ãããšè¯ããããããŸããã
ããããã°æè¿ã¿ã€ããŒãšããã°ããã¢ããŒãããšããã®ãæµè¡ã§ãã30åããšã«äžåºåãä»ãããã€ã§ããããç®æããŠã¿ãŠã¯ã©ãã§ãããããããã«ãã¢ããŒãã®ã¿ã€ããŒãã§ããŠä»äºã§äœ¿ããããã«ãªã£ãããä»åºŠã¯ä»äºã®é²æãèœçã管çããããªããããããŸããã
ãšããæãã§ã¡ãã£ãšã¢ã€ãã£ã¢ã ããäœãäœããããã®ãæãã€ããªãæ¹ã¯ããããªæãã§ç·Žç¿ããŠãäœã£ãŠã¿ãŠã¯ãããã§ãããããšãããææ¡ã§ããããŸãã
ããããããäœããã®ãããªããŠäœãæ¹ã®æ¹ã§ãæ¬æ Œçã«NPMããã±ãŒãžãè«žã å°å ¥ããŠèãåºããã®ãè¯ãããã§ãã
å ¬éãã
äœã£ããã®ã¯å ¬éããŸãããããã©ãŠã¶ãŒããšã«ã¹ãã¢ãçšæãããŠããŸãã
Chromeã®æ¹ã§éçºè ç»é²ããã®ã«ãåå$5ããããŸãã身å蚌æã¿ãããªããã§ããããFirefoxã®æ¹ã¯ç¡æã§ãã
ããããæå®ãµã€ãºã®ç»åãšããè«žã ã®æ å ±ãå ¥åããå¿ èŠããããŸãã
Edgeã¯1,847åããããŸããã§ããã®åŸã®æé ãããããããªããŠããŸã å ¬éã§ããŠãŸããããããã
䜿ããããªAPIã®çŽ¹ä»
APIäžèŠ§ãçºããŠãããããããªãã®ãèŠã€ããŸãããã
ãã®ãã¡ã®äžéšãç°¡åã«çŽ¹ä»ã ãã
æ å ±ãä¿åãã
storage.local.get()
ãš storage.local.set()
ãã
"permissions"
ã®èšå®ããå¿ããªãã
3çš®é¡ãããã©ããšãããã storage.local
ã§è¯ãããã
storage.local
– 端æ«åäœã«ä¿åãããæ å ±ãäœãèããã«äœ¿ããstorage.sync
– ãã°ã€ã³ããŠç«¯æ«éã§å ±æãããæ å ±ã£ãœããFirefoxã¯ã²ãšæéããstorage.managed
– å®è³ªmanifest.json
ã§å®çŸ©ããæ å ±ãèªãå°çšã£ãœããå¥éruntime.getManifest()
ãããã
ããš await
䜿ãå Žåã¯ãã®å€åŽã async
ã«ãªã£ãŠããå¿
èŠããããŸãã
(async () => { const message = document.querySelector('#message'); // æåã«èªã¿èŸŒã¿ const result = await browser.storage.local.get(['message']); message.value = '2'; message.value = result.message || 'Hello World!'; // å€æŽããã°æžã蟌㿠message.addEventListener('input', async () => { browser.storage.local.set({ message: message.value }); }); })();
èŠãŠããããŒãžã«æ¿å ¥ãã
ç¹å®ã®ããŒãžããããã¯å
šããŒãžã«JSãã¡ã€ã«ãCSSãã¡ã€ã«ãæ¿å
¥ããå Žåã¯ã manifest.json
ã® content_scripts
ã䜿ãã
ã¹ã¯ãªããããã³ãŒããçæããŠå®è¡ããå Žåããããã¯JSãã¡ã€ã«ãCSSãã¡ã€ã«ãæ¿å
¥ããå Žå㯠tabs.executeScript()
ã tabs.insertCSS()
ã䜿ãããããã manifest.json
ã® "permissions"
㧠"activeTab"
ãèš±å¯ããã
æ¿å
¥ãããã¡ã€ã«ãããã©ãŠã¶ãŒæ¡åŒµãæã€ãã¡ã€ã«ã«ã¢ã¯ã»ã¹ããå ŽåïŒç»åã衚瀺ãããšãïŒã manifest.json
㧠web_accessible_resources
ã䜿ã£ãŠã¢ã¯ã»ã¹æš©ã®èšå®ãå¿
èŠã
ã¢ã€ã³ã³ã®èã«ããã¹ãã衚瀺
æ°åãšãè±åèªãšããªãã browserAction.setBadgeText()
ã§ã¢ã€ã³ã³ã®èã«åºåããŠåžžæèŠããããšãã§ããã
ã¢ã€ã³ã³ãåçã«æäœ
ãªã³ãšãªãã§ã¢ã€ã³ã³ç»åãåãæ¿ããã¿ãããªã®ã«ã¯ browserAction.setIcon()
ã䜿ãã
ç»åã¯äºåã«çšæãããã®ã«éãããCanvasãšãã§åçã«çæãããã®ãèšå®ã§ãããïŒãæªèª¿æ»ã
èšå®ç»é¢
manifest.json
ã® options_ui
ã䜿ããåãã©ãŠã¶ãŒã®æ¡åŒµæ©èœç®¡çããŒãžããéããããã¹ã¯ãªããããã runtime.openOptionsPage()
ã§éããã
ã¡ãªã¿ã« options_page
ãšããã®ãããã
æ¡åŒµå°çšã®ä»»æããŒãž
tabs.create()
ã§ã url
ã®æå®ã /
ããå§ããŠä»»æã®HTMLãã¡ã€ã«çãæå®ããã
manifest.json
ã®æ
å ±ãåŸã
runtime.getManifest()
ã䜿ãã
ã¹ã¯ãªããéã§ãããšããã
æ¬æã§ããã£ããã©ã runtime.sendMessage()
ãš runtime.onMessage
ã§ãããšããããããã¯ã°ã©ãŠã³ããããã®ä»ãžããããã¯ãã®ä»ããããã¯ã°ã©ãŠã³ããžã®ã¡ãã»ãŒãžéåä¿¡ã«ãªãã£ãœãã
ã³ã³ãã³ãã¹ã¯ãªããã ãšåä¿¡ã§ããªãïŒ
ãããŸã
ã£ãŠãã話ãVuePressã䜿ã£ãŠãã£ãŠã¿ããã ãã©ãã©ãã§å ¬éããããšãèããŠãéã«å ã«å 容ãããçšåºŠåºããŠããããšæã£ãŠããã«çœ®ããŠãããŸããããã¡ãã£ãšããŒãžãåããããã¹ã¯ãªãŒã³ã·ã§ãããè¿œå ããããããäºå®ã§ãã
ããšMDNãå šç¶æ¥æ¬èªã«ãªã£ãŠãªããŠã翻蚳äœæ¥ãããæ°ã¯ãããã ãã©ãæŽæ°ãšããããªããŠæ°èŠã«ãã£ãçµéšã¯ãªããŠãã©ãªãæ§ãã«äžåºŠã¬ãã¥ãŒããŠããããããªãšæãã€ã€ãã©ãã§èª°ã«é Œãã°ããããããªãã®ã§ããªããããã°æããŠã»ããã§ããïŒåã身ïŒ
以äžã§ããä»åŸãšãäœåå®ãããé¡ãèŽããŸãã