Jump to content

User:Zhaofeng Li/SummaryGuard.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.
/*
	SummaryGuard
	* Disables the Save page button if no edit summary is entered
	* Displays the length of the edit summary above the box
	* Warns about an over-lengthed summary
*/

// ==UserScript==
// @name        SummaryGuard
// @description Forces you to enter an edit summary when editing English Wikipedia.
// @namespace   https://en.wikipedia.org/wiki/User:Zhaofeng_Li
// @include     *://en.wikipedia.org/*action=edit*
// @version     1
// @grant       none
// ==/UserScript==

$( document ).ready( function() {
	if ( $( '#editform' ).length ) { // only when editing
		$( "#wpSummaryLabel" ).after( "<span id='summaryGuard' style='margin-left:5px;'></span>" );
		$( '#wpSummary' ).keyup( checkSummary );
		checkSummary();
	}
} );

function checkSummary() {
		var length = $( '#wpSummary' ).val().length;
		if ( length ) { // edit summary provided
			$( '#wpSave' ).removeAttr( "disabled"); // enable save button
			$( "#summaryGuard" ).text( "(" + length + "/255)" );
			if ( length <= 100 ) { // okay
				$( "#summaryGuard" ).css( "color", "#a7d7bf" ); // green
			} else if ( length <= 150 ) { // a bit lengthy...
				$( "#summaryGuard" ).css( "color", "#f9c557" ); // orange
			} else { // that's too long...
				$( "#summaryGuard" ).css( "color", "#cc0000" ); // red
			}
		} else { // nothing entered
			$( '#wpSave' ).attr( "disabled", "disabled" );
			$( "#summaryGuard" ).text( "" );
		}
}