Jump to content

User:Aaron Liu/sandbox.js

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Aaron Liu (talk | contribs) at 22:34, 26 June 2025 (workaround doesn't work). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// This script does not function without additional "helper" modules!
// Please see [[Wikipedia:AutoEd]] for details on use.

// Initiates AutoEd
function autoEdExecute( isVe ) {
	if ( typeof isVe === 'undefined' ) {
		isVe = false;
	}

	if ( !document.getElementById( 'wpTextbox1' ) ) {
		let tempEditor = document.getElementsByClassName( 've-init-mw-tempWikitextEditorWidget' );
		if ( isVe && tempEditor.length !== 0 ) {
			tempEditor = tempEditor[ 0 ];
			tempEditor.id = 'wpTextbox1';
			const handler = function () {
				tempEditor.id = '';
				mw.hook( 've.activationComplete' ).remove( handler );
			};
			mw.hook( 've.activationComplete' ).add( handler );
		} else {
			return;
		}
	}

	// copy wikEd ([[User:Cacycle/wikEd.js]]) frame to wpTextbox1 textarea
	// for compatibility with WikiEd
	if ( typeof wikEdUseWikEd !== 'undefined' ) {
		if ( wikEdUseWikEd === true ) {
			WikEdUpdateTextarea();
		}
	}

	// alert/return if autoEdFunctions is not defined
	if ( typeof autoEdFunctions === 'undefined' ) {
		mw.notify( 'autoEdFunctions is undefined', { title: 'AutoEd core error', type: 'error' } );
		return;
	}

	autoEdFunctions();
	if ( !isVe ) {
		autoEdEditSummary( false );
	} else {
		const handler = function () {
			autoEdEditSummary( true );
			mw.hook( 've.saveDialog.stateChanged' ).remove( handler );
		};
		mw.hook( 've.saveDialog.stateChanged' ).add( handler );
	}

	// copy wpTextbox1 textarea back to wikEd frame
	// for compatibility with WikiEd
	if ( typeof wikEdUseWikEd !== 'undefined' ) {
		if ( wikEdUseWikEd === true ) {
			WikEdUpdateFrame();
		}
	}
}

// Execute AutoEd after source-mode VE loads through hook
function autoEdVeExecute() {
	const handler = function () {
		autoEdExecute( true );
		mw.hook( 've.wikitextInteractive' ).remove( handler );
	};
	mw.hook( 've.wikitextInteractive' ).add( handler );
}

// Adds Tag to edit summary textbox
function autoEdEditSummary( isVe ) {
	let txt, tag;
	if ( !isVe ) {
		txt = document.forms.editform.wpSummary;
	} else {
		txt = ve.init.target.saveDialog.editSummaryInput.$input[ 0 ];
	}

	if ( typeof autoEdTag === 'undefined' ) {
		tag = 'Cleaned up using [[WP:AutoEd|AutoEd]]';
	} else {
		tag = autoEdTag;
	}

	// Is the tag blank?
	if ( tag.match( /\S/ ) ) {
		// Has it already been tagged?
		if ( !txt.value.includes( tag ) ) {
			// Append a pipe if necessary
			if ( txt.value.match( /[^*\/\s][^\/\s]?\s*$/ ) ) {
				txt.value += ' | ';
			}
			// Append our tag
			txt.value += tag;
		}
	}

	// Check 'This is a minor edit'
	if ( typeof autoEdMinor === 'undefined' || autoEdMinor ) {
		if ( !isVe ) {
			document.forms.editform.wpMinoredit.checked = true;
		} else {
			ve.init.target.checkboxesByName.wpMinoredit.$input[ 0 ].checked = true;
		}
	}

	// Click 'Show changes'
	if ( typeof autoEdClick === 'undefined' || autoEdClick ) {
		if ( !isVe ) {
			document.forms.editform.wpDiff.click();
		} else {
			ve.init.target.saveDialog.actions.list.find( ( it ) => it.action === 'review' )
				.$button[ 0 ].click();
		}
	}
}

// Add "auto ed" tab and associate with actions
// Make sure the document is ready and our dependencies are loaded
$.when( $.ready, mw.loader.using( [ 'mediawiki.util' ] ) ).done( () => {
	let $link;

	// Execute AutoEd after call from "view mode"
	if ( mw.util.getParamValue( 'AutoEd' ) ) {
		if ( document.documentElement.classList.contains( 've-loading' ) ) {
			autoEdVeExecute();
		} else {
			autoEdExecute();
		}
	}

	// Set default values for any unset variables
	if ( typeof autoEdLinkHover === 'undefined' ) {
		autoEdLinkHover = 'Run AutoEd';
	}
	if ( typeof autoEdLinkName === 'undefined' ) {
		autoEdLinkName = 'auto ed';
	}
	if ( typeof autoEdLinkLocation === 'undefined' ) {
		autoEdLinkLocation = 'p-cactions';
	}

	// Add the "auto ed" tab
	if ( document.getElementById( 'ca-edit' ) && !document.getElementById( 'ca-AutoEd' ) ) {
		const url = mw.util.getUrl( mw.config.get( 'wgPageName' ), { action: 'edit', AutoEd: 'true' } );
		$link = $( mw.util.addPortletLink(
			autoEdLinkLocation,
			url,
			autoEdLinkName,
			'ca-AutoEd',
			autoEdLinkHover,
			'',
			document.getElementById( 'ca-move' )
		) );

		if ( typeof document.forms.editform !== 'undefined' ) {
			$link.on( 'click', ( e ) => {
				e.preventDefault();
				autoEdExecute();
			} );
		} else {
			// Handle source-mode VE is used for wikitext editing
			mw.loader.using( 'user.options', () => {
				if ( mw.user.options.get( 'visualeditor-newwikitext' ) === '1' ) {
					if ( mw.user.options.get( 'visualeditor-tabs' ) === 'prefer-ve' ) {
						$link.on( 'click', ( e ) => {
							e.preventDefault();
							if ( !( window.ve && ve.init && ve.init.target ) ) {
								document.getElementById( 'ca-edit' ).click();
							}
							const handler = function () {
								ve.init.target.switchToWikitextEditor();
								mw.hook( 've.activationComplete' ).remove( handler );
							};
							mw.hook( 've.activationComplete' ).add( handler );
							autoEdVeExecute(); // This won't work until T397778 fixed
						} );
					} else {
						$link.on( 'click', ( e ) => {
							e.preventDefault();
							const veTarget = ( window.ve && ve.init && ve.init.target ) ?
								ve.init.target : null;
							if ( veTarget && veTarget.surface ) {
								if ( veTarget.surface.mode === 'visual' ) {
									veTarget.switchToWikitextEditor();
								}
								// Don't click if we're already in source-mode VE
							} else {
								document.getElementById( 'ca-edit' ).click();
							}
							autoEdVeExecute();
						} );
					}
				}
			} );
		}
	}
} );