“Widget:Countdown”的版本间差异

删除361字节 、​ 2020年10月5日 (一) 22:34
撤销Bhsd讨论)的版本11063
(撤销Bhsd讨论)的版本11063)
标签撤销
<noinclude>{{doc|content={{CCnotice|MGP=widget:countdown}}
 
请使用{{tl|countdown}}调用。}}</noinclude><includeonly><!--{if !isset($wgCountdown) || !$wgCountdown}--><!--{assign var="wgCountdown" value=true scope="global"}--><script src="/mediawiki/resources/lib/moment/moment.js"></script><script>
//</includeonly>JavaScript脚本:<pre lang=js>
"use strict";
window.addEventListener('load', function () {
mw.loader.using(const 'moment'fromNow = ).then(function (ele) {
constlet fromNownow = function moment(ele) {,
let nowthen = moment(ele.dataset.target),
thenbefore = moment(ele.dataset.target)children[0],
beforeafter = ele.children[01],
afteri18n = ele.children[1]dataset.title,
i18nisBefore = ele.datasetthen.titleisBefore(now),
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
isBefore = then.isBefore(now),
year = isBefore ? now.year() - then.year() : then.year() - now.year(),
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
yearmonth = isBefore ? now.yearmonth() - then.yearmonth() : then.yearmonth() - now.yearmonth(),
monthday = isBefore ? now.monthdate() - then.monthdate() : then.monthdate() - now.monthdate(),
dayhour = isBefore ? now.datehour() - then.datehour() : then.datehour() - now.datehour(),
hourminute = isBefore ? now.hourminute() - then.hourminute() : then.hourminute() - now.hourminute(),
minutesecond = isBefore ? now.minutesecond() - then.minutesecond() : then.minutesecond() - now.minutesecond(),
result = "";
second = isBefore ? now.second() - then.second() : then.second() - now.second(),
if (second < 0) result = "";{
if (second < 0) {minute--;
second += minute--60;
second += 60;}
if (minute < 0) {
hour--;
minute += 60;
}
if (hour < 0) {
day--;
hour += 24;
}
if (day < 0) {
month--;
if (monthsHave31Days.includes((isBefore ? then : now).month())) { day += 31; }
else if ((isBefore ? then : now).month() === 1) {
if ((isBefore ? then : now).year() % 4 === 0) { day += 29; }
else { day += 28; }
}
ifelse (minute{ <day 0)+= {30; }
hour--;}
if (month < 0) minute += 60;{
year--;
month += 12;
}
if (year > 0) { result += `<span class="countdown-num">${year}</` + 'span>年'; }
if (month > 0) { result += `<span class="countdown-num">${month}</` + 'span><span class="countdown-month"><' + '/span>'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span><span class="countdown-month"><' + '/span>'; }
if (day > 0) { result += `<span class="countdown-num">${day}</` + 'span>天'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>天'; }
if(year == 0 && month == 0) {
if (hour > 0) { result += `<span class="countdown-num">${hour}</` + 'span><span class="countdown-hour"><' + '/span>'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span><span class="countdown-hour"><' + '/span>'; }
if(day == 0) {
if (minute > 0) { result += `<span class="countdown-num">${minute}</` + 'span>分'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>分'; }
if (second > 0) { result += `<span class="countdown-num">${second}</` + 'span>秒'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>秒'; }
}
if (hour < 0) {}
if(isBefore) day--;{
var countdown = before.querySelector( hour +='.countdown' 24);
if(countdown) { countdown.innerHTML = result; }
ifbefore.style.display (day= < 0) {"";
after.style.display = month--"none";
}
if (monthsHave31Days.includes((isBefore ? then : now).month())) { day += 31; }
else if ((isBefore ? then : now).month() === 1) {
var countdown = after.querySelector( '.countdown' );
if ((isBefore ? then : now).year() % 4 === 0) { day += 29; }
elseif(countdown) { daycountdown.innerHTML += 28result; }
after.style.display = }"";
before.style.display else { day += 30"none"; }
}
var textMonth = ele.querySelector( if ('.countdown-month' < 0) {,
textHour = ele.querySelector( '.countdown-hour' year--);
if(textMonth) { textMonth.textContent = i18n.substring(2); }
month += 12;
if(textHour) { textHour.textContent = i18n.substring(0,2); }
}
};
if (year > 0) { result += `<span class="countdown-num">${year}</` + 'span>年'; }
const run = () => {
if (month > 0) { result += `<span class="countdown-num">${month}</` + 'span><span class="countdown-month"><' + '/span>'; }
document.querySelectorAll( '.countdownNode:not(.disabled)' ).forEach((ele) => {
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span><span class="countdown-month"><' + '/span>'; }
if(ele.title) {
if (day > 0) { result += `<span class="countdown-num">${day}</` + 'span>天'; }
ele.dataset.title = ele.title;
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>天'; }
if(year == 0 && month == 0ele.removeAttribute('title') {;
if (hour > 0) { result += `<span class="countdown-num">${hour}</` + 'span><span class="countdown-hour"><' + '/span>'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span><span class="countdown-hour"><' + '/span>'; }
if(day == 0) {
if (minute > 0) { result += `<span class="countdown-num">${minute}</` + 'span>分'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>分'; }
if (second > 0) { result += `<span class="countdown-num">${second}</` + 'span>秒'; }
else if (result !== "") { result += `<span class="countdown-num">${0}</` + 'span>秒'; }
}
}
if(isBefore) {
var countdown = before.querySelector( '.countdown' );
if(countdown) { countdown.innerHTML = result; }
before.style.display = "";
after.style.display = "none";
}
else {
var countdown = after.querySelector( '.countdown' );
if(countdown) { countdown.innerHTML = result; }
after.style.display = "";
before.style.display = "none";
}
var textMonth = ele.querySelector( '.countdown-month' ),
textHour = ele.querySelector( '.countdown-hour' );
if(textMonth) { textMonth.textContent = i18n.substring(2); }
if(textHour) { textHour.textContent = i18n.substring(0,2); }
};
const run = () => {
document.querySelectorAll( '.countdownNode:not(.disabled)' ).forEach((ele) => {
if(ele.title) {
ele.dataset.title = ele.title;
ele.removeAttribute('title');
}
fromNow(ele);
ele.style.visibility = "visible";
});
};
document.querySelectorAll( '.countdownNode' ).forEach((ele) => {
const time = moment(ele.dataset.target);
if (!time || !time.isValid()) {
ele.classList.add("error", "disabled")
ele.textContent = "(时间格式错误!)";
ele.style.visibility = "visible";
}
fromNow(ele);
ele.style.visibility = "visible";
});
run()};
document.querySelectorAll( '.countdownNode' ).forEach((ele) => {
window.setInterval(run, 1000);
const time = moment(ele.dataset.target);
if (!time || !time.isValid()) {
ele.classList.add("error", "disabled")
ele.textContent = "(时间格式错误!)";
ele.style.visibility = "visible";
}
});
run();
window.setInterval(run, 1000);
});
//</pre>
16,874

个编辑