MediaWiki:Gadget-RightToolbar.js:修订间差异
外观
删除的内容 添加的内容
Maintenance script(留言 | 贡献) 添加JavaScript代码 |
Maintenance script(留言 | 贡献) 维护 RightToolbar:样式归一到 CSS 页 |
||
| 第1行: | 第1行: | ||
/* MediaWiki Gadget: RightToolbar */ |
|||
( function () { |
|||
'use strict'; |
|||
mw.loader.using( [ 'mediawiki.util' ] ).then( function () { |
|||
/* ================================================================ |
|||
function msg( key ) { |
|||
MediaWiki Gadget: Localized Clock Bar |
|||
var lang = mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn'; |
|||
功能: |
|||
var en = lang.toLowerCase().indexOf( 'en' ) === 0; |
|||
- 显示浏览器本地时间和 UTC 时间 |
|||
var messages = { |
|||
- 使用 MediaWiki 用户语言格式 |
|||
top: en ? '▲' : '▲', |
|||
- 深色模式适配 + 滑入动画 |
|||
bottom: en ? '▼' : '▼', |
|||
================================================================= */ |
|||
topTitle: en ? 'Back to top' : '回到顶部', |
|||
bottomTitle: en ? 'Back to bottom' : '回到底部', |
|||
loading: en ? 'Loading...' : '加载中...' |
|||
}; |
|||
return messages[ key ]; |
|||
} |
|||
function createButton( id, text, tooltip, clickHandler ) { |
|||
mw.loader.using( [ 'mediawiki.util' ] ).then( function () { |
|||
var button = document.createElement( 'button' ); |
|||
var STYLE_ID = 'mw-right-toolbar-style'; |
|||
button.id = id; |
|||
button.className = 'mw-rt-button'; |
|||
button.title = tooltip; |
|||
button.textContent = text; |
|||
button.type = 'button'; |
|||
button.addEventListener( 'click', clickHandler ); |
|||
return button; |
|||
} |
|||
function renderClock() { |
|||
var now = new Date(); |
|||
if ( document.getElementById( STYLE_ID ) ) { |
|||
var userLang = mw.config.get( 'wgUserLanguage' ) || 'zh-cn'; |
|||
return; |
|||
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
|||
} |
|||
function formatParts( zone ) { |
|||
var css = [ |
|||
var parts = new Intl.DateTimeFormat( userLang, { |
|||
'#mw-right-toolbar, .mw-right-toolbar {', |
|||
year: 'numeric', |
|||
' position: fixed;', |
|||
month: 'long', |
|||
' bottom: 96px;', |
|||
day: 'numeric', |
|||
' right: 24px;', |
|||
hour: '2-digit', |
|||
' display: flex;', |
|||
minute: '2-digit', |
|||
' flex-direction: column;', |
|||
second: '2-digit', |
|||
' align-items: flex-end;', |
|||
hour12: false, |
|||
' gap: 6px;', |
|||
timeZone: zone |
|||
' z-index: 2147483647;', |
|||
} ).formatToParts( now ); |
|||
' 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' ); |
|||
return { |
|||
var styleNode = mw.loader.addStyleTag( css ); |
|||
date: parts.filter( function ( part ) { |
|||
styleNode.id = STYLE_ID; |
|||
return part.type === 'year' || part.type === 'month' || part.type === 'day'; |
|||
} |
|||
} ).map( function ( part ) { |
|||
return part.value; |
|||
} ).join( ' ' ), |
|||
time: parts.filter( function ( part ) { |
|||
return part.type === 'hour' || part.type === 'minute' || part.type === 'second'; |
|||
} ).map( function ( part ) { |
|||
return part.value; |
|||
} ).join( ':' ) |
|||
}; |
|||
} |
|||
var local = formatParts( timeZone ); |
|||
function initClockBar() { |
|||
var utc = formatParts( 'UTC' ); |
|||
ensureToolbarStyles(); |
|||
var localDateEl = document.getElementById( 'mw-local-date' ); |
|||
var localTimeEl = document.getElementById( 'mw-local-time' ); |
|||
var utcTimeEl = document.getElementById( 'mw-utc-time' ); |
|||
if ( localDateEl ) { |
|||
var isMobile = window.innerWidth <= 768; |
|||
localDateEl.textContent = local.date; |
|||
var container = document.getElementById( 'mw-right-toolbar' ); |
|||
} |
|||
if ( localTimeEl ) { |
|||
localTimeEl.textContent = local.time; |
|||
} |
|||
if ( utcTimeEl ) { |
|||
utcTimeEl.textContent = 'UTC: ' + utc.date + ' ' + utc.time; |
|||
} |
|||
} |
|||
function initToolbar() { |
|||
if ( !container ) { |
|||
var isMobile = window.innerWidth <= 768; |
|||
container = document.createElement( 'div' ); |
|||
var container = document.getElementById( 'mw-right-toolbar' ); |
|||
var topButton; |
|||
container.className = 'mw-right-toolbar'; |
|||
var bottomButton; |
|||
document.body.appendChild( container ); |
|||
var clockDiv; |
|||
} |
|||
var ticking = false; |
|||
var clockTimer; |
|||
if ( !container ) { |
|||
function createButton( id, text, tooltip, clickHandler ) { |
|||
container = document.createElement( 'div' ); |
|||
container.id = 'mw-right-toolbar'; |
|||
button.id = id; |
|||
container.className = 'mw-right-toolbar'; |
|||
document.body.appendChild( container ); |
|||
button.title = tooltip; |
|||
} |
|||
button.textContent = text; |
|||
button.type = 'button'; |
|||
button.addEventListener( 'click', clickHandler ); |
|||
return button; |
|||
} |
|||
topButton = document.getElementById( 'mw-scroll-top' ); |
|||
if ( !topButton ) { |
|||
topButton = createButton( 'mw-scroll-top', msg( 'top' ), msg( 'topTitle' ), function () { |
|||
window.scrollTo( { top: 0, behavior: 'smooth' } ); |
|||
window.scrollTo( { |
|||
} ); |
|||
top: 0, |
|||
container.appendChild( topButton ); |
|||
behavior: 'smooth' |
|||
} |
|||
} ); |
|||
} ); |
|||
container.appendChild( topButton ); |
|||
} |
|||
bottomButton = document.getElementById( 'mw-scroll-bottom' ); |
|||
if ( !bottomButton ) { |
|||
bottomButton = createButton( 'mw-scroll-bottom', msg( 'bottom' ), msg( 'bottomTitle' ), function () { |
|||
window.scrollTo( { top: document.body.scrollHeight, behavior: 'smooth' } ); |
|||
window.scrollTo( { |
|||
} ); |
|||
top: document.body.scrollHeight, |
|||
container.appendChild( bottomButton ); |
|||
behavior: 'smooth' |
|||
} |
|||
} ); |
|||
} ); |
|||
container.appendChild( bottomButton ); |
|||
} |
|||
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'; |
|||
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' ); |
|||
var localDateDiv = document.createElement( 'div' ); |
|||
container.appendChild( clockDiv ); |
|||
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'; |
|||
window.addEventListener( 'scroll', function () { |
|||
var ticking = false; |
|||
if ( ticking ) { |
|||
window.addEventListener( 'scroll', function () { |
|||
return; |
|||
if ( ticking ) { |
|||
} |
|||
return; |
|||
ticking = true; |
|||
} |
|||
window.requestAnimationFrame( function () { |
|||
ticking = true; |
|||
var scrollTop = window.pageYOffset || document.documentElement.scrollTop; |
|||
window.requestAnimationFrame( function () { |
|||
topButton.style.display = scrollTop > 200 ? 'block' : 'none'; |
|||
var scrollTop = window.pageYOffset || document.documentElement.scrollTop; |
|||
ticking = false; |
|||
if ( scrollTop > 200 ) { |
|||
} ); |
|||
topButton.style.display = 'block'; |
|||
} ); |
|||
} else { |
|||
topButton.style.display = 'none'; |
|||
} |
|||
ticking = false; |
|||
} ); |
|||
} ); |
|||
window.requestAnimationFrame( function () { |
|||
container.classList.add( 'is-visible' ); |
|||
} ); |
|||
clockTimer = window.setInterval( renderClock, 1000 ); |
|||
function renderClock() { |
|||
renderClock(); |
|||
var now = new Date(); |
|||
mw.hook( 'user.languageChange' ).add( renderClock ); |
|||
var userLang = mw.config.get( 'wgUserLanguage' ) || 'en'; |
|||
document.addEventListener( 'visibilitychange', function () { |
|||
var localFormatter = new Intl.DateTimeFormat( userLang, { |
|||
if ( document.hidden ) { |
|||
year: 'numeric', |
|||
window.clearInterval( clockTimer ); |
|||
month: 'long', |
|||
clockTimer = null; |
|||
day: 'numeric', |
|||
return; |
|||
hour: '2-digit', |
|||
} |
|||
minute: '2-digit', |
|||
if ( !clockTimer ) { |
|||
second: '2-digit', |
|||
clockTimer = window.setInterval( renderClock, 1000 ); |
|||
hour12: false, |
|||
renderClock(); |
|||
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone |
|||
} |
|||
} ); |
|||
} ); |
|||
} |
|||
if ( document.readyState === 'loading' ) { |
|||
var localParts = localFormatter.formatToParts( now ); |
|||
document.addEventListener( 'DOMContentLoaded', initToolbar ); |
|||
var localDateStr = localParts.filter( function ( p ) { |
|||
} else { |
|||
return p.type === 'year' || p.type === 'month' || p.type === 'day'; |
|||
initToolbar(); |
|||
} ).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(); |
|||
} |
|||
} ); |
|||
2026年7月16日 (四) 16:59的版本
/* MediaWiki Gadget: RightToolbar */
( function () {
'use strict';
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
function msg( key ) {
var lang = mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn';
var en = lang.toLowerCase().indexOf( 'en' ) === 0;
var messages = {
top: en ? '▲' : '▲',
bottom: en ? '▼' : '▼',
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.textContent = text;
button.type = 'button';
button.addEventListener( 'click', clickHandler );
return button;
}
function renderClock() {
var now = new Date();
var userLang = mw.config.get( 'wgUserLanguage' ) || 'zh-cn';
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
function formatParts( zone ) {
var parts = new Intl.DateTimeFormat( userLang, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZone: zone
} ).formatToParts( now );
return {
date: parts.filter( function ( part ) {
return part.type === 'year' || part.type === 'month' || part.type === 'day';
} ).map( function ( part ) {
return part.value;
} ).join( ' ' ),
time: parts.filter( function ( part ) {
return part.type === 'hour' || part.type === 'minute' || part.type === 'second';
} ).map( function ( part ) {
return part.value;
} ).join( ':' )
};
}
var local = formatParts( timeZone );
var utc = formatParts( '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 initToolbar() {
var isMobile = window.innerWidth <= 768;
var container = document.getElementById( 'mw-right-toolbar' );
var topButton;
var bottomButton;
var clockDiv;
var ticking = false;
var clockTimer;
if ( !container ) {
container = document.createElement( 'div' );
container.id = 'mw-right-toolbar';
container.className = 'mw-right-toolbar';
document.body.appendChild( container );
}
topButton = document.getElementById( 'mw-scroll-top' );
if ( !topButton ) {
topButton = createButton( 'mw-scroll-top', msg( 'top' ), msg( 'topTitle' ), function () {
window.scrollTo( { top: 0, behavior: 'smooth' } );
} );
container.appendChild( topButton );
}
bottomButton = document.getElementById( 'mw-scroll-bottom' );
if ( !bottomButton ) {
bottomButton = createButton( 'mw-scroll-bottom', msg( 'bottom' ), msg( 'bottomTitle' ), function () {
window.scrollTo( { top: document.body.scrollHeight, behavior: 'smooth' } );
} );
container.appendChild( bottomButton );
}
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';
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' );
container.appendChild( clockDiv );
}
topButton.style.display = 'none';
window.addEventListener( 'scroll', function () {
if ( ticking ) {
return;
}
ticking = true;
window.requestAnimationFrame( function () {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
topButton.style.display = scrollTop > 200 ? 'block' : 'none';
ticking = false;
} );
} );
window.requestAnimationFrame( function () {
container.classList.add( 'is-visible' );
} );
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', initToolbar );
} else {
initToolbar();
}
} );
}() );