1document.addEventListener('DOMContentLoaded', function() { 2 3 // --- è¨å® --- 4 const timeElementId = 'tokei_time'; // æå»è¡¨ç¤ºè¦ç´ ã®ID 5 // ç»åè¦ç´ ã®IDã§ã¯ãªããå ±éã¯ã©ã¹åã¨IDãã¬ãã£ãã¯ã¹ã使ã 6 const imageWrapperId = 'tokei_photo'; 7 const imageClass = 'clock-image'; 8 const imageIdPrefix = 'clock-image-'; // ID㯠clock-image-1, clock-image-2 ã¨ãã 9 const activeClass = 'active'; // 表示ä¸ã示ãã¯ã©ã¹å 10 // ç»åã®è¨å® 11 const imagePathPrefix = '/img/tokei/photo/'; // ç»åãã£ã¬ã¯ããªã®ãã¹ 12 const imageExtension = '.jpg'; // ç»åã®æ¡å¼µå 13 const defaultImageFile = '00'; // ããã©ã«ãç»åã®ãã¡ã¤ã«å (æ¡å¼µåãªã) 14 // const fadeDuration = 1000; // ãã§ã¼ãæé (ããªç§ãCSSã®transitionã¨åããã) 15 16 // --- è¦ç´ ã®åå¾ --- 17 const timeElement = document.getElementById(timeElementId); 18 const imageWrapper = document.getElementById(imageWrapperId); // ã³ã³ãããåå¾ 19 20 // --- è¦ç´ åå¨ãã§ã㯠--- 21 if (!timeElement) { 22 return; 23 } 24 if (!imageWrapper) { 25 return; 26 } 27 // ç»åè¦ç´ ãåå¾ (2ã¤ããæ³å®) 28 const imageElements = imageWrapper.querySelectorAll(`.${imageClass}`); 29 if (imageElements.length < 2) { 30 return; 31 } 32 const image1 = document.getElementById(`${imageIdPrefix}1`); 33 const image2 = document.getElementById(`${imageIdPrefix}2`); 34 if (!image1 || !image2) { 35 return; 36 } 37 38 // --- ããã©ã«ãç»åã®ãã¹ --- 39 const defaultImagePath = `${imagePathPrefix}${defaultImageFile}${imageExtension}`; 40 41 // --- ç»åèªã¿è¾¼ã¿ã¨ã©ã¼æã®å¦ç颿° --- 42 function handleImageError(imgElement) { 43 // ã¨ã©ã¼ãçºçãããããã©ã«ãç»åã表示 44 // ãã ããç¾å¨ã®srcãæ¢ã«ããã©ã«ãç»åã®å ´åã¯ç¡éã«ã¼ããé²ãããä½ãããªã 45 const currentSrc = imgElement.src.split('?')[0]; // ãã£ãã·ã¥å¯¾çãé¤ãããã¹ 46 if (currentSrc !== defaultImagePath) { 47 // console.warn(`ç»åã®èªã¿è¾¼ã¿ã«å¤±æãã¾ãã: ${imgElement.src}. ããã©ã«ãç»åã表示ãã¾ãã`); 48 imgElement.src = defaultImagePath + '?t=' + Date.now(); // ããã©ã«ãç»åã«ããã£ãã·ã¥å¯¾ç 49 // ããã©ã«ãç»åã®alt屿§ãè¨å® (ä»»æ) 50 imgElement.alt = `ããã©ã«ãã®æè¨ç»å (${defaultImageFile}å)`; 51 } else { 52 // console.error(`ããã©ã«ãç»å (${defaultImagePath}) ã®èªã¿è¾¼ã¿ã«ã失æãã¾ããã`); 53 } 54 // ã¨ã©ã¼ãã³ãã©ã¯ src è¨å®åã«æ¯åè¨å®ããã®ã§ãããã§ null ã«ããå¿ è¦ã¯ãªã 55 } 56 57 // --- æå»ã¨ç»åã®æ´æ°é¢æ° --- 58 function updateClock() { 59 const now = new Date(); 60 const hours = String(now.getHours()).padStart(2, '0'); 61 const minutes = String(now.getMinutes()).padStart(2, '0'); 62 63 // 1. æå»è¡¨ç¤ºãæ´æ° 64 timeElement.textContent = `${hours}:${minutes}`; 65 66 // 2. 表示ããç»åãã¹ãçæ 67 const imagePath = `${imagePathPrefix}${minutes}${imageExtension}`; 68 const cacheBustedImagePath = imagePath + '?t=' + now.getTime(); // ãã£ãã·ã¥å¯¾ç 69 70 // 3. ç¾å¨ã¢ã¯ãã£ããªç»åã¨ã次ã«ã¢ã¯ãã£ãã«ããç»åè¦ç´ ãç¹å® 71 const activeImage = imageWrapper.querySelector(`.${imageClass}.${activeClass}`); 72 const inactiveImage = imageWrapper.querySelector(`.${imageClass}:not(.${activeClass})`); 73 74 // 念ã®ããåå¨ãã§ã㯠75 if (!activeImage || !inactiveImage) { 76 // console.error('ã¢ã¯ãã£ãã¾ãã¯éã¢ã¯ãã£ããªç»åè¦ç´ ãè¦ã¤ããã¾ããã'); 77 scheduleNextUpdate(now); // æ¬¡ã®æ´æ°ã ãã¹ã±ã¸ã¥ã¼ã«ãã¦ä¸æ 78 return; 79 } 80 81 // 4. 次ã«è¡¨ç¤ºããç»åã®ãã¹ããç¾å¨éã¢ã¯ãã£ããªç»åã®ãã¹ã¨åãããã§ã㯠82 const inactiveBaseSrc = inactiveImage.src.split('?')[0]; 83 if (inactiveBaseSrc === imagePath) { 84 // ãã§ã«è£ã§æ£ããç»åãèªã¿è¾¼ã¾ãã¦ããå ´å (ä¾: ã¨ã©ã¼ãã復帰ããå ´åãªã©)
85 // ã¾ãã¯åå表示ãªã©ã§ã¾ã src ãè¨å®ããã¦ããªãå ´å 86 if (inactiveImage.src === '' || inactiveBaseSrc === imagePath) { 87 // ãã®ã¾ã¾ãã§ã¼ãå¦çã¸é²ã 88 performFade(activeImage, inactiveImage); 89 } 90 scheduleNextUpdate(now); // æ¬¡ã®æ´æ°ãã¹ã±ã¸ã¥ã¼ã« 91 return; // ç»åã®åèªã¿è¾¼ã¿ã¯ä¸è¦ 92 } 93 94 // 5. éã¢ã¯ãã£ããªç»åè¦ç´ ã«æ°ããç»åãè¨å®ããèªã¿è¾¼ã¿ã¨ã¨ã©ã¼ãã³ãã©ãè¨å® 95 inactiveImage.onerror = () => handleImageError(inactiveImage); // ã¨ã©ã¼ãã³ãã©ãè¨å® 96 inactiveImage.onload = () => { 97 // ç»åèªã¿è¾¼ã¿æåï¼ã¯ãã¹ãã§ã¼ããå®è¡ 98 performFade(activeImage, inactiveImage); 99 // èªã¿è¾¼ã¿ãæåããã®ã§ãã³ãã©ãã¯ãªã¢ 100 inactiveImage.onload = null; 101 inactiveImage.onerror = null; 102 }; 103 104 // src ãè¨å®ãã¦èªã¿è¾¼ã¿éå§ 105 inactiveImage.src = cacheBustedImagePath; 106 inactiveImage.alt = `${minutes}åã®åç`; // alt屿§ãæ´æ° 107 108 // 6. æ¬¡ã®æ´æ°ãã¹ã±ã¸ã¥ã¼ã« 109 scheduleNextUpdate(now); 110 } 111 112 // --- ã¯ãã¹ãã§ã¼ãå®è¡é¢æ° --- 113 function performFade(fadeOutElement, fadeInElement) { 114 fadeOutElement.classList.remove(activeClass); // å¤ãæ¹ããã§ã¼ãã¢ã¦ã 115 fadeInElement.classList.add(activeClass); // æ°ããæ¹ããã§ã¼ãã¤ã³ 116 } 117 118 // --- æ¬¡ã®æ´æ°ãã¹ã±ã¸ã¥ã¼ã«ãã颿° --- 119 function scheduleNextUpdate(currentTime) { 120 const seconds = currentTime.getSeconds(); 121 const milliseconds = currentTime.getMilliseconds(); 122 const delay = (60 - seconds) * 1000 - milliseconds; 123 setTimeout(updateClock, Math.max(100, delay)); 124 } 125 126 // --- ååå®è¡åã®æºå --- 127 // 念ã®ãããåæç¶æ ã§ã¢ã¯ãã£ãã§ãªãæ¹ã®ç»åã® src ã空ã«ãã¦ãã 128 const initialInactiveImage = imageWrapper.querySelector(`.${imageClass}:not(.${activeClass})`); 129 if (initialInactiveImage) { 130 initialInactiveImage.src = ''; // onload/onerror ãæå³ããçºç«ããã®ãé²ã 131 } 132 133 // --- ååå®è¡ --- 134 updateClock(); 135 136});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.