MediaWiki:Common.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* ── AllPages: filter excluded pages from homepage list ───────────── */
$( function () {
var chunk = document.querySelector( '.mw-allpages-chunk' );
if ( !chunk ) { return; }
// Always hide the main page entry
chunk.querySelectorAll( 'li' ).forEach( function ( li ) {
var a = li.querySelector( 'a' );
if ( a && ( a.title === '首页' || a.title === 'Main Page' ) ) {
li.remove();
}
} );
// Fetch server-side exclusion list and apply
new mw.Api().get( {
action: 'query', prop: 'revisions', rvprop: 'content',
titles: 'MediaWiki:Allpages-exclude', format: 'json'
} ).done( function ( data ) {
var page = Object.values( data.query.pages )[ 0 ];
if ( !page || page.missing !== undefined || !page.revisions ) { return; }
var excluded = page.revisions[ 0 ][ '*' ]
.split( '\n' ).map( function ( s ) { return s.trim(); } ).filter( Boolean );
chunk.querySelectorAll( 'li' ).forEach( function ( li ) {
var a = li.querySelector( 'a' );
if ( a && excluded.indexOf( a.title ) !== -1 ) { li.remove(); }
} );
} );
} );
/* ── "Hide from homepage" button — visible to sysops only ─────────── */
$( function () {
var groups = mw.config.get( 'wgUserGroups' ) || [];
if ( groups.indexOf( 'sysop' ) === -1 ) { return; }
var pageTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
var mainPage = '首页';
if ( pageTitle === mainPage ) { return; } // don't show on the main page itself
var btn = document.createElement( 'button' );
btn.id = 'hp-hide-btn';
btn.innerHTML = '🚫 Hide from homepage';
btn.title = 'Add this page to the homepage AllPages exclusion list';
document.body.appendChild( btn );
btn.addEventListener( 'click', function () {
btn.disabled = true;
btn.textContent = 'Saving…';
var api = new mw.Api();
api.get( {
action: 'query', prop: 'revisions', rvprop: 'content',
titles: 'MediaWiki:Allpages-exclude', format: 'json'
} ).done( function ( data ) {
var page = Object.values( data.query.pages )[ 0 ];
var lines = [];
if ( page && page.revisions ) {
lines = page.revisions[ 0 ][ '*' ]
.split( '\n' ).map( function ( s ) { return s.trim(); } ).filter( Boolean );
}
if ( lines.indexOf( pageTitle ) !== -1 ) {
mw.notify( '"' + pageTitle + '" is already hidden from the homepage.', { type: 'info' } );
btn.textContent = '✓ Already hidden';
return;
}
lines.push( pageTitle );
api.postWithToken( 'csrf', {
action: 'edit',
title: 'MediaWiki:Allpages-exclude',
text: lines.join( '\n' ),
summary: 'Hide "' + pageTitle + '" from homepage list'
} ).done( function () {
mw.notify( '"' + pageTitle + '" hidden from homepage list.', { type: 'success' } );
btn.innerHTML = '✓ Hidden';
btn.style.background = '#38a169';
} ).fail( function () {
mw.notify( 'Failed — check you have interface-admin rights.', { type: 'error' } );
btn.disabled = false;
btn.innerHTML = '🚫 Hide from homepage';
} );
} );
} );
} );