MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 997: | Line 997: | ||
function buildLogoutForm( token ) { | function buildLogoutForm( token ) { | ||
// | token = token || ''; | ||
var esc = String( token ).replace( /"/g, '"' ); | |||
var script = ( window.mw && mw.util && mw.util.wikiScript ) ? mw.util.wikiScript() : '/index.php'; | |||
var form = document.createElement( 'form' ); | var form = document.createElement( 'form' ); | ||
form.method = 'post'; | form.method = 'post'; | ||
form.action = | form.action = script + '?title=Special:UserLogout'; | ||
form.style.display = 'none'; | form.style.display = 'none'; | ||
form.innerHTML = | form.innerHTML = | ||
'<input type="hidden" name="wpEditToken" value="' + | '<input type="hidden" name="wpEditToken" value="' + esc + '">' + | ||
'<input type="hidden" name="title" value="Special:UserLogout">' + | '<input type="hidden" name="title" value="Special:UserLogout">' + | ||
'<input type="hidden" name="returnto" value="Main Page">'; | '<input type="hidden" name="returnto" value="Main Page">'; | ||
| Line 1,072: | Line 1,074: | ||
document.getElementById( 'gr-logout-link' ).addEventListener( 'click', function ( e ) { | document.getElementById( 'gr-logout-link' ).addEventListener( 'click', function ( e ) { | ||
e.preventDefault(); | e.preventDefault(); | ||
buildLogoutForm( token ). | // Fetch a fresh CSRF token at click time (more reliable than at load time) | ||
if ( window.mw && mw.user && mw.user.tokens ) { | |||
var t = mw.user.tokens.get( 'csrfToken' ); | |||
if ( t ) { buildLogoutForm( t ).submit(); return; } | |||
} | |||
// Fallback: use the API to get a logout token, then submit | |||
if ( window.mw && mw.Api ) { | |||
new mw.Api().postWithToken( 'csrf', { action: 'logout' } ) | |||
.done( function () { location.href = '/Main_Page'; } ) | |||
.fail( function () { location.href = '/index.php?title=Special:UserLogout'; } ); | |||
} else { | |||
location.href = '/index.php?title=Special:UserLogout'; | |||
} | |||
} ); | } ); | ||
} | } | ||