LLWiki正在建设中,欢迎加入我们!
“User:Bhsd/widget/countdown.js”的版本间差异
跳转到导航
跳转到搜索
小 标签:移动版网页编辑 移动版编辑 |
小 (// 使用Wikiplus小工具快速编辑) 标签:移动版网页编辑 移动版编辑 |
||
第5行: | 第5行: | ||
function fromNow(ele, mw) { |
function fromNow(ele, mw) { |
||
const now = new Date(), |
const now = new Date(), |
||
then = new Date( ele. |
then = new Date( ele.title ), |
||
before = ele.children[0], |
before = ele.children[0], |
||
after = ele.children[1], |
after = ele.children[1], |
||
第78行: | 第78行: | ||
document.querySelectorAll( '.countdownNode' ).forEach(ele => { |
document.querySelectorAll( '.countdownNode' ).forEach(ele => { |
||
// 补齐2位数日期 |
// 补齐2位数日期 |
||
const target = ele. |
const target = ele.title.replace( /-(\d)(?=\D)/g, '-0$1' ).replace( /-(\d)$/, '-0$1' ), |
||
time = new Date(target); |
time = new Date(target); |
||
if (!time.getTime()) { |
if (!time.getTime()) { |
||
第85行: | 第85行: | ||
ele.classList.remove( 'countdownNode' ); |
ele.classList.remove( 'countdownNode' ); |
||
} |
} |
||
else { ele. |
else { ele.title = target; } |
||
}); |
}); |
||
console.log( 'setInterval: 等待mediaWiki加载完毕' ); |
console.log( 'setInterval: 等待mediaWiki加载完毕' ); |
2020年11月30日 (一) 08:49的版本
//<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}}