Widget:Countdown:修订间差异
来自OGAS数据中枢
更多语言
更多操作
imported>弃权者 无编辑摘要 |
imported>弃权者 |
||
| 第2行: | 第2行: | ||
"use strict"; | "use strict"; | ||
(function() { | (function() { | ||
// 如果 moment 已经存在,直接初始化 | |||
if (typeof moment !== 'undefined') { | if (typeof moment !== 'undefined') { | ||
initCountdown(); | initCountdown(); | ||
| 第7行: | 第8行: | ||
} | } | ||
// 检查是否支持 mw.loader | |||
if (typeof mw !== 'undefined' && mw.loader && typeof mw.loader.using === 'function') { | if (typeof mw !== 'undefined' && mw.loader && typeof mw.loader.using === 'function') { | ||
mw.loader.using('moment').then(initCountdown).catch(loadFromCDN); | mw.loader.using('moment').then(initCountdown).catch(loadFromCDN); | ||
} else { | } else { | ||
// mw.loader 不可用,直接从 CDN 加载 | |||
loadFromCDN(); | loadFromCDN(); | ||
} | } | ||
function loadFromCDN() { | function loadFromCDN() { | ||
// 国内 CDN 备选列表 | |||
var cdnList = [ | var cdnList = [ | ||
'https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.4/moment.min.js', | |||
'https://cdn.staticfile.org/moment.js/2.29.4/moment.min.js', | |||
'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js' | 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js' | ||
]; | ]; | ||
tryLoadCDN(); | tryLoadCDN(0); | ||
function tryLoadCDN() { | function tryLoadCDN(index) { | ||
if (index >= cdnList.length) { | |||
// 所有 CDN 都失败 | |||
document.querySelectorAll('.countdownNode').forEach(el => { | |||
el.textContent = '(无法加载时间库)'; | |||
el.classList.add('error'); | |||
}); | |||
return; | |||
} | |||
var script = document.createElement('script'); | var script = document.createElement('script'); | ||
script.src = | script.src = cdnList[index]; | ||
script.onload = initCountdown; | script.onload = initCountdown; | ||
script.onerror = function() { | script.onerror = function() { | ||
tryLoadCDN(index + 1); // 尝试下一个 CDN | |||
}; | }; | ||
document.head.appendChild(script); | document.head.appendChild(script); | ||
| 第32行: | 第47行: | ||
function initCountdown() { | function initCountdown() { | ||
// 确保 DOM 加载完成 | |||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener('DOMContentLoaded', setupCountdown); | document.addEventListener('DOMContentLoaded', setupCountdown); | ||
| 第114行: | 第130行: | ||
const run = () => { | const run = () => { | ||
if (typeof jQuery === 'undefined') { | if (typeof jQuery === 'undefined') { | ||
// 如果没有 jQuery,使用原生方法 | |||
document.querySelectorAll(".countdownNode:not(.disabled)").forEach(ele => { | document.querySelectorAll(".countdownNode:not(.disabled)").forEach(ele => { | ||
const time = moment(ele.dataset.target); | const time = moment(ele.dataset.target); | ||
| 第121行: | 第138行: | ||
}); | }); | ||
} else { | } else { | ||
// 有 jQuery 就用 jQuery | |||
jQuery(".countdownNode:not(.disabled)").each((_, ele) => { | jQuery(".countdownNode:not(.disabled)").each((_, ele) => { | ||
const self = jQuery(ele); | const self = jQuery(ele); | ||
| 第128行: | 第146行: | ||
}; | }; | ||
// 初始化所有倒计时节点 | |||
if (typeof jQuery === 'undefined') { | if (typeof jQuery === 'undefined') { | ||
// 原生方式 | // 原生方式 | ||
| 第140行: | 第159行: | ||
}); | }); | ||
} else { | } else { | ||
// jQuery 方式 | |||
jQuery(".countdownNode").each((_, ele) => { | jQuery(".countdownNode").each((_, ele) => { | ||
const self = jQuery(ele), | const self = jQuery(ele), | ||
2026年3月4日 (三) 12:26的版本
仅供{{Countdown}}使用。