MediaWiki:Gadget-RightToolbar.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
小 功能优化 |
小 功能增强v2 |
||
| 第1行: | 第1行: | ||
/* ================================================================ |
/* ================================================================ |
||
MediaWiki Gadget: Custom Tools |
MediaWiki Gadget: Custom Tools Bar(多语言时间显示版) |
||
功能: |
功能: |
||
- 回到顶部 / 回到底部按钮 |
- 回到顶部 / 回到底部按钮 |
||
- |
- 实时时钟(显示浏览器时区 + UTC) |
||
- 根据用户语言切换日期时间格式 |
|||
- 深色模式适配 |
- 深色模式自动适配 |
||
- 动态显示按钮 + 平滑动画 |
- 动态显示按钮 + 平滑动画 |
||
================================================================= */ |
================================================================= */ |
||
mw.loader.using(['mediawiki.util'], function () { |
mw.loader.using(['mediawiki.util', 'mediawiki.user'], function () { |
||
function updateClock() { |
function updateClock(userLang) { |
||
var now = new Date(); |
var now = new Date(); |
||
// |
// 使用 Intl.DateTimeFormat 根据用户语言格式化 |
||
var |
var localFormatter = new Intl.DateTimeFormat(userLang, { |
||
year: 'numeric', month: '2-digit', day: '2-digit', |
|||
| ⚫ | |||
hour12: false, timeZoneName: 'short' |
|||
| ⚫ | |||
var localStr = localFormatter.format(now); |
|||
// |
// UTC 格式 |
||
var |
var utcFormatter = new Intl.DateTimeFormat('en-US', { |
||
year: 'numeric', |
year: 'numeric', month: '2-digit', day: '2-digit', |
||
hour: '2-digit', minute: '2-digit', second: '2-digit', |
|||
hour12: false, timeZone: 'UTC', timeZoneName: 'short' |
|||
| ⚫ | |||
var timeString = now.toLocaleTimeString(userLang, { |
|||
hour: '2-digit', |
|||
| ⚫ | |||
second: '2-digit' |
|||
}); |
}); |
||
var utcStr = utcFormatter.format(now); |
|||
$('#current- |
$('#current-local').text(localStr); |
||
$('#current- |
$('#current-utc').text(utcStr); |
||
} |
} |
||
$(function () { |
$(function () { |
||
var isMobile = window.innerWidth <= 768; |
var isMobile = window.innerWidth <= 768; |
||
/* ---------- 获取用户语言 ---------- */ |
|||
var userLang = mw.config.get('wgUserLanguage') || 'en'; |
|||
/* ---------- 深色模式检测 ---------- */ |
/* ---------- 深色模式检测 ---------- */ |
||
| 第76行: | 第81行: | ||
} |
} |
||
/* ---------- 按钮 ---------- */ |
/* ---------- 回到顶部 / 底部按钮 ---------- */ |
||
var $topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () { |
var $topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () { |
||
$('html, body').animate({ scrollTop: 0 }, { duration: 500, easing: 'swing' }); |
$('html, body').animate({ scrollTop: 0 }, { duration: 500, easing: 'swing' }); |
||
}); |
}); |
||
var $bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () { |
var $bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () { |
||
$('html, body').animate({ scrollTop: $(document).height() }, { duration: 500, easing: 'swing' }); |
$('html, body').animate({ scrollTop: $(document).height() }, { duration: 500, easing: 'swing' }); |
||
}); |
}); |
||
/* ---------- 时钟(桌面端) ---------- */ |
/* ---------- 时钟区域(桌面端) ---------- */ |
||
if (!isMobile) { |
if (!isMobile) { |
||
var $clockDiv = $('<div id="current-clock"></div>').css({ |
var $clockDiv = $('<div id="current-clock"></div>').css({ |
||
| 第99行: | 第103行: | ||
backdropFilter: 'blur(4px)' |
backdropFilter: 'blur(4px)' |
||
}).append( |
}).append( |
||
$('<div id="current- |
$('<div id="current-local">加载中...</div>'), |
||
$('<div id="current- |
$('<div id="current-utc">加载中...</div>') |
||
); |
); |
||
$container.append($clockDiv); |
$container.append($clockDiv); |
||
| 第108行: | 第112行: | ||
$container.append($topButton, $bottomButton); |
$container.append($topButton, $bottomButton); |
||
/* ---------- 初次进入动画 ---------- */ |
/* ---------- 初次进入的动画效果 ---------- */ |
||
$container.animate({ opacity: 1, transform: 'translateY(0)' }, 600); |
$container.animate({ opacity: 1, transform: 'translateY(0)' }, 600); |
||
/* ---------- 时钟定时更新 ---------- */ |
/* ---------- 时钟定时更新 ---------- */ |
||
if (!isMobile) { |
if (!isMobile) { |
||
updateClock(); |
updateClock(userLang); |
||
setInterval(updateClock, 1000); |
setInterval(function () { updateClock(userLang); }, 1000); |
||
} |
} |
||
/* ---------- 滚动动态控制按钮显示 ---------- */ |
/* ---------- 滚动动态控制按钮显示 ---------- */ |
||
$topButton.hide(); |
$topButton.hide(); |
||
$(window).on('scroll', function () { |
$(window).on('scroll', function () { |
||
var scrollTop = $(this).scrollTop(); |
var scrollTop = $(this).scrollTop(); |
||
| 第127行: | 第131行: | ||
} |
} |
||
}); |
}); |
||
}); |
}); |
||
}); |
}); |
||
2025年11月11日 (二) 00:17的版本
/* ================================================================
MediaWiki Gadget: Custom Tools Bar(多语言时间显示版)
功能:
- 回到顶部 / 回到底部按钮
- 实时时钟(显示浏览器时区 + UTC)
- 根据用户语言切换日期时间格式
- 深色模式自动适配
- 动态显示按钮 + 平滑动画
================================================================= */
mw.loader.using(['mediawiki.util', 'mediawiki.user'], function () {
function updateClock(userLang) {
var now = new Date();
// 使用 Intl.DateTimeFormat 根据用户语言格式化
var localFormatter = new Intl.DateTimeFormat(userLang, {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit',
hour12: false, timeZoneName: 'short'
});
var localStr = localFormatter.format(now);
// UTC 格式
var utcFormatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit',
hour12: false, timeZone: 'UTC', timeZoneName: 'short'
});
var utcStr = utcFormatter.format(now);
$('#current-local').text(localStr);
$('#current-utc').text(utcStr);
}
$(function () {
var isMobile = window.innerWidth <= 768;
/* ---------- 获取用户语言 ---------- */
var userLang = mw.config.get('wgUserLanguage') || 'en';
/* ---------- 深色模式检测 ---------- */
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const bg = isDark ? 'rgba(40,40,40,0.85)' : 'rgba(255,255,255,0.85)';
const border = isDark ? '#555' : '#aaa';
const color = isDark ? '#eee' : '#333';
/* ---------- 工具栏容器 ---------- */
var $container = $('<div id="mw-custom-tools"></div>').css({
position: 'fixed',
bottom: '120px',
right: '50px',
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-end',
gap: '5px',
zIndex: 9999,
opacity: 0,
transform: 'translateY(20px)',
}).appendTo('body');
/* ---------- 按钮生成函数 ---------- */
function createButton(id, text, tooltip, clickHandler) {
return $('<div></div>', { id: id, title: tooltip, text: text }).css({
padding: '5px 10px',
fontSize: '12px',
background: bg,
border: '1px solid ' + border,
borderRadius: '4px',
boxShadow: '0 1px 3px rgba(0,0,0,0.2)',
color: color,
cursor: 'pointer',
textAlign: 'center',
opacity: 0.7,
transition: 'background 0.3s, opacity 0.3s, transform 0.3s',
backdropFilter: 'blur(4px)'
}).hover(
function () { $(this).css({ background: isDark ? '#555' : '#ddd', opacity: 1 }); },
function () { $(this).css({ background: bg, opacity: 0.7 }); }
).click(clickHandler);
}
/* ---------- 回到顶部 / 底部按钮 ---------- */
var $topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () {
$('html, body').animate({ scrollTop: 0 }, { duration: 500, easing: 'swing' });
});
var $bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () {
$('html, body').animate({ scrollTop: $(document).height() }, { duration: 500, easing: 'swing' });
});
/* ---------- 时钟区域(桌面端) ---------- */
if (!isMobile) {
var $clockDiv = $('<div id="current-clock"></div>').css({
padding: '6px 12px',
fontSize: '12px',
background: bg,
border: '1px solid ' + border,
borderRadius: '4px',
boxShadow: '0 1px 3px rgba(0,0,0,0.2)',
color: color,
textAlign: 'center',
lineHeight: '1.4',
backdropFilter: 'blur(4px)'
}).append(
$('<div id="current-local">加载中...</div>'),
$('<div id="current-utc">加载中...</div>')
);
$container.append($clockDiv);
}
/* ---------- 组装按钮 ---------- */
$container.append($topButton, $bottomButton);
/* ---------- 初次进入的动画效果 ---------- */
$container.animate({ opacity: 1, transform: 'translateY(0)' }, 600);
/* ---------- 时钟定时更新 ---------- */
if (!isMobile) {
updateClock(userLang);
setInterval(function () { updateClock(userLang); }, 1000);
}
/* ---------- 滚动动态控制按钮显示 ---------- */
$topButton.hide();
$(window).on('scroll', function () {
var scrollTop = $(this).scrollTop();
if (scrollTop > 200) {
$topButton.fadeIn();
} else {
$topButton.fadeOut();
}
});
});
});