User:Guycn2/ResetPrefs.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$( async () => {
	
	if (
		mw.config.get( 'wgCanonicalNamespace' ) === 'Special' ||
		mw.config.get( 'wgAction' ) !== 'view'
	) {
		mw.notify( 'resetPrefs was NOT executed.' );
		return;
	}
	
	await mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] );
	
	const api = new mw.Api();
	
	document.addEventListener( 'keypress', async () => {
		
		mw.util.$content.fadeTo( '_default', 0.25 );
		
		try {
			await resetPrefs( api, 'globalpreferences' );
		} catch ( e ) {
			return;
		}
		
		await wait( 300 );
		
		try {
			await resetPrefs( api, 'globalpreferenceoverrides' );
		} catch ( e ) {
			return;
		}
		
		await wait( 300 );
		
		try {
			await resetPrefs( api, 'options' );
		} catch ( e ) {
			return;
		}
		
		await wait( 500 );
		
		window.close();
		
	} );
	
	async function resetPrefs( apiObj, action ) {
		
		const defer = $.Deferred();
		
		try {
			
			await apiObj.postWithToken( 'csrf', {
				action: action,
				reset: true,
				resetkinds: 'all'
			} );
			
		} catch ( e ) {
			
			mw.notify( `Action "${ action }" failed. Error code: ${ e }`, {
				title: 'Error!',
				type: 'error',
				autoHide: false
			} );
			
			return defer.reject();
			
		}
		
		await wait( 1000 );
		
		mw.notify( `Action "${ action }" succeeded.`, {
			title: 'Success!',
			type: 'success',
			autoHide: false
		} );
		
		return defer.resolve();
		
	}
	
	async function wait( ms ) {
		const defer = $.Deferred();
		setTimeout( () => defer.resolve(), ms );
		return defer;
	}
	
} );