User:Aaron Liu/sandbox.js

//This script does not function without additional "helper" modules!

//Please see Wikipedia:AutoEd for details on use.

//Initiates AutoEd

function autoEdExecute( ve ) {

if ( typeof ve === 'undefined' ) ve = false;

if(!document.getElementById('wpTextbox1')) 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.js error', type: 'error' } );

return;

}

autoEdFunctions();

if ( !ve ) {

autoEdEditSummary( false );

} else {

var 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() {

var handler = function () {

autoEdExecute( true );

mw.hook( 've.wikitextInteractive' ).remove( handler );

};

mw.hook( 've.wikitextInteractive' ).add( handler );

}

//Adds Tag to edit summary textbox

function autoEdEditSummary( ve ) {

var txt, tag;

if ( !ve )

txt = document.forms.editform.wpSummary;

else

txt = ve.init.target.saveDialog.editSummaryInput.$input[0];

if( typeof autoEdTag === 'undefined' ) {

tag = 'Cleaned up using AutoEd';

} else {

tag = autoEdTag;

}

// Is the tag blank?

if( tag.match(/[^\s]/) ) {

// Has it already been tagged?

if( txt.value.indexOf(tag) == -1 ) {

// 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 ( !ve )

document.forms.editform.wpMinoredit.checked = true;

else

ve.init.target.checkboxesByName.$input[ 0 ].checked = true;

}

// Click 'Show changes'

if( typeof autoEdClick === 'undefined' || autoEdClick ) {

if ( !ve )

document.forms.editform.wpDiff.click();

else

ve.init.target.saveDialog.actions.list.find( function ( 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(function () {

var $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' ) ) {

var 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')

));

var veTarget = (window.ve && ve.init && ve.init.target) ? ve.init.target : null;

if ( typeof document.forms.editform !== 'undefined' ) {

$link.on('click', function (e) {

e.preventDefault();

autoEdExecute();

});

} else {

// Handle source-mode VE is used for wikitext editing

mw.loader.using( 'user.options', function () {

if ( mw.user.options.get( 'visualeditor-newwikitext' ) == '1' ) {

$link.on( 'click', function (e) {

e.preventDefault();

if ( veTarget )

if ( veTarget.mode === "visual" )

veTarget.switchToWikitextEditor();

// Don't click if we're already in source-mode VE

else

document.getElementById( 'ca-edit' ).click();

autoEdVeExecute();

} );

}

} );

}

}

});