MediaWiki:Gadget-RightToolbar.js:修订间差异
外观
删除的内容 添加的内容
new |
Maintenance script(留言 | 贡献) 小 优化 RightToolbar:移动端禁用并降低空闲开销 |
||
| (未显示2个用户的12个中间版本) | |||
| 第1行: | 第1行: | ||
/* MediaWiki Gadget: RightToolbar */ |
|||
mw.loader.using(['mediawiki.util'], function () { |
|||
( function () { |
|||
'use strict'; |
|||
var now = new Date(); |
|||
var dateString = now.getFullYear() + "年" + |
|||
(now.getMonth() + 1).toString().padStart(2, '0') + "月" + |
|||
now.getDate().toString().padStart(2, '0') + "日"; |
|||
mw.loader.using( [ 'mediawiki.util' ] ).then( function () { |
|||
var timeString = now.toLocaleTimeString(); |
|||
var toolbar; |
|||
var topButton; |
|||
var bottomButton; |
|||
var clockTimer = 0; |
|||
var ticking = false; |
|||
var mobileQuery = window.matchMedia( '(max-width: 768px)' ); |
|||
function isMobileView() { |
|||
$('#current-date').text(dateString); |
|||
return mobileQuery.matches || |
|||
$('#current-time').text(timeString); |
|||
document.documentElement.classList.contains( 'skin-minerva' ) || |
|||
} |
|||
new URLSearchParams( window.location.search ).get( 'mobileaction' ) === 'toggle_view_mobile'; |
|||
} |
|||
function msg( key ) { |
|||
$(function () { |
|||
var lang = ( mw.config.get( 'wgUserLanguage' ) || mw.config.get( 'wgContentLanguage' ) || 'zh-cn' ).toLowerCase(); |
|||
var isMobile = window.innerWidth <= 768; // 判断是否为移动端 |
|||
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' ); |
|||
var $container = $('<div id="mw-custom-tools"></div>').css({ |
|||
button.id = id; |
|||
position: 'fixed', |
|||
button.className = 'mw-rt-button'; |
|||
bottom: '120px', // 避免与其他工具重叠 |
|||
button.title = tooltip; |
|||
right: '20px', |
|||
button.type = 'button'; |
|||
display: 'flex', |
|||
button.textContent = text; |
|||
flexDirection: 'column', |
|||
button.setAttribute( 'aria-label', tooltip ); |
|||
alignItems: 'flex-end', |
|||
button.addEventListener( 'click', clickHandler ); |
|||
gap: '5px', |
|||
return button; |
|||
zIndex: 9999 |
|||
} |
|||
}); |
|||
function formatParts( date, zone ) { |
|||
// 统一按钮样式 |
|||
var userLang = mw.config.get( 'wgUserLanguage' ) || 'zh-cn'; |
|||
function createButton(id, text, tooltip, clickHandler) { |
|||
var parts = new Intl.DateTimeFormat( userLang, { |
|||
return $('<div></div>', { id: id, title: tooltip, text: text }).css({ |
|||
year: 'numeric', |
|||
padding: '5px 10px', |
|||
month: '2-digit', |
|||
fontSize: '12px', |
|||
day: '2-digit', |
|||
background: '#f1f1f1', |
|||
hour: '2-digit', |
|||
border: '1px solid #aaa', |
|||
minute: '2-digit', |
|||
borderRadius: '4px', |
|||
second: '2-digit', |
|||
boxShadow: '0 1px 3px rgba(0,0,0,0.2)', |
|||
hour12: false, |
|||
color: '#333', |
|||
timeZone: zone |
|||
cursor: 'pointer', |
|||
} ).formatToParts( date ); |
|||
textAlign: 'center', |
|||
opacity: 0.7, |
|||
transition: 'background 0.3s, opacity 0.3s' |
|||
}).hover(function () { |
|||
$(this).css({ background: '#ddd', opacity: 1 }); |
|||
}, function () { |
|||
$(this).css({ background: '#f1f1f1', opacity: 0.7 }); |
|||
}).click(clickHandler); |
|||
} |
|||
function partValue( type ) { |
|||
// 创建回到顶部按钮 |
|||
var part = parts.find( function ( item ) { |
|||
var $topButton = createButton('mw-scroll-top', '▲', '回到顶部', function () { |
|||
return item.type === type; |
|||
$('html, body').animate({ scrollTop: 0 }, 500); |
|||
} ); |
|||
}); |
|||
return part ? part.value : ''; |
|||
} |
|||
return { |
|||
// 创建回到底部按钮 |
|||
date: partValue( 'year' ) + '-' + partValue( 'month' ) + '-' + partValue( 'day' ), |
|||
var $bottomButton = createButton('mw-scroll-bottom', '▼', '回到底部', function () { |
|||
time: partValue( 'hour' ) + ':' + partValue( 'minute' ) + ':' + partValue( 'second' ) |
|||
$('html, body').animate({ scrollTop: $(document).height() }, 500); |
|||
}; |
|||
}); |
|||
} |
|||
function renderClock() { |
|||
// 创建时间显示区域(仅桌面端) |
|||
var now = new Date(); |
|||
if (!isMobile) { |
|||
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
|||
var $clockDiv = $('<div id="current-clock"></div>').css({ |
|||
var local = formatParts( now, timeZone ); |
|||
padding: '6px 12px', |
|||
var utc = formatParts( now, 'UTC' ); |
|||
fontSize: '12px', |
|||
var localDateEl = document.getElementById( 'mw-local-date' ); |
|||
background: '#f1f1f1', |
|||
var localTimeEl = document.getElementById( 'mw-local-time' ); |
|||
border: '1px solid #aaa', |
|||
var utcTimeEl = document.getElementById( 'mw-utc-time' ); |
|||
borderRadius: '4px', |
|||
boxShadow: '0 1px 3px rgba(0,0,0,0.2)', |
|||
color: '#333', |
|||
textAlign: 'center', |
|||
lineHeight: '1.4' |
|||
}).append( |
|||
$('<div id="current-date">加载中...</div>'), |
|||
$('<div id="current-time"></div>') |
|||
); |
|||
$container.append($clockDiv); |
|||
} |
|||
if ( localDateEl ) { |
|||
// 组装按钮 |
|||
localDateEl.textContent = local.date; |
|||
$container.append($topButton, $bottomButton); |
|||
} |
|||
$('body').append($container); |
|||
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; |
|||
if (!isMobile) { |
|||
topButton.hidden = scrollTop <= 240; |
|||
updateClock(); |
|||
bottomButton.hidden = document.documentElement.scrollHeight - window.innerHeight - scrollTop <= 240; |
|||
setInterval(updateClock, 1000); |
|||
} |
|||
} |
|||
}); |
|||
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 ); |
|||
} |
|||
} ); |
|||
} ); |
|||
}() ); |
|||
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 );
}
} );
} );
}() );