跳转到内容

MediaWiki:Gadget-RightToolbar.js:修订间差异

来自陋室
删除的内容 添加的内容
XP-jia​(留言 | 贡献)
小 修改bug
Maintenance script​(留言 | 贡献)
小 优化 RightToolbar:移动端禁用并降低空闲开销
 
(未显示2个用户的6个中间版本)
第1行: 第1行:
/* MediaWiki Gadget: RightToolbar */
/* ================================================================
( function () {
MediaWiki Gadget: Custom Tools Bar with Localized Clock
'use strict';
功能:
- 回到顶部 / 回到底部按钮
- 本地化日期和时间显示(浏览器时区 + UTC)
- 深色模式自动适配
- 动态显示按钮 + 平滑动画
作者:你自己
================================================================= */


mw.loader.using(['mediawiki.util', 'mediawiki.language'], function () {
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
var toolbar;
var topButton;
var bottomButton;
var clockTimer = 0;
var ticking = false;
var mobileQuery = window.matchMedia( '(max-width: 768px)' );


function updateClock() {
function isMobileView() {
return mobileQuery.matches ||
// 浏览器本地时间
document.documentElement.classList.contains( 'skin-minerva' ) ||
var local = new Date();
new URLSearchParams( window.location.search ).get( 'mobileaction' ) === 'toggle_view_mobile';
// UTC 时间,从浏览器系统获取即可
}
var utc = new Date(local.getTime() + local.getTimezoneOffset() * 60000);


function msg( key ) {
// MediaWiki 提供的语言设置
var userLang = mw.config.get('wgUserLanguage') || 'en';
var lang = ( mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn' ).toLowerCase();
var en = lang.indexOf( 'en' ) === 0;
var messages = {
top: en ? 'Top' : '顶部',
bottom: en ? 'Bottom' : '底部',
topTitle: en ? 'Back to top' : '回到顶部',
bottomTitle: en ? 'Back to bottom' : '回到底部',
loading: en ? 'Loading...' : '加载中...'
};
return messages[ key ];
}


function createButton( id, text, tooltip, clickHandler ) {
// 本地化格式化函数
var button = document.createElement( 'button' );
// dateStyle/timeStyle 采用 Intl.DateTimeFormat 可选参数,也可以用短/长格式
button.id = id;
var localString = new Intl.DateTimeFormat(userLang, {
button.className = 'mw-rt-button';
year: 'numeric', month: 'long', day: 'numeric',
button.title = tooltip;
hour: '2-digit', minute: '2-digit', second: '2-digit',
button.type = 'button';
hour12: false
button.textContent = text;
}).format(local);
button.setAttribute( 'aria-label', tooltip );
button.addEventListener( 'click', clickHandler );
return button;
}


function formatParts( date, zone ) {
var utcString = new Intl.DateTimeFormat(userLang, {
var userLang = mw.config.get( 'wgUserLanguage' ) || 'zh-cn';
year: 'numeric', month: 'long', day: 'numeric',
var parts = new Intl.DateTimeFormat( userLang, {
hour: '2-digit', minute: '2-digit', second: '2-digit',
year: 'numeric',
hour12: false, timeZone: 'UTC'
month: '2-digit',
}).format(utc);
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZone: zone
} ).formatToParts( date );


function partValue( type ) {
$('#current-local-time').text(localString);
var part = parts.find( function ( item ) {
$('#current-utc-time').text(utcString);
return item.type === type;
}
} );
return part ? part.value : '';
}


return {
$(function () {
date: partValue( 'year' ) + '-' + partValue( 'month' ) + '-' + partValue( 'day' ),
var isMobile = window.innerWidth <= 768;
time: partValue( 'hour' ) + ':' + partValue( 'minute' ) + ':' + partValue( 'second' )
};
}


function renderClock() {
/* ---------- 深色模式检测 ---------- */
var now = new Date();
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const bg = isDark ? 'rgba(40,40,40,0.85)' : 'rgba(255,255,255,0.85)';
var local = formatParts( now, timeZone );
const border = isDark ? '#555' : '#aaa';
var utc = formatParts( now, 'UTC' );
const color = isDark ? '#eee' : '#333';
var localDateEl = document.getElementById( 'mw-local-date' );
var localTimeEl = document.getElementById( 'mw-local-time' );
var utcTimeEl = document.getElementById( 'mw-utc-time' );


if ( localDateEl ) {
/* ---------- 工具栏容器 ---------- */
localDateEl.textContent = local.date;
var $container = $('<div id="mw-custom-tools"></div>').css({
}
position: 'fixed',
if ( localTimeEl ) {
bottom: '120px',
localTimeEl.textContent = local.time;
right: '50px',
}
display: 'flex',
if ( utcTimeEl ) {
flexDirection: 'column',
utcTimeEl.textContent = 'UTC ' + utc.date + ' ' + utc.time;
alignItems: 'flex-end',
}
gap: '5px',
}
zIndex: 9999,
opacity: 0,
transform: 'translateY(20px)',
}).appendTo('body');


function updateTopButton() {
/* ---------- 按钮生成函数 ---------- */
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
function createButton(id, text, tooltip, clickHandler) {
topButton.hidden = scrollTop <= 240;
return $('<div></div>', { id: id, title: tooltip, text: text }).css({
bottomButton.hidden = document.documentElement.scrollHeight - window.innerHeight - scrollTop <= 240;
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);
}


function onScroll() {
/* ---------- 按钮 ---------- */
if ( ticking ) {
var $topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () {
return;
$('html, body').animate({ scrollTop: 0 }, { duration: 500, easing: 'swing' });
}
});
ticking = true;
window.requestAnimationFrame( function () {
updateTopButton();
ticking = false;
} );
}


function stopClock() {
var $bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () {
if ( clockTimer ) {
$('html, body').animate({ scrollTop: $(document).height() }, { duration: 500, easing: 'swing' });
window.clearInterval( clockTimer );
});
clockTimer = 0;
}
}


function teardownToolbar() {
/* ---------- 时钟(桌面端) ---------- */
stopClock();
if (!isMobile) {
window.removeEventListener( 'scroll', onScroll );
var $clockDiv = $('<div id="current-clock"></div>').css({
if ( toolbar ) {
padding: '6px 12px',
toolbar.remove();
fontSize: '12px',
}
background: bg,
toolbar = null;
border: '1px solid ' + border,
topButton = null;
borderRadius: '4px',
bottomButton = null;
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-time">加载中...</div>'),
$('<div id="current-utc-time">加载中...</div>')
);
$container.append($clockDiv);
}


function initToolbar() {
/* ---------- 组装按钮 ---------- */
var clockDiv;
$container.append($topButton, $bottomButton);


if ( isMobileView() ) {
/* ---------- 初次进入动画 ---------- */
teardownToolbar();
$container.animate({ opacity: 1, transform: 'translateY(0)' }, 600);
return;
}
if ( toolbar ) {
return;
}


toolbar = document.createElement( 'div' );
/* ---------- 时钟定时更新 ---------- */
toolbar.id = 'mw-right-toolbar';
if (!isMobile) {
toolbar.className = 'mw-right-toolbar';
updateClock();
setInterval(updateClock, 1000);
}


topButton = createButton( 'mw-scroll-top', msg( 'top' ), msg( 'topTitle' ), function () {
/* ---------- 滚动动态控制按钮显示 ---------- */
window.scrollTo( { top: 0, behavior: 'smooth' } );
$topButton.hide(); // 初始隐藏
} );
$(window).on('scroll', function () {
bottomButton = createButton( 'mw-scroll-bottom', msg( 'bottom' ), msg( 'bottomTitle' ), function () {
var scrollTop = $(this).scrollTop();
window.scrollTo( {
if (scrollTop > 200) {
top: Math.max( document.body.scrollHeight, document.documentElement.scrollHeight ),
$topButton.fadeIn();
behavior: 'smooth'
} else {
} );
$topButton.fadeOut();
} );
}

});
clockDiv = document.createElement( 'div' );
});
clockDiv.id = 'mw-right-toolbar-clock';
});
clockDiv.className = 'mw-rt-clock';
clockDiv.innerHTML = '<div id="mw-local-date"></div><div id="mw-local-time"></div><div id="mw-utc-time"></div>';
clockDiv.firstChild.textContent = msg( 'loading' );

toolbar.appendChild( topButton );
toolbar.appendChild( bottomButton );
toolbar.appendChild( clockDiv );
document.body.appendChild( toolbar );

window.addEventListener( 'scroll', onScroll, { passive: true } );
updateTopButton();
renderClock();
clockTimer = window.setInterval( renderClock, 1000 );

window.requestAnimationFrame( function () {
if ( toolbar ) {
toolbar.classList.add( 'is-visible' );
}
} );
}

function refreshToolbar() {
if ( isMobileView() ) {
teardownToolbar();
return;
}
initToolbar();
}

if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', initToolbar );
} else {
initToolbar();
}

mobileQuery.addEventListener( 'change', refreshToolbar );
window.addEventListener( 'orientationchange', refreshToolbar );
document.addEventListener( 'visibilitychange', function () {
if ( !toolbar ) {
return;
}
if ( document.hidden ) {
stopClock();
return;
}
renderClock();
if ( !clockTimer ) {
clockTimer = window.setInterval( renderClock, 1000 );
}
} );
} );
}() );

2026年7月19日 (日) 20:11的最新版本

/* MediaWiki Gadget: RightToolbar */
( function () {
	'use strict';

	mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
		var toolbar;
		var topButton;
		var bottomButton;
		var clockTimer = 0;
		var ticking = false;
		var mobileQuery = window.matchMedia( '(max-width: 768px)' );

		function isMobileView() {
			return mobileQuery.matches ||
				document.documentElement.classList.contains( 'skin-minerva' ) ||
				new URLSearchParams( window.location.search ).get( 'mobileaction' ) === 'toggle_view_mobile';
		}

		function msg( key ) {
			var lang = ( mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn' ).toLowerCase();
			var en = lang.indexOf( 'en' ) === 0;
			var messages = {
				top: en ? 'Top' : '顶部',
				bottom: en ? 'Bottom' : '底部',
				topTitle: en ? 'Back to top' : '回到顶部',
				bottomTitle: en ? 'Back to bottom' : '回到底部',
				loading: en ? 'Loading...' : '加载中...'
			};
			return messages[ key ];
		}

		function createButton( id, text, tooltip, clickHandler ) {
			var button = document.createElement( 'button' );
			button.id = id;
			button.className = 'mw-rt-button';
			button.title = tooltip;
			button.type = 'button';
			button.textContent = text;
			button.setAttribute( 'aria-label', tooltip );
			button.addEventListener( 'click', clickHandler );
			return button;
		}

		function formatParts( date, zone ) {
			var userLang = mw.config.get( 'wgUserLanguage' ) || 'zh-cn';
			var parts = new Intl.DateTimeFormat( userLang, {
				year: 'numeric',
				month: '2-digit',
				day: '2-digit',
				hour: '2-digit',
				minute: '2-digit',
				second: '2-digit',
				hour12: false,
				timeZone: zone
			} ).formatToParts( date );

			function partValue( type ) {
				var part = parts.find( function ( item ) {
					return item.type === type;
				} );
				return part ? part.value : '';
			}

			return {
				date: partValue( 'year' ) + '-' + partValue( 'month' ) + '-' + partValue( 'day' ),
				time: partValue( 'hour' ) + ':' + partValue( 'minute' ) + ':' + partValue( 'second' )
			};
		}

		function renderClock() {
			var now = new Date();
			var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
			var local = formatParts( now, timeZone );
			var utc = formatParts( now, 'UTC' );
			var localDateEl = document.getElementById( 'mw-local-date' );
			var localTimeEl = document.getElementById( 'mw-local-time' );
			var utcTimeEl = document.getElementById( 'mw-utc-time' );

			if ( localDateEl ) {
				localDateEl.textContent = local.date;
			}
			if ( localTimeEl ) {
				localTimeEl.textContent = local.time;
			}
			if ( utcTimeEl ) {
				utcTimeEl.textContent = 'UTC ' + utc.date + ' ' + utc.time;
			}
		}

		function updateTopButton() {
			var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			topButton.hidden = scrollTop <= 240;
			bottomButton.hidden = document.documentElement.scrollHeight - window.innerHeight - scrollTop <= 240;
		}

		function onScroll() {
			if ( ticking ) {
				return;
			}
			ticking = true;
			window.requestAnimationFrame( function () {
				updateTopButton();
				ticking = false;
			} );
		}

		function stopClock() {
			if ( clockTimer ) {
				window.clearInterval( clockTimer );
				clockTimer = 0;
			}
		}

		function teardownToolbar() {
			stopClock();
			window.removeEventListener( 'scroll', onScroll );
			if ( toolbar ) {
				toolbar.remove();
			}
			toolbar = null;
			topButton = null;
			bottomButton = null;
		}

		function initToolbar() {
			var clockDiv;

			if ( isMobileView() ) {
				teardownToolbar();
				return;
			}
			if ( toolbar ) {
				return;
			}

			toolbar = document.createElement( 'div' );
			toolbar.id = 'mw-right-toolbar';
			toolbar.className = 'mw-right-toolbar';

			topButton = createButton( 'mw-scroll-top', msg( 'top' ), msg( 'topTitle' ), function () {
				window.scrollTo( { top: 0, behavior: 'smooth' } );
			} );
			bottomButton = createButton( 'mw-scroll-bottom', msg( 'bottom' ), msg( 'bottomTitle' ), function () {
				window.scrollTo( {
					top: Math.max( document.body.scrollHeight, document.documentElement.scrollHeight ),
					behavior: 'smooth'
				} );
			} );

			clockDiv = document.createElement( 'div' );
			clockDiv.id = 'mw-right-toolbar-clock';
			clockDiv.className = 'mw-rt-clock';
			clockDiv.innerHTML = '<div id="mw-local-date"></div><div id="mw-local-time"></div><div id="mw-utc-time"></div>';
			clockDiv.firstChild.textContent = msg( 'loading' );

			toolbar.appendChild( topButton );
			toolbar.appendChild( bottomButton );
			toolbar.appendChild( clockDiv );
			document.body.appendChild( toolbar );

			window.addEventListener( 'scroll', onScroll, { passive: true } );
			updateTopButton();
			renderClock();
			clockTimer = window.setInterval( renderClock, 1000 );

			window.requestAnimationFrame( function () {
				if ( toolbar ) {
					toolbar.classList.add( 'is-visible' );
				}
			} );
		}

		function refreshToolbar() {
			if ( isMobileView() ) {
				teardownToolbar();
				return;
			}
			initToolbar();
		}

		if ( document.readyState === 'loading' ) {
			document.addEventListener( 'DOMContentLoaded', initToolbar );
		} else {
			initToolbar();
		}

		mobileQuery.addEventListener( 'change', refreshToolbar );
		window.addEventListener( 'orientationchange', refreshToolbar );
		document.addEventListener( 'visibilitychange', function () {
			if ( !toolbar ) {
				return;
			}
			if ( document.hidden ) {
				stopClock();
				return;
			}
			renderClock();
			if ( !clockTimer ) {
				clockTimer = window.setInterval( renderClock, 1000 );
			}
		} );
	} );
}() );