LLWiki正在建设中,欢迎加入我们

“User:Bhsd/widget/countdown.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
第2行: 第2行:
// 用于[[Widget:Countdown]],可以使用ES6语法
// 用于[[Widget:Countdown]],可以使用ES6语法
"use strict";
"use strict";
/*global mw, $, moment, wgUCS*/
(function() {
(function() {
const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
function fromNow(ele, mw) {
const now = new Date(),
fromNow = function(ele) {
then = new Date( ele.title ),
const now = moment(),
before = ele.children[0],
then = ele.data('target'),
after = ele.children[1],
before = ele.children().first(),
isBefore = (then < now),
after = ele.children().last(),
monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
isBefore = then.isBefore( now ),
lang = mw.config.get( 'wgUserVariant' ),
early = isBefore ? then : now,
isHant = ['zh-hant', 'zh-tw', 'zh-hk', 'zh-mo'].includes( lang ),
textMonth = wgUCS('个月', '個月'),
textMonth = isHant ? '個月' : '个月',
textHour = wgUCS('小时', '小時');
textHour = isHant ? '小時' : '小时';
let year = isBefore ? now.year() - then.year() : then.year() - now.year(),
let year = isBefore ? now.getFullYear() - then.getFullYear() : then.getFullYear() - now.getFullYear(),
month = isBefore ? now.month() - then.month() : then.month() - now.month(),
month = isBefore ? now.getMonth() - then.getMonth() : then.getMonth() - now.getMonth(),
day = isBefore ? now.date() - then.date() : then.date() - now.date(),
day = isBefore ? now.getDate() - then.getDate() : then.getDate() - now.getDate(),
hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
hour = isBefore ? now.getHours() - then.getHours() : then.getHours() - now.getHours(),
minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
minute = isBefore ? now.getMinutes() - then.getMinutes() : then.getMinutes() - now.getMinutes(),
second = isBefore ? now.second() - then.second() : then.second() - now.second(),
second = isBefore ? now.getSeconds() - then.getSeconds() : then.getSeconds() - now.getSeconds(),
result = "";
result = "";
if (second < 0) {
if (second < 0) {
第35行: 第35行:
if (day < 0) {
if (day < 0) {
month--;
month--;
if (monthsHave31Days.includes( (isBefore ? then : now).getMonth() )) { day += 31; }
if (early.month() === 1) { day += early.year() % 4 === 0 ? 29 : 28; }
else if ((isBefore ? then : now).getMonth() === 1) {
else { day += monthsHave31Days.includes( early.month() ) ? 31 : 30; }
if ((isBefore ? then : now).getFullYear() % 4 === 0) { day += 29; }
else { day += 28; }
}
else { day += 30; }
}
}
if (month < 0) {
if (month < 0) {
第47行: 第43行:
}
}
if (year > 0) { result += `<span class="countdown-num">${year}</span>年`; }
if (year > 0) { result += `<span class="countdown-num">${year}</span>年`; }
if (month > 0 || result !== "") { result += `<span class="countdown-num">${month}</span>${textMonth}`; }
if (month > 0) { result += `<span class="countdown-num">${month}</span>${textMonth}`; }
if (day > 0 || result !== "") { result += `<span class="countdown-num">${day}</span>天`; }
if (year === 0 && day > 0) { result += `<span class="countdown-num">${day}</span>天`; }
if (year === 0 && month === 0) {
if (year === 0 && month === 0) {
if (hour > 0 || result !== "") { result += `<span class="countdown-num">${hour}</span>${textHour}`; }
if (hour > 0) { result += `<span class="countdown-num">${hour}</span>${textHour}`; }
if (day === 0) {
if (day === 0) {
if (minute > 0 || result !== "") { result += `<span class="countdown-num">${minute}</span>分`; }
if (minute > 0 || hour > 0) { result += `<span class="countdown-num">${minute}</span>分`; }
if (second > 0 || result !== "") { result += `<span class="countdown-num">${second}</span>秒`; }
result += `<span class="countdown-num">${second}</span>秒`;
}
}
}
}
if (isBefore) {
ele.toggleCLass('isBefore', isBefore);
const countdown = before.querySelector( '.countdown' );
if (isBefore) { before.find( '.countdown' ).html( result ); }
if (countdown) { countdown.innerHTML = result; }
else { after.find( '.countdown' ).html( result ); }
},
before.style.display = "";
run = function() {
after.style.display = "none";
mw.loader.using( 'ext.gadget.site-lib' ).then(() => {
}
$( '.counting' ).each(function() { fromNow( $(this) ); });
else {
const countdown = after.querySelector( '.countdown' );
if (countdown) { countdown.innerHTML = result; }
after.style.display = "";
before.style.display = "none";
}
}
function run(mw) {
document.querySelectorAll( '.countdownNode' ).forEach(ele => {
fromNow(ele, mw);
ele.classList.add( 'counting' );
});
});
}
},
main = function($content) {

document.querySelectorAll( '.countdownNode' ).forEach(ele => {
if ($content.find( '.countdownNode' ).length === 0) { return; }
mw.loader.using( 'moment' ).then(() => {
// 补齐2位数日期
$content.find( '.countdownNode' ).each(function() {
const target = ele.title.replace( /-(\d)(?=\D)/g, '-0$1' ).replace( /-(\d)$/, '-0$1' ),
time = new Date(target);
const ele = $(this),
then = moment(this.title, 'YYYY-MM-DD HH:mm Z');
if (!time.getTime()) {
ele.classList.add( "error" );
if (then.isValid()) { ele.data('target', then).addClass( 'counting' ); }
ele.textContent = "(时间格式错误!)";
else { ele.toggleClass('error countdownNode').text( "(时间格式错误!)" ); }
ele.classList.remove( 'countdownNode' );
});
if ($content.find( '.counting' ).length === 0) { return; }
}
console.log('Hook: wikipage.content,开始处理新增的倒计时');
else { ele.title = target; }
});
run();
// 新節點至少要執行過一次run()後才能移除countdownNode類
console.log( 'setInterval: 等待mediaWiki加载完毕' );
$content.find( '.counting' ).removeClass( 'countdownNode' );
mw.countdown = mw.countdown || setInterval(() => { run(); }, 1000 );
});
};
console.log( 'setInterval: 等待jQuery加载完毕' );
const timerStart = Date.now(),
const timerStart = Date.now(),
timer = setInterval(() => {
timer = setInterval(() => {
if (!window.mediaWiki) { return; }
if (!window.jQuery) { return; }
clearInterval(timer);
clearInterval(timer);
console.log(`End setInterval: mediaWiki加载完毕,用时 ${Date.now() - timerStart} ms`);
console.log(`End setInterval: jQuery加载完毕,用时 ${Date.now() - timerStart} ms`);
mw.hook( 'wikipage.content' ).add($content => { main($content); });
run(window.mediaWiki);
}, 500);
setInterval(() => { run(window.mediaWiki); }, 1000 );
}, 100);
}) ();
}) ();
//</nowiki>
//</nowiki>
// [[category:JavaScript小部件]] {{DEFAULTSORT:Countdown}}
// [[category:jQuery小部件]] {{DEFAULTSORT:Countdown}}

2020年12月5日 (六) 04:36的版本

//<nowiki>
// 用于[[Widget:Countdown]],可以使用ES6语法
"use strict";
/*global mw, $, moment, wgUCS*/
(function() {
    const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11], // 月份从0开始
        fromNow = function(ele) {
        const now = moment(),
            then = ele.data('target'),
            before = ele.children().first(),
            after = ele.children().last(),
            isBefore = then.isBefore( now ),
            early = isBefore ? then : now,
            textMonth = wgUCS('个月', '個月'),
            textHour = wgUCS('小时', '小時');
        let year = isBefore ? now.year() - then.year() : then.year() - now.year(),
            month = isBefore ? now.month() - then.month() : then.month() - now.month(),
            day = isBefore ? now.date() - then.date() : then.date() - now.date(),
            hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
            minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
            second = isBefore ? now.second() - then.second() : then.second() - now.second(),
            result = "";
        if (second < 0) {
            minute--;
            second += 60;
        }
        if (minute < 0) {
            hour--;
            minute += 60;
        }
        if (hour < 0) {
            day--;
            hour += 24;
        }
        if (day < 0) {
            month--;
            if (early.month() === 1) { day += early.year() % 4 === 0 ? 29 : 28; }
            else { day += monthsHave31Days.includes( early.month() ) ? 31 : 30; }
        }
        if (month < 0) {
            year--;
            month += 12;
        }
        if (year > 0) { result += `<span class="countdown-num">${year}</span>年`; }
        if (month > 0) { result += `<span class="countdown-num">${month}</span>${textMonth}`; }
        if (year === 0 && day > 0) { result += `<span class="countdown-num">${day}</span>天`; }
        if (year === 0 && month === 0) {
            if (hour > 0) { result += `<span class="countdown-num">${hour}</span>${textHour}`; }
            if (day === 0) {
                if (minute > 0 || hour > 0) { result += `<span class="countdown-num">${minute}</span>分`; }
                result += `<span class="countdown-num">${second}</span>秒`;
            }
        }
        ele.toggleCLass('isBefore', isBefore);
        if (isBefore) { before.find( '.countdown' ).html( result ); }
        else { after.find( '.countdown' ).html( result ); }
    },
        run = function() {
        mw.loader.using( 'ext.gadget.site-lib' ).then(() => {
            $( '.counting' ).each(function() { fromNow( $(this) ); });
        });
    },
        main = function($content) {
        if ($content.find( '.countdownNode' ).length === 0) { return; }
        mw.loader.using( 'moment' ).then(() => {
            $content.find( '.countdownNode' ).each(function() {
                const ele = $(this),
                    then = moment(this.title, 'YYYY-MM-DD HH:mm Z');
                if (then.isValid()) { ele.data('target', then).addClass( 'counting' ); }
                else { ele.toggleClass('error countdownNode').text( "(时间格式错误!)" ); }
            });
            if ($content.find( '.counting' ).length === 0) { return; }
            console.log('Hook: wikipage.content,开始处理新增的倒计时');
            run();
            // 新節點至少要執行過一次run()後才能移除countdownNode類
            $content.find( '.counting' ).removeClass( 'countdownNode' );
            mw.countdown = mw.countdown || setInterval(() => { run(); }, 1000 );
        });
    };
    console.log( 'setInterval: 等待jQuery加载完毕' );
    const timerStart = Date.now(),
        timer = setInterval(() => {
        if (!window.jQuery) { return; }
        clearInterval(timer);
        console.log(`End setInterval: jQuery加载完毕,用时 ${Date.now() - timerStart} ms`);
        mw.hook( 'wikipage.content' ).add($content => { main($content); });
    }, 500);
}) ();
//</nowiki>
// [[category:jQuery小部件]] {{DEFAULTSORT:Countdown}}