User:KnightMiner/editSummaryPresets.js

/** * Function to append the summary */ var input; function onSelectSummaries { if (!input) { // find its input and cache for later var inputs = $('#wpSummaryWidget input'); if(inputs.length === 0) { return; }		input = inputs[0]; }	var newSummary = input.value;

// Append old edit summary with space, if exists, // and last character != space if(newSummary.length > 0 && newSummary.charAt(newSummary.length - 1) !== ' ') { newSummary += ' '; }	newSummary += this.options[this.selectedIndex].text; input.value = newSummary; }

/** * Helper to create the option elements */ function createOption(optionText, username = null) { var option = document.createElement( 'option' ); if(username) { optionText = optionText.replace( /\{user\}/g, username ); }	option.appendChild(document.createTextNode(optionText)); return option; }

/** * Function which adds the select dropdown in */ $(function {	// ensure the summar widget exists	if(!document.getElementById('wpSummaryWidget')) {		return;	}

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

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

// Add support for the {user} keyword mw.loader.using( 'mediawiki.api', function {		new mw.Api.get({ action: 'query', titles: mw.config.get( 'wgPageName' ), prop: 'revisions', rvprop: 'user', formatversion: 2 }).done(function({query: {pages}}) { // find the summary element again, as page load order causes the old reference to be invalid var summaryElement = document.getElementById('wpSummaryWidget'); if(!summaryElement) { return; }

// Add summaries based on user's variable // ensure we have data var username = null; if (!pages[0].missing) { username = pages[0].revisions[0].user; }			for (var i=0; i < customSummaries.length; i++) { var summary = customSummaries[i]; if(typeof summary === 'string') { dropdown.appendChild(createOption(summary, username)); } else { // filter for namespaces if they exist if(summary.namespaces) { var namespace = mw.config.get('wgNamespaceNumber'); var type = namespace % 2 ? "talk" : "main"; // if we are not in the required namespace, skip if(!summary.namespaces.includes(namespace) && !summary.namespaces.includes(type)) { continue; }					}					// just a value? insert that if(summary.value) { dropdown.appendChild(createOption(summary.value, username)); } else { // otherwise go for an optgroup var optgroup = document.createElement( 'optgroup' ); optgroup.label = summary.label; for(var v = 0; v < summary.values.length; v++) { optgroup.appendChild(createOption(summary.values[v], username)); }						dropdown.appendChild(optgroup); }				}			}			summaryElement.appendChild(dropdown); });	}); });