跳转到内容

MediaWiki:Common.js:修订间差异

来自槌基百科
Add JS to hide 首页 from AllPages list
 
Add AllPages exclusion filter + sysop hide-from-homepage button
第1行: 第1行:
/* Common.js — site-wide JavaScript */
/* ── AllPages: filter excluded pages from homepage list ───────────── */
$( function () {
    var chunk = document.querySelector( '.mw-allpages-chunk' );
    if ( !chunk ) { return; }


/* Hide the main page (首页) entry from the AllPages list on the homepage */
    // Always hide the main page entry
$( function () {
     chunk.querySelectorAll( 'li' ).forEach( function ( li ) {
     var allpages = document.querySelector( '.mw-allpages-chunk' );
    if ( !allpages ) { return; }
    allpages.querySelectorAll( 'li' ).forEach( function ( li ) {
         var a = li.querySelector( 'a' );
         var a = li.querySelector( 'a' );
         if ( a && ( a.title === '首页' || a.title === 'Main Page' ) ) {
         if ( a && ( a.title === '首页' || a.title === 'Main Page' ) ) {
             li.remove();
             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';
            } );
        } );
     } );
     } );
} );
} );

2026年3月12日 (四) 12:38的版本

/* ── 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';
            } );
        } );
    } );
} );