ååã®ç¶ããâ ð°JSåå¿è ãäœãGoogle extension V3 â¡ -popup.htmlã®äœæ-
å®æããäœåã¯ãâããã¡ãð
popup.js
ååpopupã®HTMLãäœæããã®ã§è²ã
ãšåããã€ããããã«jsãã¡ã€ã«ãèšå
¥ããŸãã
ãã®jsãã¡ã€ã«ã«ã¯
ã»ã¿ãã®åãæ¿ãã®åŠç
ã»ã¿ã€ããŒã衚瀺ããã
ã»ã¹ã¿ãŒã/ã¹ããã/ãªã»ããã®åãã¿ã³æ©èœ
ã»ã¿ã€ããŒäœåäžã®æç¡ã§è¡šç€ºããç»é¢ã®åŠç
ãšå€ãã®åŠçãè¡ãã
ãã®èšäºã§ã¯ã¿ãã®åãæ¿ãã®åŠçãèšè¿°ããŠããã
ã¿ãã®åãæ¿ãã®åŠç
ã¿ãã®åãæ¿ãã¯ãã¡ãã®èšäºãåèã«ããŸããã
document.getElementsBy~ã®äœ¿ãæ¹ãæå³ããªããšãªããããçšåºŠã ã£ãã®ã§ãèšäºãèŠãŠãç解ããã®ã«æéãããããŸããã
ããããå
容ã¯ãã¡ãââ
ïŒãã£ã©ãšãã®ä»ã®äžèº«ã¯çç¥ããŸããïŒ
<div class="tab-panel">
<!--ã¿ãã«ã¯å
±éããŠãtabããã®classãæããã
ãããããããæåã«è¡šç€ºããŠãããããã®ã«ãis-activeããè¿œå ãã-->
<ul class="tab-group">
<li class="tab tab-timer is-active">ã¿ã€ããŒ</li>
<li class="tab tab-character">ãã£ã©</li>
<li class="tab tab-other">ãã®ä»</li>
</ul>
<!--ã¿ããåãæ¿ããŠè¡šç€ºããã³ã³ãã³ã-->
ãããã<!--ã³ã³ãã³ããã¿ãåèã«ãå
±éããŠãcontentããã®classãæããã
ãããããããæåã«è¡šç€ºããŠãããããã®ã«ãis-showããè¿œå ãã-->
<div class="content-group">
<div class="content tab-timer is-show">ã¿ã€ããŒã®äžèº«</div>
<div class="content tab-character">ãã£ã©ã®äžèº«</div>
<div class="content tab-other">ãã®ä»ã®äžèº«</div>
</div>
</div>
document.addEventListener('DOMContentLoaded', function(){
// ãtabããæã£ãŠããã¯ã©ã¹å
šãŠã«å¯ŸããŠã¯ãªãã¯ã€ãã³ããé©çš
const tabs = document.getElementsByClassName('tab');
for(let i = 0; i < tabs.length; i++) {
tabs[i].addEventListener('click', tabSwitch, false);
}
// ã¿ããã¯ãªãã¯ãããšå®è¡ããé¢æ°
function tabSwitch(){
// ã¿ãã®classã®å€ãå€æŽ
//[0]ãšãªã£ãŠããã®ã¯ã'is-active'ã¯ïŒåãããã
document.getElementsByClassName('is-active')[0].classList.remove('is-active');
this.classList.add('is-active');
// ã³ã³ãã³ãã®classã®å€ãå€æŽ
//[0]ãšãªã£ãŠããã®ã¯ã'is-show'ã¯ïŒåãããªããã
document.getElementsByClassName('is-show')[0].classList.remove('is-show');
const arrayTabs = Array.prototype.slice.call(tabs);
const index = arrayTabs.indexOf(this);
document.getElementsByClassName('content')[index].classList.add('is-show');
};
}, false);
âclassList
classåãèªã¿åãã
add(), remove(), replace(), toggle() ã®åã¡ãœãããçšããŠãªããžã§ã¯ããå€æŽããããšãã§ããã
âthis
thisã¯åŒã³ã ãããã¿ã€ãã³ã°ãç¶æ³ã«ãã£ãŠåç §å ãã³ãã³ããšå€ããã
åºæ¬çãªã€ã¡ãŒãžãšããŠã¯
ã¡ãœããå
ã§thisãåŒã³åºãããå Žåã¯ããã®ã¡ãœãããæ ŒçŽãããŠãããªããžã§ã¯ããžã®åç
§ãšãªãã
æ ŒçŽãããŠãããªããžã§ã¯ããååšããªãå Žåã¯ãwindowãªããžã§ã¯ãã®åç §ãšãªããšããæãã«ãªããŸãã
ä»åã®thisã¯ã¯ãªãã¯ããã¿ãã®ããšã瀺ãããŸãã
âconst arrayTabs = Array.prototype.slice.call(tabs)
Array.prototypeã®ãšããã[].
ã«å€ããŠ[].slice.call(tabs)
ãšãæžãããããã
ããã¯äœãããŠããã®ããšãããšãé
å颚ãªããžã§ã¯ããé
åã«å€æããŠããã
é å颚ãªããžã§ã¯ããšã¯
ãlengthãæã¡ã[0], [1], [2]ã®ããã«ã¢ã¯ã»ã¹ã§ãããªããžã§ã¯ããããé å颚ãªããžã§ã¯ããšèšã
Array
â é
å
prototype
â ç¶æ¿
slice
â æååãé
åãªã©ããããŒã¿ã®äžéšåã ãåãåºããã¡ãœããã
call
â ãããªããžã§ã¯ãã«æå±ããé¢æ°ãã¡ãœããããå¥ã®ãªããžã§ã¯ãã«å²ãåœãŠãŠåŒã³åºãã
ïŒåïŒåã¡ãœãããèŠãŠæå³çã«ãclassåtabãåå¥ã§åãåºãé åã«ããŠããããšãããããŸãã
âconst index = arrayTabs.indexOf(this)
indexOfïŒïŒ
â ãã®ã¡ãœããã¯åŒæ°ã«äžããããå
容ãšåãå
容ãæã€æåã®é
åèŠçŽ ã®æ·»åãè¿ãã
æ°ãã«ã¯ãªãã¯ãããã¿ã[n]çªç®ãindexãšããå€æ°ã«æ ŒçŽããŠããã
document.getElementsByClassName('content')[index].classList.add('is-show');
ã®index
ã«ãããã
ã€ãŸããis-active
ãšåã衚瀺ããããã³ã³ãã³ãã«is-show
ãè¿œå ã§ãããããªåŠçãè¡ãªã£ãŠããã
æ¡åŒµæ©èœã®èšäºãªã³ã¯
ð°JSåå¿è
ãäœãGoogle extension V3 â
ð°JSåå¿è
ãäœãGoogle extension V3 â¡ -popup.htmlã®äœæ-
ð°JSåå¿è
ãäœãGoogle extension V3 ⢠-ã¿ãã®åãæ¿ãã®åŠç-
ð°JSåå¿è
ãäœãGoogle extension V3 ⣠-ã¿ã€ããŒã衚瀺ããã-
ð°JSåå¿è
ãäœãGoogle extension V3 †-ã¹ã¿ãŒã/ã¹ããã/ãªã»ããã®åãã¿ã³æ©èœ-
ð°JSåå¿è
ãäœãGoogle extension V3 ⥠-ã¿ã€ããŒäœåäžã®æç¡ã§è¡šç€ºããç»é¢ã®åŠç-
ð°JSåå¿è
ãäœãGoogle extension V3 ⊠-åã€ã³ã¹ããŒã«æã®åŠç/ïŒåéã®åæ¥ç¶åŠç-
ð°JSåå¿è
ãäœãGoogle extension V3 ⧠-start/stop/resetæŒããæã®æå-
ð°JSåå¿è
ãäœãGoogle extension V3 âš -ãã¢ããŒãã¿ã€ããŒã®åŠç-
ð°JSåå¿è
ãäœãGoogle extension V3 â© -popupã®ãã¢ããŒãã¿ã€ããŒã®åŠç-
ð°JSåå¿è
ãäœãGoogle extension V3 ⪠-content.jsã®åŠç-
åèèšäº
å€æ°ãšå®£èš
ãJavaScriptå
¥éãsliceã§æååãé
å(Array)ãåãæãæ¹æ³ãŸãšã
thisã®æŠèŠãšäœ¿ãæ¹