User:TheJJJunk/FastSummary.js

From Wikipedia, the free encyclopedia
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.
// <nowiki>
// Everything is encapsulated in a private namespace object---``FastSummary'':
var FastSummary = FastSummary || {};

$(document).ready(function() 
{
	//initialize Constants
	FastSummary.Constants = getFastSummaryConstants();

	//only execute on the edit page
	if (!FastSummary.Constants.IS_EDIT_PAGE || FastSummary.Constants.IS_JS_PAGE || FastSummary.Constants.ARTICLE_TEXT_BOX_ELEMENT == null)
		return;
		
	let btnStyle      = "box-shadow:inset 0px 1px 0px 0px #ffffff;background:linear-gradient(to bottom, #f9f9f9 5%, #e9e9e9 100%);background-color:#f9f9f9;border-radius:6px;border:1px solid #ccc;display:inline-block;cursor:pointer;color:#666666;font-family:Arial;font-size:12px;font-weight:bold;padding:3px 6px;text-decoration:none;text-shadow:0px 1px 0px #ffffff;margin-bottom:5px;";
	let linkStartText = "javascript:FastSummary.addToSummary('";
	let linkEndText   = " ([[User:TheJJJunk/FastSummary|FS]])');";
	
	//init UI
	let link1 = $("<a>", {
		"href":linkStartText + "Added missing [[Template:Notelist|notelist]]" + linkEndText, 
		"style":btnStyle
	});
	link1.text("Added missing notelist");
	
	//init UI
	let link2 = $("<a>", {
		"href":linkStartText + "Fixed [[Category:Pages with URL errors|URL error]]" + linkEndText, 
		"style":btnStyle
	});
	link2.text("Fixed URL error");
	$('#wpSummaryLabel').prepend(link2).prepend("&nbsp;").prepend(link1);
});

FastSummary.addToSummary = function(summary) {
	var wpSummary = document.getElementById('wpSummary');
	if (wpSummary.value !== '') {
		summary = wpSummary.value + '; ' + summary;
	}
	if ((wpSummary.maxLength > 0) && (summary.length > wpSummary.maxLength)) {
		alert('Error: If the proposed text is added to the summary, its length will exceed the character limit.');
		return;
	}
	wpSummary.value = summary;
}

function getFastSummaryConstants()
{
	var ARA_Constants = ARA_Constants || {};

	//article text box element
	ARA_Constants.ARTICLE_TEXT_BOX_ELEMENT = $("#wpTextbox1");
	
	//are we on an Edit page?
	ARA_Constants.IS_EDIT_PAGE = mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit';
	
	//are we on a JS page?
	ARA_Constants.IS_JS_PAGE = mw.config.get('wgRelevantPageName').endsWith('.js');
	
	//the ARA Suggestion box, which appears above the editing section
	ARA_Constants.SUGGESTION_BOX_DIV = $('<div>', {'id':'suggestionBox.ARA', 'style':'border:dashed #ccc 1px;color:#888;'});
	
	//the Add to Summary box, which appears near the edit summary
	ARA_Constants.ADD_TO_SUMMARY_DIV = $('<div>', {'id':'addToSummaryBox.ARA', 'style':'border:dashed #ccc 1px;color:#888;display:none;'});
	
	ARA_Constants.DEFAULT_MAX_SUGGESTIONS = 8;
	ARA_Constants.maxSuggestions = ARA_Constants.DEFAULT_MAX_SUGGESTIONS;
	ARA_Constants.suggestions; // : Suggestion[]
	ARA_Constants.appliedSuggestions = {}; // : Map<String, int>
	
	ARA_Constants.scannedText = null; // remember what we scan, to check if it is
	                       // still the same when we try to fix it
	
	ARA_Constants.BIG_THRESHOLD = 100 * 1024;
	ARA_Constants.isBigScanConfirmed = false; // is the warning about a big article confirmed
	ARA_Constants.isTalkPageScanConfirmed = false;
	
	ARA_Constants.scanTimeoutId = null; // a timeout is set after a keystroke and before
	                         // a scan, this variable tracks its id
	                         
	return ARA_Constants;
}
// </nowiki>