MediaWiki:Gadget-RightToolbar.js:修订间差异
删除的内容 添加的内容
小 修改bug |
小 切换优化 |
||
第1行:
/* ================================================================
MediaWiki Gadget:
功能:
- 显示浏览器本地时间和 UTC 时间
- 使用 MediaWiki 用户语言格式
- 动态响应语言切换,无需刷新
-
作者:你自己
================================================================= */
mw.loader.using(['mediawiki.util
// 工具栏主函数
function initClockBar() {
var isMobile = window.innerWidth <= 768;
/
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const bg = isDark ? 'rgba(40,40,40,0.85)' : 'rgba(255,255,255,0.85)';
第47行 ⟶ 第21行:
const color = isDark ? '#eee' : '#333';
/
var $container = $('
if (!$container.length) {
transform: 'translateY(20px)', // 初始位置
}).appendTo('body');
}
/
function createButton(id, text, tooltip, clickHandler) {
return $('<div></div>', { id: id, title: tooltip, text: text }).css({
第82行 ⟶ 第59行:
}
/
var $topButton =
if (!$topButton.length) {
$topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () {
$('html, body').animate({ scrollTop: 0 }, { duration: 500, easing: 'swing' });
});
$container.append($topButton);
}
var $bottomButton =
if (!$bottomButton.length) {
$bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () {
$('html, body').animate({ scrollTop: $(document).height() }, { duration: 500, easing: 'swing' });
});
$container.append($bottomButton);
}
/
if (!isMobile &&
$clockDiv = $('<div id="current-clock"></div>').css({
padding: '6px 12px',
fontSize: '12px',
第105行 ⟶ 第91行:
backdropFilter: 'blur(4px)'
}).append(
$('<div id="
$('<div id="
$('<div id="utc-time"></div>')
);
$container.append($clockDiv);
}
/
$topButton.hide(); // 初始隐藏
$(window).on('scroll', function () {
var scrollTop = $(this).scrollTop();
if (scrollTop > 200) $topButton.fadeIn(); else $topButton.fadeOut();
});
/
$container.animate({ opacity: 1, transform: 'translateY(0)' }, 600);
/
// 获取 MediaWiki 用户语言(wgUserLanguage)
var userLang = mw.config.get('wgUserLanguage') || 'en';
// 本地时间
var localFormatter = new Intl.DateTimeFormat(userLang, {
year: 'numeric', month: 'long', day: 'numeric',
hour: '2-digit', minute: '2-digit', second: '2-digit',
hour12: false,
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
});
var localParts = localFormatter.formatToParts(now);
var localDateStr = localParts.filter(p => p.type === 'year' || p.type === 'month' || p.type === 'day')
.map(p => p.value).join(' ');
var localTimeStr = localParts.filter(p => p.type === 'hour' || p.type === 'minute' || p.type === 'second')
.map(p => p.value).join(':');
$('#local-date').text('本地日期: ' + localDateStr);
$('#local-time').text('本地时间: ' + localTimeStr);
// UTC 时间
var utcFormatter = new Intl.DateTimeFormat(userLang, {
year: 'numeric', month: 'long', day: 'numeric',
hour: '2-digit', minute: '2-digit', second: '2-digit',
hour12: false,
timeZone: 'UTC'
});
var utcParts = utcFormatter.formatToParts(now);
var utcDateStr = utcParts.filter(p => p.type === 'year' || p.type === 'month' || p.type === 'day')
.map(p => p.value).join(' ');
var utcTimeStr = utcParts.filter(p => p.type === 'hour' || p.type === 'minute' || p.type === 'second')
.map(p => p.value).join(':');
$('#utc-time').text('UTC 时间: ' + utcDateStr + ' ' + utcTimeStr);
}
/
setInterval(renderClock, 1000);
renderClock(); // 启动立即渲染
// ---------- 语言切换动态刷新 ----------
// MediaWiki 用户语言修改后触发 user.languageChange hook
mw.hook('user.languageChange').add(renderClock);
}
// 初始化工具条
});
| |||