User:KnightMiner/editSummaryPresets.js

var editsummOriginalSummary = '';

// Save the original value of the edit summary field function editsummOnCannedSummarySelected { editsummOriginalSummary = document.getElementById( 'wpSummary' ); if ( editsummOriginalSummary ) { editsummOriginalSummary = editsummOriginalSummary.value; } else { editsummOriginalSummary = ''; }	var idx = this.selectedIndex, canned = this.options[idx].text, newSummary = editsummOriginalSummary;

// Append old edit summary with space, if exists, // and last character != space if (newSummary.length !== 0 && newSummary.charAt(newSummary.length - 1) !== ' ') { newSummary += ' '; }	newSummary += canned; document.getElementById( 'wpSummary' ).value = newSummary; }

// Loop through siblings, looking for editCheckboxes class $( function {	var insertBeforeThis = document.getElementById( 'wpSummary' );	while ( insertBeforeThis ) {		if ( insertBeforeThis.className === 'editCheckboxes' ) {			break;		}		insertBeforeThis = insertBeforeThis.nextSibling;	}

// If we failed to find the editCheckboxes class, or insertBeforeThis is null if ( !insertBeforeThis || insertBeforeThis.className !== 'editCheckboxes' ) { return; }	editsummOriginalSummary = editsummOriginalSummary.value;

// Create the dropdown box var dropdown = document.createElement("select"); dropdown.style.width = "50%"; dropdown.style.margin = "0 4px 0 0"; dropdown.onchange = editsummOnCannedSummarySelected;

// Add the default "Summary presets" to the box var option = document.createElement("option"); option.disabled = true; option.selected = true; option.appendChild( document.createTextNode( 'Summary presets' ) ); dropdown.appendChild( option );

// Add support for the {user} keyword var username; mw.loader.using( 'mediawiki.api', function {		new mw.Api.get( { action: 'query', titles: mw.config.get( 'wgPageName' ), prop: 'revisions' } ).done( function( data ) { username = data.query.pages[mw.config.get( 'wgArticleId' )].revisions[0].user;

// Add summaries based on user's variable var optgroup, noSummaries; for ( var i=0; i<customSummaries.length; i++ ) { if ( typeof customSummaries[i] !== 'undefined' ) { if ( customSummaries[i].search( '== ' ) === 0 ) { var namespaceLimit = customSummaries[i].replace( /^== ((TALK:|MAIN:|\d+:)*).*/, '$1' ); console.log( namespaceLimit ); if (							namespaceLimit == '' ||							namespaceLimit.search( mw.config.get( 'wgNamespaceNumber' ) + ':' ) !== -1 ||							(namespaceLimit.search( 'TALK:' ) !== -1) && mw.config.get( 'wgNamespaceNumber' ) % 2 == 1 ||							(namespaceLimit.search( 'MAIN:' ) !== -1) && mw.config.get( 'wgNamespaceNumber' ) % 2 == 0 						) { noSummaries = false; if ( optgroup ) { dropdown.appendChild( optgroup ); }							optgroup = document.createElement( 'optgroup' ); optgroup.label = customSummaries[i].replace( /^== (TALK:|MAIN:|\d+:)*/, '' ); } else { noSummaries = true; }					} else { if ( !noSummaries ) { var append; if ( optgroup ) { append = optgroup; } else { append = dropdown; }							var option = document.createElement( 'option' ), optionText = document.createTextNode( customSummaries[i].replace( /\{user\}/g, username ) ); option.appendChild( optionText ); append.appendChild( option ); }					}				}			}			if ( optgroup ) { dropdown.appendChild( optgroup ); }

var theParent = insertBeforeThis.parentNode; theParent.insertBefore(dropdown, insertBeforeThis); theParent.insertBefore(document.createElement("br"), dropdown); } );	} ); } );