LLWiki正在建设中,欢迎加入我们!
“User:Bhsd/widget/countdown.js”的版本间差异
跳转到导航
跳转到搜索
小 (Bhsd移动页面User:Bhsd/countdown.js至User:Bhsd/widget/countdown.js,不留重定向:错误命名) |
小 标签:移动版网页编辑 移动版编辑 |
||
第3行: | 第3行: | ||
"use strict"; |
"use strict"; |
||
(function() { |
(function() { |
||
function fromNow(ele, mw) { |
|||
const now = new Date(), |
const now = new Date(), |
||
then = new Date( ele.dataset.target ), |
then = new Date( ele.dataset.target ), |
||
before = ele.children[0], |
before = ele.children[0], |
||
after = ele.children[1], |
after = ele.children[1], |
||
i18n = ele.dataset.title, |
|||
textMonth = i18n.slice(2, 4).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'), |
|||
textHour = i18n.slice(0, 2).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'), |
|||
isBefore = (then < now), |
isBefore = (then < now), |
||
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11] |
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(), |
let year = isBefore ? now.getFullYear() - then.getFullYear() : then.getFullYear() - now.getFullYear(), |
||
month = isBefore ? now.getMonth() - then.getMonth() : then.getMonth() - now.getMonth(), |
month = isBefore ? now.getMonth() - then.getMonth() : then.getMonth() - now.getMonth(), |
||
第67行: | 第68行: | ||
before.style.display = "none"; |
before.style.display = "none"; |
||
} |
} |
||
} |
} |
||
function run(mw) { |
|||
document.querySelectorAll( '.countdownNode' ). |
document.querySelectorAll( '.countdownNode' ).each(ele => { |
||
fromNow(ele, mw); |
|||
ele.classList.add( 'counting' ); |
|||
ele.removeAttribute('title'); |
|||
} |
|||
fromNow(ele); |
|||
ele.style.visibility = "visible"; |
|||
}); |
}); |
||
} |
} |
||
document.querySelectorAll( '.countdownNode' ).forEach(ele => { |
document.querySelectorAll( '.countdownNode' ).forEach(ele => { |
||
// 补齐2位数日期 |
// 补齐2位数日期 |
||
第86行: | 第84行: | ||
ele.textContent = "(时间格式错误!)"; |
ele.textContent = "(时间格式错误!)"; |
||
ele.classList.remove( 'countdownNode' ); |
ele.classList.remove( 'countdownNode' ); |
||
ele.removeAttribute('title'); |
|||
} |
} |
||
else { ele.dataset.target = target; } |
else { ele.dataset.target = 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); |
|||
⚫ | |||
⚫ | |||
}) (); |
}) (); |
||
//</nowiki> |
//</nowiki> |
2020年11月30日 (一) 08:46的版本
//<nowiki> // 用于[[Widget:Countdown]],可以使用ES6语法 "use strict"; (function() { function fromNow(ele, mw) { const now = new Date(), then = new Date( ele.dataset.target ), 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.dataset.target.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.dataset.target = 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}}