MediaWiki:Gadget-RightToolbar.js:修订间差异
MediaWiki界面页面
更多操作
删除的内容 添加的内容
小 修改bug |
Maintenance script(留言 | 贡献) 添加JavaScript代码 |
||
| (未显示2个用户的4个中间版本) | |||
| 第1行: | 第1行: | ||
/* ================================================================ |
/* ================================================================ |
||
MediaWiki Gadget: |
MediaWiki Gadget: Localized Clock Bar |
||
功能: |
功能: |
||
- 显示浏览器本地时间和 UTC 时间 |
|||
- 回到顶部 / 回到底部按钮 |
|||
- 使用 MediaWiki 用户语言格式 |
|||
- 本地化日期和时间显示(浏览器时区 + UTC) |
|||
- 深色模式 |
- 深色模式适配 + 滑入动画 |
||
- 动态显示按钮 + 平滑动画 |
|||
作者:你自己 |
|||
================================================================= */ |
================================================================= */ |
||
mw.loader.using(['mediawiki.util' |
mw.loader.using( [ 'mediawiki.util' ] ).then( function () { |
||
var STYLE_ID = 'mw-right-toolbar-style'; |
|||
function |
function ensureToolbarStyles() { |
||
if ( document.getElementById( STYLE_ID ) ) { |
|||
// 浏览器本地时间 |
|||
return; |
|||
} |
|||
// UTC 时间,从浏览器系统获取即可 |
|||
var utc = new Date(local.getTime() + local.getTimezoneOffset() * 60000); |
|||
var css = [ |
|||
'#mw-right-toolbar, .mw-right-toolbar {', |
|||
var userLang = mw.config.get('wgUserLanguage') || 'en'; |
|||
' position: fixed;', |
|||
' bottom: 96px;', |
|||
' right: 24px;', |
|||
' display: flex;', |
|||
' flex-direction: column;', |
|||
' align-items: flex-end;', |
|||
' gap: 6px;', |
|||
' z-index: 2147483647;', |
|||
' opacity: 0;', |
|||
' transform: translateY(12px);', |
|||
' transition: opacity 0.4s ease, transform 0.4s ease;', |
|||
'}', |
|||
'', |
|||
'.mw-right-toolbar.is-visible {', |
|||
' opacity: 1;', |
|||
' transform: translateY(0);', |
|||
'}', |
|||
'', |
|||
'.mw-right-toolbar .mw-rt-button,', |
|||
'.mw-right-toolbar .mw-rt-clock {', |
|||
' background: var(--color-surface-0, rgba(255, 255, 255, 0.85));', |
|||
' border: 1px solid var(--border-color-base, #aaa);', |
|||
' color: var(--color-base, #333);', |
|||
' border-radius: 4px;', |
|||
' box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);', |
|||
' font-size: 12px;', |
|||
' line-height: 1.4;', |
|||
' padding: 6px 12px;', |
|||
' backdrop-filter: blur(4px);', |
|||
'}', |
|||
'', |
|||
'.mw-right-toolbar .mw-rt-button {', |
|||
' cursor: pointer;', |
|||
' opacity: 0.8;', |
|||
' transition: background 0.2s ease, opacity 0.2s ease, transform 0.2s ease;', |
|||
'}', |
|||
'', |
|||
'.mw-right-toolbar .mw-rt-button:hover {', |
|||
' opacity: 1;', |
|||
' background: var(--background-color-progressive-subtle, #ddd);', |
|||
'}', |
|||
'', |
|||
'.mw-right-toolbar .mw-rt-button:active {', |
|||
' transform: translateY(1px);', |
|||
'}', |
|||
'', |
|||
'.skin-theme-clientpref-night .mw-right-toolbar .mw-rt-button,', |
|||
'.skin-theme-clientpref-night .mw-right-toolbar .mw-rt-clock {', |
|||
' background: rgba(30, 30, 30, 0.85);', |
|||
' border-color: #555;', |
|||
' color: #eee;', |
|||
'}', |
|||
'', |
|||
'@media (max-width: 768px) {', |
|||
' #mw-right-toolbar, .mw-right-toolbar {', |
|||
' bottom: 72px;', |
|||
' right: 16px;', |
|||
' }', |
|||
'}' |
|||
].join( '\n' ); |
|||
var styleNode = mw.loader.addStyleTag( css ); |
|||
// 本地化格式化函数 |
|||
styleNode.id = STYLE_ID; |
|||
// dateStyle/timeStyle 采用 Intl.DateTimeFormat 可选参数,也可以用短/长格式 |
|||
var localString = new Intl.DateTimeFormat(userLang, { |
|||
year: 'numeric', month: 'long', day: 'numeric', |
|||
hour: '2-digit', minute: '2-digit', second: '2-digit', |
|||
hour12: false |
|||
}).format(local); |
|||
var utcString = new Intl.DateTimeFormat(userLang, { |
|||
year: 'numeric', month: 'long', day: 'numeric', |
|||
hour: '2-digit', minute: '2-digit', second: '2-digit', |
|||
hour12: false, timeZone: 'UTC' |
|||
}).format(utc); |
|||
$('#current-local-time').text(localString); |
|||
$('#current-utc-time').text(utcString); |
|||
} |
} |
||
function initClockBar() { |
|||
ensureToolbarStyles(); |
|||
var isMobile = window.innerWidth <= 768; |
var isMobile = window.innerWidth <= 768; |
||
var container = document.getElementById( 'mw-right-toolbar' ); |
|||
if ( !container ) { |
|||
container = document.createElement( 'div' ); |
|||
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches; |
|||
container.id = 'mw-right-toolbar'; |
|||
container.className = 'mw-right-toolbar'; |
|||
document.body.appendChild( container ); |
|||
const color = isDark ? '#eee' : '#333'; |
|||
} |
|||
function createButton( id, text, tooltip, clickHandler ) { |
|||
/* ---------- 工具栏容器 ---------- */ |
|||
var button = document.createElement( 'button' ); |
|||
var $container = $('<div id="mw-custom-tools"></div>').css({ |
|||
button.id = id; |
|||
button.className = 'mw-rt-button'; |
|||
button.title = tooltip; |
|||
button.textContent = text; |
|||
button.type = 'button'; |
|||
button.addEventListener( 'click', clickHandler ); |
|||
return button; |
|||
} |
|||
opacity: 0, |
|||
transform: 'translateY(20px)', |
|||
}).appendTo('body'); |
|||
var topButton = document.getElementById( 'mw-scroll-top' ); |
|||
/* ---------- 按钮生成函数 ---------- */ |
|||
if ( !topButton ) { |
|||
topButton = createButton( 'mw-scroll-top', '▲', '回到顶部', function () { |
|||
window.scrollTo( { |
|||
top: 0, |
|||
behavior: 'smooth' |
|||
} ); |
|||
} ); |
|||
container.appendChild( topButton ); |
|||
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 bottomButton = document.getElementById( 'mw-scroll-bottom' ); |
|||
/* ---------- 按钮 ---------- */ |
|||
if ( !bottomButton ) { |
|||
var $topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () { |
|||
bottomButton = createButton( 'mw-scroll-bottom', '▼', '回到底部', function () { |
|||
$('html, body').animate({ scrollTop: 0 }, { duration: 500, easing: 'swing' }); |
|||
window.scrollTo( { |
|||
top: document.body.scrollHeight, |
|||
behavior: 'smooth' |
|||
var $bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () { |
|||
} ); |
|||
$('html, body').animate({ scrollTop: $(document).height() }, { duration: 500, easing: 'swing' }); |
|||
}); |
} ); |
||
container.appendChild( bottomButton ); |
|||
} |
|||
var clockDiv = document.getElementById( 'mw-right-toolbar-clock' ); |
|||
/* ---------- 时钟(桌面端) ---------- */ |
|||
if (!isMobile) { |
if ( !isMobile && !clockDiv ) { |
||
clockDiv = document.createElement( 'div' ); |
|||
clockDiv.id = 'mw-right-toolbar-clock'; |
|||
clockDiv.className = 'mw-rt-clock'; |
|||
var localDateDiv = document.createElement( 'div' ); |
|||
localDateDiv.id = 'mw-local-date'; |
|||
localDateDiv.textContent = '加载中...'; |
|||
var localTimeDiv = document.createElement( 'div' ); |
|||
localTimeDiv.id = 'mw-local-time'; |
|||
var utcTimeDiv = document.createElement( 'div' ); |
|||
utcTimeDiv.id = 'mw-utc-time'; |
|||
$('<div id="current-utc-time">加载中...</div>') |
|||
); |
clockDiv.appendChild( localDateDiv ); |
||
clockDiv.appendChild( localTimeDiv ); |
|||
clockDiv.appendChild( utcTimeDiv ); |
|||
container.appendChild( clockDiv ); |
|||
} |
} |
||
topButton.style.display = 'none'; |
|||
/* ---------- 组装按钮 ---------- */ |
|||
var ticking = false; |
|||
$container.append($topButton, $bottomButton); |
|||
window.addEventListener( 'scroll', function () { |
|||
if ( ticking ) { |
|||
return; |
|||
} |
|||
ticking = true; |
|||
window.requestAnimationFrame( function () { |
|||
var scrollTop = window.pageYOffset || document.documentElement.scrollTop; |
|||
if ( scrollTop > 200 ) { |
|||
topButton.style.display = 'block'; |
|||
} else { |
|||
topButton.style.display = 'none'; |
|||
} |
|||
ticking = false; |
|||
} ); |
|||
} ); |
|||
window.requestAnimationFrame( function () { |
|||
/* ---------- 初次进入动画 ---------- */ |
|||
container.classList.add( 'is-visible' ); |
|||
$container.animate({ opacity: 1, transform: 'translateY(0)' }, 600); |
|||
} ); |
|||
function renderClock() { |
|||
/* ---------- 时钟定时更新 ---------- */ |
|||
var now = new Date(); |
|||
var userLang = mw.config.get( 'wgUserLanguage' ) || 'en'; |
|||
setInterval(updateClock, 1000); |
|||
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( function ( p ) { |
|||
return p.type === 'year' || p.type === 'month' || p.type === 'day'; |
|||
} ).map( function ( p ) { |
|||
return p.value; |
|||
} ).join( ' ' ); |
|||
var localTimeStr = localParts.filter( function ( p ) { |
|||
return p.type === 'hour' || p.type === 'minute' || p.type === 'second'; |
|||
} ).map( function ( p ) { |
|||
return p.value; |
|||
} ).join( ':' ); |
|||
var localDateEl = document.getElementById( 'mw-local-date' ); |
|||
var localTimeEl = document.getElementById( 'mw-local-time' ); |
|||
if ( localDateEl ) localDateEl.textContent = localDateStr; |
|||
if ( localTimeEl ) localTimeEl.textContent = localTimeStr; |
|||
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( function ( p ) { |
|||
return p.type === 'year' || p.type === 'month' || p.type === 'day'; |
|||
} ).map( function ( p ) { |
|||
return p.value; |
|||
} ).join( ' ' ); |
|||
var utcTimeStr = utcParts.filter( function ( p ) { |
|||
return p.type === 'hour' || p.type === 'minute' || p.type === 'second'; |
|||
} ).map( function ( p ) { |
|||
return p.value; |
|||
} ).join( ':' ); |
|||
var utcTimeEl = document.getElementById( 'mw-utc-time' ); |
|||
if ( utcTimeEl ) utcTimeEl.textContent = 'UTC: ' + utcDateStr + ' ' + utcTimeStr; |
|||
} |
} |
||
var clockTimer = window.setInterval( renderClock, 1000 ); |
|||
/* ---------- 滚动动态控制按钮显示 ---------- */ |
|||
renderClock(); |
|||
$(window).on('scroll', function () { |
|||
mw.hook( 'user.languageChange' ).add( renderClock ); |
|||
var scrollTop = $(this).scrollTop(); |
|||
if (scrollTop > 200) { |
|||
document.addEventListener( 'visibilitychange', function () { |
|||
$topButton.fadeIn(); |
|||
if ( document.hidden ) { |
|||
window.clearInterval( clockTimer ); |
|||
clockTimer = null; |
|||
return; |
|||
} |
} |
||
if ( !clockTimer ) { |
|||
clockTimer = window.setInterval( renderClock, 1000 ); |
|||
}); |
|||
renderClock(); |
|||
}); |
|||
} |
|||
} ); |
|||
} |
|||
// 页面加载完成后初始化 |
|||
if ( document.readyState === 'loading' ) { |
|||
document.addEventListener( 'DOMContentLoaded', initClockBar ); |
|||
} else { |
|||
initClockBar(); |
|||
} |
|||
} ); |
|||
2026年1月11日 (日) 12:10的最新版本
/* ================================================================
MediaWiki Gadget: Localized Clock Bar
功能:
- 显示浏览器本地时间和 UTC 时间
- 使用 MediaWiki 用户语言格式
- 深色模式适配 + 滑入动画
================================================================= */
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
var STYLE_ID = 'mw-right-toolbar-style';
function ensureToolbarStyles() {
if ( document.getElementById( STYLE_ID ) ) {
return;
}
var css = [
'#mw-right-toolbar, .mw-right-toolbar {',
' position: fixed;',
' bottom: 96px;',
' right: 24px;',
' display: flex;',
' flex-direction: column;',
' align-items: flex-end;',
' gap: 6px;',
' z-index: 2147483647;',
' opacity: 0;',
' transform: translateY(12px);',
' transition: opacity 0.4s ease, transform 0.4s ease;',
'}',
'',
'.mw-right-toolbar.is-visible {',
' opacity: 1;',
' transform: translateY(0);',
'}',
'',
'.mw-right-toolbar .mw-rt-button,',
'.mw-right-toolbar .mw-rt-clock {',
' background: var(--color-surface-0, rgba(255, 255, 255, 0.85));',
' border: 1px solid var(--border-color-base, #aaa);',
' color: var(--color-base, #333);',
' border-radius: 4px;',
' box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);',
' font-size: 12px;',
' line-height: 1.4;',
' padding: 6px 12px;',
' backdrop-filter: blur(4px);',
'}',
'',
'.mw-right-toolbar .mw-rt-button {',
' cursor: pointer;',
' opacity: 0.8;',
' transition: background 0.2s ease, opacity 0.2s ease, transform 0.2s ease;',
'}',
'',
'.mw-right-toolbar .mw-rt-button:hover {',
' opacity: 1;',
' background: var(--background-color-progressive-subtle, #ddd);',
'}',
'',
'.mw-right-toolbar .mw-rt-button:active {',
' transform: translateY(1px);',
'}',
'',
'.skin-theme-clientpref-night .mw-right-toolbar .mw-rt-button,',
'.skin-theme-clientpref-night .mw-right-toolbar .mw-rt-clock {',
' background: rgba(30, 30, 30, 0.85);',
' border-color: #555;',
' color: #eee;',
'}',
'',
'@media (max-width: 768px) {',
' #mw-right-toolbar, .mw-right-toolbar {',
' bottom: 72px;',
' right: 16px;',
' }',
'}'
].join( '\n' );
var styleNode = mw.loader.addStyleTag( css );
styleNode.id = STYLE_ID;
}
function initClockBar() {
ensureToolbarStyles();
var isMobile = window.innerWidth <= 768;
var container = document.getElementById( 'mw-right-toolbar' );
if ( !container ) {
container = document.createElement( 'div' );
container.id = 'mw-right-toolbar';
container.className = 'mw-right-toolbar';
document.body.appendChild( container );
}
function createButton( id, text, tooltip, clickHandler ) {
var button = document.createElement( 'button' );
button.id = id;
button.className = 'mw-rt-button';
button.title = tooltip;
button.textContent = text;
button.type = 'button';
button.addEventListener( 'click', clickHandler );
return button;
}
var topButton = document.getElementById( 'mw-scroll-top' );
if ( !topButton ) {
topButton = createButton( 'mw-scroll-top', '▲', '回到顶部', function () {
window.scrollTo( {
top: 0,
behavior: 'smooth'
} );
} );
container.appendChild( topButton );
}
var bottomButton = document.getElementById( 'mw-scroll-bottom' );
if ( !bottomButton ) {
bottomButton = createButton( 'mw-scroll-bottom', '▼', '回到底部', function () {
window.scrollTo( {
top: document.body.scrollHeight,
behavior: 'smooth'
} );
} );
container.appendChild( bottomButton );
}
var clockDiv = document.getElementById( 'mw-right-toolbar-clock' );
if ( !isMobile && !clockDiv ) {
clockDiv = document.createElement( 'div' );
clockDiv.id = 'mw-right-toolbar-clock';
clockDiv.className = 'mw-rt-clock';
var localDateDiv = document.createElement( 'div' );
localDateDiv.id = 'mw-local-date';
localDateDiv.textContent = '加载中...';
var localTimeDiv = document.createElement( 'div' );
localTimeDiv.id = 'mw-local-time';
var utcTimeDiv = document.createElement( 'div' );
utcTimeDiv.id = 'mw-utc-time';
clockDiv.appendChild( localDateDiv );
clockDiv.appendChild( localTimeDiv );
clockDiv.appendChild( utcTimeDiv );
container.appendChild( clockDiv );
}
topButton.style.display = 'none';
var ticking = false;
window.addEventListener( 'scroll', function () {
if ( ticking ) {
return;
}
ticking = true;
window.requestAnimationFrame( function () {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
if ( scrollTop > 200 ) {
topButton.style.display = 'block';
} else {
topButton.style.display = 'none';
}
ticking = false;
} );
} );
window.requestAnimationFrame( function () {
container.classList.add( 'is-visible' );
} );
function renderClock() {
var now = new Date();
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( function ( p ) {
return p.type === 'year' || p.type === 'month' || p.type === 'day';
} ).map( function ( p ) {
return p.value;
} ).join( ' ' );
var localTimeStr = localParts.filter( function ( p ) {
return p.type === 'hour' || p.type === 'minute' || p.type === 'second';
} ).map( function ( p ) {
return p.value;
} ).join( ':' );
var localDateEl = document.getElementById( 'mw-local-date' );
var localTimeEl = document.getElementById( 'mw-local-time' );
if ( localDateEl ) localDateEl.textContent = localDateStr;
if ( localTimeEl ) localTimeEl.textContent = localTimeStr;
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( function ( p ) {
return p.type === 'year' || p.type === 'month' || p.type === 'day';
} ).map( function ( p ) {
return p.value;
} ).join( ' ' );
var utcTimeStr = utcParts.filter( function ( p ) {
return p.type === 'hour' || p.type === 'minute' || p.type === 'second';
} ).map( function ( p ) {
return p.value;
} ).join( ':' );
var utcTimeEl = document.getElementById( 'mw-utc-time' );
if ( utcTimeEl ) utcTimeEl.textContent = 'UTC: ' + utcDateStr + ' ' + utcTimeStr;
}
var clockTimer = window.setInterval( renderClock, 1000 );
renderClock();
mw.hook( 'user.languageChange' ).add( renderClock );
document.addEventListener( 'visibilitychange', function () {
if ( document.hidden ) {
window.clearInterval( clockTimer );
clockTimer = null;
return;
}
if ( !clockTimer ) {
clockTimer = window.setInterval( renderClock, 1000 );
renderClock();
}
} );
}
// 页面加载完成后初始化
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', initClockBar );
} else {
initClockBar();
}
} );