LLWiki正在建设中,欢迎加入我们!
User:Bhsd/widget/countdown.js
跳转到导航
跳转到搜索
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
//<nowiki> // 用于[[Widget:Countdown]],可以使用ES6语法 "use strict"; (function() { function fromNow(ele, mw) { const now = new Date(), then = new Date( ele.title ), before = ele.children[0], after = ele.children[1], isBefore = (then < now), monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始 lang = mw.config.get( 'wgUserVariant' ), isHant = ['zh-hant', 'zh-tw', 'zh-hk', 'zh-mo'].includes( lang ), textMonth = isHant ? '個月' : '个月', textHour = isHant ? '小時' : '小时'; let year = isBefore ? now.getFullYear() - then.getFullYear() : then.getFullYear() - now.getFullYear(), month = isBefore ? now.getMonth() - then.getMonth() : then.getMonth() - now.getMonth(), day = isBefore ? now.getDate() - then.getDate() : then.getDate() - now.getDate(), hour = isBefore ? now.getHours() - then.getHours() : then.getHours() - now.getHours(), minute = isBefore ? now.getMinutes() - then.getMinutes() : then.getMinutes() - now.getMinutes(), second = isBefore ? now.getSeconds() - then.getSeconds() : then.getSeconds() - now.getSeconds(), result = ""; if (second < 0) { minute--; second += 60; } if (minute < 0) { hour--; minute += 60; } if (hour < 0) { day--; hour += 24; } if (day < 0) { month--; if (monthsHave31Days.includes( (isBefore ? then : now).getMonth() )) { day += 31; } else if ((isBefore ? then : now).getMonth() === 1) { if ((isBefore ? then : now).getFullYear() % 4 === 0) { day += 29; } else { day += 28; } } else { day += 30; } } if (month < 0) { year--; month += 12; } if (year > 0) { result += `<span class="countdown-num">${year}</span>年`; } if (month > 0 || result !== "") { result += `<span class="countdown-num">${month}</span>${textMonth}`; } if (day > 0 || result !== "") { result += `<span class="countdown-num">${day}</span>天`; } if (year === 0 && month === 0) { if (hour > 0 || result !== "") { result += `<span class="countdown-num">${hour}</span>${textHour}`; } if (day === 0) { if (minute > 0 || result !== "") { result += `<span class="countdown-num">${minute}</span>分`; } if (second > 0 || result !== "") { result += `<span class="countdown-num">${second}</span>秒`; } } } if (isBefore) { const countdown = before.querySelector( '.countdown' ); if (countdown) { countdown.innerHTML = result; } before.style.display = ""; after.style.display = "none"; } else { const countdown = after.querySelector( '.countdown' ); if (countdown) { countdown.innerHTML = result; } after.style.display = ""; before.style.display = "none"; } } function run(mw) { document.querySelectorAll( '.countdownNode' ).each(ele => { fromNow(ele, mw); ele.classList.add( 'counting' ); }); } document.querySelectorAll( '.countdownNode' ).forEach(ele => { // 补齐2位数日期 const target = ele.title.replace( /-(\d)(?=\D)/g, '-0$1' ).replace( /-(\d)$/, '-0$1' ), time = new Date(target); if (!time.getTime()) { ele.classList.add( "error" ); ele.textContent = "(时间格式错误!)"; ele.classList.remove( 'countdownNode' ); } else { ele.title = target; } }); console.log( 'setInterval: 等待mediaWiki加载完毕' ); const timerStart = Date.now(), timer = setInterval(() => { if (!window.mediaWiki) { return; } clearInterval(timer); console.log(`End setInterval: mediaWiki加载完毕,用时 ${Date.now() - timerStart} ms`); run(window.mediaWiki); setInterval( run(window.mediaWiki), 1000 ); }, 100); }) (); //</nowiki> // [[category:JavaScript小部件]] {{DEFAULTSORT:Countdown}}