User:Iluvatar/gs.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function () {
	$( document ).ready( function() {
		if (mw.config.get( 'wgAction' ) !== "delete" )
		    return;
		mw.loader.using( [ 'mediawiki.util' ], function() {
			$( '#wpReason' ).replaceWith($( '<textarea>' ).attr( { id: 'wpReason', name: 'wpReason', rows: '3', maxlength: '500'}));
			$('<div>').attr('id','wpReasonPreview').insertAfter( '#wpReason' );
			var newel = document.createElement("div"); var newel2 = document.createElement("div");
			var output = document.getElementById("wpReasonPreview");
			newel.setAttribute("id", "pressetDelete"); newel.style.display = "inline-block";
			newel.style.marginRight = "4px"; newel2.setAttribute("id", "reasonDelete");
			newel2.style.display = "inline-block"; output.appendChild(newel); output.appendChild(newel2);
			output.style.border = "1px solid #a2a9b1"; output.style.borderRadius = "2px";
			output.style.padding = ".625em"; output.style.marginTop = ".5em";
			output.style.fontSize = "90%"; output.style.backgroundColor = "#eaecf0";
			output.style.lineHeight = "1.3em"; output.style.wordBreak = "break-all";
			output.style.minHeight = "18px";
			
			document.getElementById("wpReason").addEventListener("input", function() {
				    $("#reasonDelete").html(replaceReason(this.value));
			});
			document.getElementById("wpDeleteReasonList").onchange = function() {
				alert(self.value);
				if (document.getElementById("wpDeleteReasonList").value !== "other") {
					document.getElementById("pressetDelete").display = "inline-block";
				    $("#pressetDelete").html(replaceReason(this.value) + ":");
				}
				else
				    document.getElementById("pressetDelete").display = "none";
			};
				    
			function replaceReason(text) {
				text = text.replace(/\[{1,2}([^\]\s\|]+)([\|\s]?)(.*?)\]{1,2}(\w*)/g, function($0, $1, $2, $3, $4) {
					if ($0.includes("<") || $0.includes(">"))
					    return $0.replace(/</g, "").replace(/>/g, "");
					if ($0.search(/^\[\[/gm) == -1 || ($1.match(/\[/g)||[]).length !== ($1.match(/\]/g)||[]).length)
					    return $0;
					if ($3 === "")
					    return "<a style='text-decoration: none' href='https://ru.wikipedia.org/wiki/" + $1 + "'>" + $1 + $4 + "</a>";
					else {
						if ($2 === " ")
						    return "<a style='text-decoration: none' href='https://ru.wikipedia.org/wiki/" + $1 + "_" + $3 + "'>" + $1 + " " + $3 + $4 + "</a>";
						else
						    return "<a style='text-decoration: none' href='https://ru.wikipedia.org/wiki/" + $1 + "'>" + $3 + $4 + "</a>";
					}
				});
				return text;
			}
		});
	});
}() );