MediaWiki:Common.js
来自神国反恶俗
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
( function () {
injectFooterLogoStyle();
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; }
if ( document.getElementById( "p-toc" ) ) { 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 );
sidebar.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" ) );
if ( leftLinks.length === 0 && rightLinks.length === 0 ) { 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++ ) {
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 injectFooterLogoStyle() {
if ( document.getElementById( "sg-footer-logo-style" ) ) { return; }
var style = document.createElement( "style" );
style.id = "sg-footer-logo-style";
style.textContent = [
"#f-copyrightico{visibility:hidden !important;}",
"#f-copyrightico.sg-floating-logo{display:block !important;visibility:visible !important;position:fixed !important;left:12px !important;bottom:46px !important;z-index:9500 !important;background:#fff !important;padding:4px !important;border-radius:4px !important;line-height:0 !important;}",
"@media (max-width:720px){#f-copyrightico{visibility:visible !important;}#f-copyrightico.sg-floating-logo{position:static !important;left:auto !important;bottom:auto !important;padding:0 !important;background:transparent !important;}}"
].join( "\\n" );
( document.head || document.documentElement ).appendChild( style );
}
function moveFooterLogo() {
var el = document.getElementById( "f-copyrightico" );
if ( !el ) { return; }
if ( window.innerWidth <= 720 ) {
el.style.visibility = "visible";
return;
}
document.body.appendChild( el );
el.classList.add( "sg-floating-logo" );
el.style.visibility = "visible";
}
function run() {
moveTOC();
makeSticky();
moveFooterLogo();
}
if ( document.readyState === "loading" ) {
document.addEventListener( "DOMContentLoaded", run );
} else {
run();
}
}() );