MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
自定义退出URL |
MutationObserver替换退出链接 |
||
| 第4行: | 第4行: | ||
var WIKI_BASE = 'https://wiki.dolshipmaker.vip'; | var WIKI_BASE = 'https://wiki.dolshipmaker.vip'; | ||
var LOGOUT_URL = WIKI_BASE + '/wiki/首页?sso_action=logout'; | var LOGOUT_URL = WIKI_BASE + '/wiki/首页?sso_action=logout'; | ||
// 尽早替换退出链接,在 DOM ready 之前 | |||
function replaceLogoutLinks() { | |||
document.querySelectorAll( 'a' ).forEach( function( a ) { | |||
if ( a.href && ( a.href.includes('action=logout') || a.href.includes('UserLogout') ) ) { | |||
a.href = LOGOUT_URL; | |||
a.removeAttribute('data-mw'); | |||
// 移除所有已绑定的事件 | |||
var newA = a.cloneNode( true ); | |||
a.parentNode.replaceChild( newA, a ); | |||
} | |||
} ); | |||
} | |||
// 用 MutationObserver 监控 DOM 变化,实时替换 | |||
var observer = new MutationObserver( function() { | |||
replaceLogoutLinks(); | |||
} ); | |||
observer.observe( document.documentElement, { childList: true, subtree: true } ); | |||
// 立即执行一次 | |||
replaceLogoutLinks(); | |||
function syncLoginState() { | function syncLoginState() { | ||
| 第14行: | 第36行: | ||
} | } | ||
} ).catch(function(){}); | } ).catch(function(){}); | ||
} | } | ||
| 第53行: | 第63行: | ||
$( document ).ready( function() { | $( document ).ready( function() { | ||
syncLoginState(); | syncLoginState(); | ||
replaceLogoutLinks(); | |||
interceptLoginButton(); | interceptLoginButton(); | ||
interceptRegisterButton(); | interceptRegisterButton(); | ||
2026年4月15日 (三) 02:23的版本
( function () {
'use strict';
var MAIN_SITE = 'https://www.dolshipmaker.vip';
var WIKI_BASE = 'https://wiki.dolshipmaker.vip';
var LOGOUT_URL = WIKI_BASE + '/wiki/首页?sso_action=logout';
// 尽早替换退出链接,在 DOM ready 之前
function replaceLogoutLinks() {
document.querySelectorAll( 'a' ).forEach( function( a ) {
if ( a.href && ( a.href.includes('action=logout') || a.href.includes('UserLogout') ) ) {
a.href = LOGOUT_URL;
a.removeAttribute('data-mw');
// 移除所有已绑定的事件
var newA = a.cloneNode( true );
a.parentNode.replaceChild( newA, a );
}
} );
}
// 用 MutationObserver 监控 DOM 变化,实时替换
var observer = new MutationObserver( function() {
replaceLogoutLinks();
} );
observer.observe( document.documentElement, { childList: true, subtree: true } );
// 立即执行一次
replaceLogoutLinks();
function syncLoginState() {
if ( mw.config.get( 'wgUserId' ) !== 0 ) return;
fetch( 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();
replaceLogoutLinks();
interceptLoginButton();
interceptRegisterButton();
interceptEditForAnon();
} );
} );
}() );