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

MediaWiki:Common.js:修订间差异

MediaWiki界面页面
修复退出拦截
拦截AJAX退出
第8行: 第8行:
         .finally( function() { window.location.href = WIKI_BASE + '/wiki/首页'; } );
         .finally( function() { window.location.href = WIKI_BASE + '/wiki/首页'; } );
     }
     }
    // 拦截 fetch 请求
    var origFetch = window.fetch;
    window.fetch = function() {
        var url = arguments[0] || '';
        if ( typeof url === 'string' && ( url.includes('action=logout') || url.includes('UserLogout') ) ) {
            doLogout();
            return Promise.resolve( new Response('{}') );
        }
        return origFetch.apply( this, arguments );
    };
    // 拦截 XMLHttpRequest
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function( method, url ) {
        if ( url && ( url.includes('action=logout') || url.includes('UserLogout') ) ) {
            doLogout();
            return;
        }
        return origOpen.apply( this, arguments );
    };


     function syncLoginState() {
     function syncLoginState() {
         if ( mw.config.get( 'wgUserId' ) !== 0 ) return;
         if ( mw.config.get( 'wgUserId' ) !== 0 ) return;
         fetch( MAIN_SITE + '/api/auth/verify', { method: 'POST', credentials: 'include' } )
         origFetch( MAIN_SITE + '/api/auth/verify', { method: 'POST', credentials: 'include' } )
         .then( function(r) { return r.json(); } )
         .then( function(r) { return r.json(); } )
         .then( function(d) {
         .then( function(d) {
第18行: 第39行:
             }
             }
         } ).catch(function(){});
         } ).catch(function(){});
    }
    function setupLogout() {
        $( document ).on( 'click', '[href*="action=logout"], [href*="UserLogout"], [data-mw-logouturl]', function(e) {
            e.preventDefault();
            e.stopImmediatePropagation();
            doLogout();
            return false;
        } );
        $( document ).on( 'click', function(e) {
            var $target = $( e.target ).closest( 'a, button' );
            var href = $target.attr( 'href' ) || '';
            if ( href.includes( 'logout' ) || href.includes( 'UserLogout' ) ) {
                e.preventDefault();
                e.stopImmediatePropagation();
                doLogout();
                return false;
            }
        } );
     }
     }


第64行: 第66行:
         $( document ).ready( function() {
         $( document ).ready( function() {
             syncLoginState();
             syncLoginState();
            setupLogout();
             interceptLoginButton();
             interceptLoginButton();
             interceptRegisterButton();
             interceptRegisterButton();

2026年4月15日 (三) 02:14的版本

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

    function doLogout() {
        fetch( MAIN_SITE + '/api/auth/cookie-logout', { method: 'POST', credentials: 'include' } )
        .finally( function() { window.location.href = WIKI_BASE + '/wiki/首页'; } );
    }

    // 拦截 fetch 请求
    var origFetch = window.fetch;
    window.fetch = function() {
        var url = arguments[0] || '';
        if ( typeof url === 'string' && ( url.includes('action=logout') || url.includes('UserLogout') ) ) {
            doLogout();
            return Promise.resolve( new Response('{}') );
        }
        return origFetch.apply( this, arguments );
    };

    // 拦截 XMLHttpRequest
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function( method, url ) {
        if ( url && ( url.includes('action=logout') || url.includes('UserLogout') ) ) {
            doLogout();
            return;
        }
        return origOpen.apply( this, arguments );
    };

    function syncLoginState() {
        if ( mw.config.get( 'wgUserId' ) !== 0 ) return;
        origFetch( MAIN_SITE + '/api/auth/verify', { method: 'POST', credentials: 'include' } )
        .then( function(r) { return r.json(); } )
        .then( function(d) {
            if ( d.valid && d.user && !window.location.search.includes('sso_refresh') ) {
                window.location.href = window.location.href + ( window.location.search ? '&' : '?' ) + 'sso_refresh=1';
            }
        } ).catch(function(){});
    }

    function interceptLoginButton() {
        $( document ).on( 'click', 'a[href*="Special:UserLogin"], a[href*="action=login"]', function(e) {
            e.preventDefault();
            window.location.href = MAIN_SITE + '/login.html?redirect=' + encodeURIComponent( window.location.href );
        } );
    }

    function interceptRegisterButton() {
        $( document ).on( 'click', 'a[href*="Special:CreateAccount"], a[href*="action=createaccount"]', function(e) {
            e.preventDefault();
            window.location.href = MAIN_SITE + '/register.html?redirect=' + encodeURIComponent( window.location.href );
        } );
    }

    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() {
            syncLoginState();
            interceptLoginButton();
            interceptRegisterButton();
            interceptEditForAnon();
        } );
    } );
}() );