打开/关闭菜单
6629
8
8126
OGAS数据中枢
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Widget:Countdown:修订间差异

来自OGAS数据中枢
imported>弃权者
创建页面,内容为“<noinclude>仅供{{tl|Countdown}}使用。</noinclude><includeonly><!--{if !isset($wgCountdown) || !$wgCountdown}--><!--{assign var="wgCountdown" value=true scope="global"}--><script> "use strict"; window.RLQ = window.RLQ || []; window.RLQ.push(async () => { await mw.loader.using("moment"); const fromNow = (then, before, after, on) => { const now = moment(); const isBefore = then.isBefore(now); const monthsHave31Days = [0, 2, 4,…”
 
imported>弃权者
无编辑摘要
第1行: 第1行:
<noinclude>仅供{{tl|Countdown}}使用。</noinclude><includeonly><!--{if !isset($wgCountdown) || !$wgCountdown}--><!--{assign var="wgCountdown" value=true scope="global"}--><script>
<noinclude>仅供{{tl|Countdown}}使用。</noinclude><includeonly><!--{if !isset($wgCountdown) || !$wgCountdown}--><!--{assign var="wgCountdown" value=true scope="global"}--><script>
"use strict";
"use strict";
window.RLQ = window.RLQ || [];
(function() {
window.RLQ.push(async () => {
    if (typeof moment !== 'undefined') {
     await mw.loader.using("moment");
        initCountdown();
     const fromNow = (then, before, after, on) => {
        return;
        const now = moment();
    }
        const isBefore = then.isBefore(now);
   
        const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11]; // 月份从0开始
    const script = document.createElement('script');
        let year = isBefore ? now.year() - then.year() : then.year() - now.year(),
    script.src = 'https://cdn.staticfile.org/moment.js/2.29.4/moment.min.js';
            month = isBefore ? now.month() - then.month() : then.month() - now.month(),
    script.onload = initCountdown;
            day = isBefore ? now.date() - then.date() : then.date() - now.date(),
    script.onerror = () => {
            hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
        console.error('无法加载 moment.js,倒计时功能不可用');
            minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
        document.querySelectorAll('.countdownNode').forEach(el => {
            second = isBefore ? now.second() - then.second() : then.second() - now.second();
            el.textContent = '(无法加载时间库)';
        if (second < 0) {
            el.classList.add('error');
            minute--;
        });
            second += 60;
    };
        }
     document.head.appendChild(script);
        if (minute < 0) {
      
            hour--;
    function initCountdown() {
            minute += 60;
        const fromNow = (then, before, after, on) => {
        }
            const now = moment();
        if (hour < 0) {
            const isBefore = then.isBefore(now);
            day--;
            const monthsHave31Days = [0, 2, 4, 6, 7, 9, 11];
            hour += 24;
            let year = isBefore ? now.year() - then.year() : then.year() - now.year(),
        }
                month = isBefore ? now.month() - then.month() : then.month() - now.month(),
        if (day < 0) {
                day = isBefore ? now.date() - then.date() : then.date() - now.date(),
            month--;
                hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
            if (monthsHave31Days.includes((isBefore ? then : now).month())) {
                minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
                day += 31;
                second = isBefore ? now.second() - then.second() : then.second() - now.second();
            } else if ((isBefore ? then : now).month() === 1) {
            if (second < 0) {
                if ((isBefore ? then : now).year() % 4 === 0) {
                minute--;
                     day += 29;
                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;
                    }
                 } else {
                 } else {
                     day += 28;
                     day += 30;
                 }
                 }
            } else {
                day += 30;
             }
             }
        }
            if (month < 0) {
        if (month < 0) {
                year--;
            year--;
                month += 12;
            month += 12;
            }
        }
            let result = "";
        let result = "";
            if (year > 0) {
        if (year > 0) {
                result += `${year}年`;
            result += `${year}年`;
            }
        }
            if (month > 0) {
        if (month > 0) {
                result += `${month}月`;
            result += `${month}月`;
            } else if (result !== "") {
        } else if (result !== "") {
                result += `${0}月`;
            result += `${0}月`;
            }
        }
            if (day > 0) {
        if (day > 0) {
                result += `${day}日`;
            result += `${day}日`;
            } else if (result !== "") {
        } else if (result !== "") {
                result += `${0}日`;
            result += `${0}日`;
            }
        }
            if (hour > 0) {
        if (hour > 0) {
                result += `${hour}小时`;
            result += `${hour}小时`;
            } else if (result !== "") {
        } else if (result !== "") {
                result += `${0}小时`;
            result += `${0}小时`;
            }
        }
            if (minute > 0) {
        if (minute > 0) {
                result += `${minute}分`;
            result += `${minute}分`;
            } else if (result !== "") {
        } else if (result !== "") {
                result += `${0}分`;
            result += `${0}分`;
            }
        }
            if (second > 0) {
        if (second > 0) {
                result += `${second}秒`;
            result += `${second}秒`;
            } else if (result !== "") {
        } else if (result !== "") {
                result += `${0}秒`;
            result += `${0}秒`;
            }
        }
            return (result === "" ? on : isBefore ? before : after).replace("$1", result.replace(/(\d) /g, "$1"));
        return (result === "" ? on : isBefore ? before : after).replace("$1", result.replace(/(\d) /g, "$1"));
        };
    };
       
    const run = () => {
        const run = () => {
        $(".countdownNode:not(.disabled)").each((_, ele) => {
            $(".countdownNode:not(.disabled)").each((_, ele) => {
            const self = $(ele);
                const self = $(ele);
            self.text(fromNow(self.data("target"), ele.dataset.before || "$1前", ele.dataset.after || "还剩$1", ele.dataset.on || "就是现在!"));
                self.text(fromNow(self.data("target"), ele.dataset.before || "$1前", ele.dataset.after || "还剩$1", ele.dataset.on || "就是现在!"));
            });
        };
       
        $(() => {
            $(".countdownNode").each((_, ele) => {
                const self = $(ele),
                    time = moment(ele.dataset.target);
                if (!time || !time.isValid()) {
                    self.addClass("error disabled").text("(发生了致命错误!)");
                    return;
                }
                self.data("target", time);
            });
            run();
            window.setInterval(run, 1000);
         });
         });
     };
     }
    $(".countdownNode").each((_, ele) => {
})();
        const self = $(ele),
            time = moment(ele.dataset.target);
        if (!time || !time.isValid()) {
            self.addClass("error disabled").text("(发生了致命错误!)");
            return;
        }
        self.data("target", time);
    });
    run();
    window.setInterval(run, 1000);
});
</script><!--{/if}--></includeonly>
</script><!--{/if}--></includeonly>

2026年3月3日 (二) 08:34的版本

仅供{{Countdown}}使用。