打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
初始化
 
拦截退出按钮
第1行: 第1行:
/**
* MediaWiki:Common.js
* 任务 6.3:未登录用户访问需要编辑权限的页面时,引导到主站登录
* 将此内容粘贴到 Wiki 站的 MediaWiki:Common.js 页面
*/
( function () {
( function () {
     'use strict';
     'use strict';
    var MAIN_SITE = 'https://www.dolshipmaker.vip';
    var WIKI_BASE = 'https://wiki.dolshipmaker.vip';


     var MAIN_LOGIN_URL = 'https://dolshipmaker.vip/login.html';
     function interceptLogout() {
    var WIKI_BASE_URL  = 'https://wiki.dolshipmaker.vip';
        $( document ).on( 'click', 'a[href*="action=logout"], a[href*="Special:UserLogout"]', function ( e ) {
 
            e.preventDefault();
    /**
            e.stopPropagation();
    * 检测当前是否为编辑/上传等需要登录的操作,
            fetch( MAIN_SITE + '/api/auth/cookie-logout', {
    * 若未登录则重定向到主站登录页。
                method: 'POST',
    */
                credentials: 'include'
    function checkAuthForEditActions() {
            } ).finally( function () {
        var action = mw.config.get( 'wgAction' );
                window.location.href = MAIN_SITE + '/login.html?redirect=' + encodeURIComponent( WIKI_BASE );
        var isLoggedIn = mw.config.get( 'wgUserId' ) !== 0;
            } );
 
         } );
        if ( isLoggedIn ) {
            return;
        }
 
        // 需要登录的操作
        var protectedActions = [ 'edit', 'submit', 'upload' ];
        if ( protectedActions.indexOf( action ) !== -1 ) {
            var returnUrl = encodeURIComponent( window.location.href );
            window.location.href = MAIN_LOGIN_URL + '?redirect=' + returnUrl;
         }
     }
     }


    /**
     function interceptEditForAnon() {
    * 为页面内的"编辑"链接添加登录引导
         if ( mw.config.get( 'wgUserId' ) !== 0 ) return;
    */
         $( document ).on( 'click', 'a[href*="action=edit"], a[href*="veaction=edit"]', function ( e ) {
     function patchEditLinks() {
         var isLoggedIn = mw.config.get( 'wgUserId' ) !== 0;
        if ( isLoggedIn ) {
            return;
        }
 
         $( 'a[href*="action=edit"], a[href*="veaction=edit"]' ).on( 'click', function ( e ) {
             e.preventDefault();
             e.preventDefault();
            var returnUrl = encodeURIComponent( window.location.href );
             window.location.href = MAIN_SITE + '/login.html?redirect=' + encodeURIComponent( window.location.href );
             window.location.href = MAIN_LOGIN_URL + '?redirect=' + returnUrl;
         } );
         } );
     }
     }


     mw.loader.using( 'mediawiki.util' ).done( function () {
     mw.loader.using( 'mediawiki.util' ).done( function () {
        checkAuthForEditActions();
         $( document ).ready( function () {
         $( document ).ready( patchEditLinks );
            interceptLogout();
            interceptEditForAnon();
        } );
     } );
     } );
}() );
}() );

2026年4月14日 (二) 23:24的版本

( function () {
    'use strict';
    var MAIN_SITE = 'https://www.dolshipmaker.vip';
    var WIKI_BASE = 'https://wiki.dolshipmaker.vip';

    function interceptLogout() {
        $( document ).on( 'click', 'a[href*="action=logout"], a[href*="Special:UserLogout"]', function ( e ) {
            e.preventDefault();
            e.stopPropagation();
            fetch( MAIN_SITE + '/api/auth/cookie-logout', {
                method: 'POST',
                credentials: 'include'
            } ).finally( function () {
                window.location.href = MAIN_SITE + '/login.html?redirect=' + encodeURIComponent( WIKI_BASE );
            } );
        } );
    }

    function interceptEditForAnon() {
        if ( mw.config.get( 'wgUserId' ) !== 0 ) return;
        $( document ).on( 'click', 'a[href*="action=edit"], a[href*="veaction=edit"]', function ( e ) {
            e.preventDefault();
            window.location.href = MAIN_SITE + '/login.html?redirect=' + encodeURIComponent( window.location.href );
        } );
    }

    mw.loader.using( 'mediawiki.util' ).done( function () {
        $( document ).ready( function () {
            interceptLogout();
            interceptEditForAnon();
        } );
    } );
}() );