MediaWiki:Common.js:修订间差异
来自神国反恶俗
跳到导航跳到搜索
同步顶部工具栏高度 |
add private message topbar link |
||
| (未显示同一用户的8个中间版本) | |||
| 第6行: | 第6行: | ||
var sidebar = document.getElementById( "column-one" ); | var sidebar = document.getElementById( "column-one" ); | ||
if ( !toc || !sidebar ) { return; } | if ( !toc || !sidebar ) { return; } | ||
var nav = document.getElementById( "p-navigation" ); | |||
var container = ( nav && nav.parentNode ) ? nav.parentNode : sidebar; | |||
var existing = document.getElementById( "p-toc" ); | |||
if ( existing ) { | |||
if ( nav ) { | |||
container.insertBefore( existing, nav.nextSibling ); | |||
} else { | |||
container.appendChild( existing ); | |||
} | |||
return; | |||
} | |||
var box = document.createElement( "div" ); | var box = document.createElement( "div" ); | ||
box.className = "portlet"; | box.className = "portlet"; | ||
| 第17行: | 第27行: | ||
box.appendChild( title ); | box.appendChild( title ); | ||
box.appendChild( inner ); | box.appendChild( inner ); | ||
if ( nav ) { | |||
container.insertBefore( box, nav.nextSibling ); | |||
} else { | |||
var search = document.getElementById( "p-search" ); | |||
if ( search && search.parentNode ) { | |||
search.parentNode.insertBefore( box, search ); | |||
} else { | |||
container.appendChild( box ); | |||
} | |||
} | |||
} | } | ||
| 第67行: | 第86行: | ||
var rightLinks = pickLinks( document.getElementById( "p-personal" ) ); | var rightLinks = pickLinks( document.getElementById( "p-personal" ) ); | ||
if ( leftLinks.length === 0 && rightLinks.length === 0 ) { return; } | var notifHref = null; | ||
var notifCount = 0; | |||
var notifIndex = -1; | |||
var plainRightLinks = []; | |||
for ( var n = 0; n < rightLinks.length; n++ ) { | |||
var item = rightLinks[ n ]; | |||
var parent = item.parentNode; | |||
var itemId = parent ? parent.id : ""; | |||
if ( itemId === "pt-notifications-alert" || itemId === "pt-notifications-notice" ) { | |||
if ( notifIndex < 0 ) { notifIndex = plainRightLinks.length; } | |||
if ( !notifHref ) { notifHref = item.href; } | |||
var raw = item.getAttribute( "data-counter-num" ); | |||
var count = parseInt( raw, 10 ); | |||
if ( isNaN( count ) ) { | |||
var match = ( item.textContent || "" ).match( /[((](\d+)[))]/ ); | |||
count = match ? parseInt( match[ 1 ], 10 ) : 0; | |||
} | |||
notifCount += count; | |||
continue; | |||
} | |||
plainRightLinks.push( item ); | |||
} | |||
rightLinks = plainRightLinks; | |||
var privateIndex = -1; | |||
for ( var p = 0; p < rightLinks.length; p++ ) { | |||
var pParent = rightLinks[ p ].parentNode; | |||
var pId = pParent ? pParent.id : ""; | |||
var pText = ( rightLinks[ p ].textContent || "" ).replace( /\s/g, "" ); | |||
if ( pId === "pt-mytalk" || pText === "\u8ba8\u8bba" ) { | |||
privateIndex = p + 1; | |||
break; | |||
} | |||
} | |||
if ( leftLinks.length === 0 && rightLinks.length === 0 && !notifHref ) { return; } | |||
var bar = document.createElement( "div" ); | var bar = document.createElement( "div" ); | ||
| 第84行: | 第138行: | ||
var right = document.createElement( "div" ); | var right = document.createElement( "div" ); | ||
right.className = "sg-bar-right"; | right.className = "sg-bar-right"; | ||
for ( var j = 0; j < rightLinks.length; j++ ) { | for ( var j = 0; j <= rightLinks.length; j++ ) { | ||
if ( j === privateIndex ) { | |||
var privateLink = document.createElement( "a" ); | |||
privateLink.href = "/index.php?title=Special:UserBoard"; | |||
privateLink.textContent = "\u79c1\u4fe1"; | |||
privateLink.title = "\u67e5\u770b\u79c1\u4fe1"; | |||
right.appendChild( privateLink ); | |||
} | |||
if ( j === notifIndex && notifHref ) { | |||
var notice = document.createElement( "a" ); | |||
notice.href = notifHref; | |||
notice.textContent = "\u901a\u77e5\uff08" + notifCount + "\uff09"; | |||
notice.title = "\u91cd\u8981\u901a\u77e5\u4e0e\u5e38\u89c4\u901a\u77e5"; | |||
right.appendChild( notice ); | |||
} | |||
if ( j === rightLinks.length ) { break; } | |||
var b = document.createElement( "a" ); | var b = document.createElement( "a" ); | ||
b.href = rightLinks[ j ].href; | b.href = rightLinks[ j ].href; | ||
| 第104行: | 第173行: | ||
enableHorizontalScroll( bar ); | enableHorizontalScroll( bar ); | ||
window.addEventListener( "resize", function () { syncBarHeight( bar ); } ); | window.addEventListener( "resize", function () { syncBarHeight( bar ); } ); | ||
} | |||
function moveSearch() { | |||
var search = document.getElementById( "p-search" ); | |||
var nav = document.getElementById( "p-navigation" ); | |||
if ( !search || !nav ) { return; } | |||
var container = nav.parentNode; | |||
if ( !container || search.parentNode !== container ) { return; } | |||
if ( container.firstChild === search ) { return; } | |||
container.insertBefore( search, container.firstChild ); | |||
} | } | ||
function run() { | function run() { | ||
moveTOC(); | moveTOC(); | ||
moveSearch(); | |||
makeSticky(); | makeSticky(); | ||
} | } | ||
| 第117行: | 第197行: | ||
} | } | ||
}() ); | }() ); | ||
2026年11月1日 (日) 04:16的最新版本
( function () {
function moveTOC() {
var toc = document.getElementById( "toc" );
if ( !toc ) { toc = document.querySelector( ".mw-table-of-contents-container" ); }
var sidebar = document.getElementById( "column-one" );
if ( !toc || !sidebar ) { return; }
var nav = document.getElementById( "p-navigation" );
var container = ( nav && nav.parentNode ) ? nav.parentNode : sidebar;
var existing = document.getElementById( "p-toc" );
if ( existing ) {
if ( nav ) {
container.insertBefore( existing, nav.nextSibling );
} else {
container.appendChild( existing );
}
return;
}
var box = document.createElement( "div" );
box.className = "portlet";
box.id = "p-toc";
var title = document.createElement( "h3" );
title.textContent = "\u76ee\u5f55";
var inner = document.createElement( "div" );
inner.className = "pBody";
inner.appendChild( toc );
box.appendChild( title );
box.appendChild( inner );
if ( nav ) {
container.insertBefore( box, nav.nextSibling );
} else {
var search = document.getElementById( "p-search" );
if ( search && search.parentNode ) {
search.parentNode.insertBefore( box, search );
} else {
container.appendChild( box );
}
}
}
function pickLinks( portlet ) {
var out = [];
if ( !portlet ) { return out; }
var links = portlet.querySelectorAll( "ul a" );
for ( var i = 0; i < links.length; i++ ) { out.push( links[ i ] ); }
return out;
}
function syncBarHeight( bar ) {
if ( !bar ) { return; }
document.body.style.paddingTop = bar.offsetHeight + "px";
}
function enableHorizontalScroll( bar ) {
if ( !bar ) { return; }
bar.addEventListener( "wheel", function ( e ) {
if ( Math.abs( e.deltaY ) > Math.abs( e.deltaX ) ) {
bar.scrollLeft += e.deltaY;
e.preventDefault();
}
}, { passive: false } );
var down = false, startX = 0, startLeft = 0;
bar.addEventListener( "mousedown", function ( e ) {
if ( e.target && e.target.tagName === "A" ) { return; }
down = true;
startX = e.pageX;
startLeft = bar.scrollLeft;
bar.style.cursor = "grabbing";
} );
window.addEventListener( "mousemove", function ( e ) {
if ( !down ) { return; }
bar.scrollLeft = startLeft - ( e.pageX - startX );
} );
window.addEventListener( "mouseup", function () {
down = false;
bar.style.cursor = "";
} );
}
function makeSticky() {
var old = document.getElementById( "sg-topbar" );
if ( old ) { old.parentNode.removeChild( old ); }
var leftLinks = pickLinks( document.getElementById( "p-cactions" ) );
var rightLinks = pickLinks( document.getElementById( "p-personal" ) );
var notifHref = null;
var notifCount = 0;
var notifIndex = -1;
var plainRightLinks = [];
for ( var n = 0; n < rightLinks.length; n++ ) {
var item = rightLinks[ n ];
var parent = item.parentNode;
var itemId = parent ? parent.id : "";
if ( itemId === "pt-notifications-alert" || itemId === "pt-notifications-notice" ) {
if ( notifIndex < 0 ) { notifIndex = plainRightLinks.length; }
if ( !notifHref ) { notifHref = item.href; }
var raw = item.getAttribute( "data-counter-num" );
var count = parseInt( raw, 10 );
if ( isNaN( count ) ) {
var match = ( item.textContent || "" ).match( /[((](\d+)[))]/ );
count = match ? parseInt( match[ 1 ], 10 ) : 0;
}
notifCount += count;
continue;
}
plainRightLinks.push( item );
}
rightLinks = plainRightLinks;
var privateIndex = -1;
for ( var p = 0; p < rightLinks.length; p++ ) {
var pParent = rightLinks[ p ].parentNode;
var pId = pParent ? pParent.id : "";
var pText = ( rightLinks[ p ].textContent || "" ).replace( /\s/g, "" );
if ( pId === "pt-mytalk" || pText === "\u8ba8\u8bba" ) {
privateIndex = p + 1;
break;
}
}
if ( leftLinks.length === 0 && rightLinks.length === 0 && !notifHref ) { return; }
var bar = document.createElement( "div" );
bar.id = "sg-topbar";
bar.className = "sg-sticky-bar";
var left = document.createElement( "div" );
left.className = "sg-bar-left";
for ( var i = 0; i < leftLinks.length; i++ ) {
var a = document.createElement( "a" );
a.href = leftLinks[ i ].href;
a.textContent = leftLinks[ i ].textContent;
left.appendChild( a );
}
var right = document.createElement( "div" );
right.className = "sg-bar-right";
for ( var j = 0; j <= rightLinks.length; j++ ) {
if ( j === privateIndex ) {
var privateLink = document.createElement( "a" );
privateLink.href = "/index.php?title=Special:UserBoard";
privateLink.textContent = "\u79c1\u4fe1";
privateLink.title = "\u67e5\u770b\u79c1\u4fe1";
right.appendChild( privateLink );
}
if ( j === notifIndex && notifHref ) {
var notice = document.createElement( "a" );
notice.href = notifHref;
notice.textContent = "\u901a\u77e5\uff08" + notifCount + "\uff09";
notice.title = "\u91cd\u8981\u901a\u77e5\u4e0e\u5e38\u89c4\u901a\u77e5";
right.appendChild( notice );
}
if ( j === rightLinks.length ) { break; }
var b = document.createElement( "a" );
b.href = rightLinks[ j ].href;
b.textContent = rightLinks[ j ].textContent;
right.appendChild( b );
}
bar.appendChild( left );
bar.appendChild( right );
document.body.appendChild( bar );
var personal = document.getElementById( "p-personal" );
var cactions = document.getElementById( "p-cactions" );
if ( personal ) { personal.style.visibility = "hidden"; }
if ( cactions ) { cactions.style.visibility = "hidden"; }
document.body.className = document.body.className + " sg-sticky-on";
syncBarHeight( bar );
enableHorizontalScroll( bar );
window.addEventListener( "resize", function () { syncBarHeight( bar ); } );
}
function moveSearch() {
var search = document.getElementById( "p-search" );
var nav = document.getElementById( "p-navigation" );
if ( !search || !nav ) { return; }
var container = nav.parentNode;
if ( !container || search.parentNode !== container ) { return; }
if ( container.firstChild === search ) { return; }
container.insertBefore( search, container.firstChild );
}
function run() {
moveTOC();
moveSearch();
makeSticky();
}
if ( document.readyState === "loading" ) {
document.addEventListener( "DOMContentLoaded", run );
} else {
run();
}
}() );