User:Guywan/Scripts/Headings.js

// // $(function {	if(mw.config.get("wgAction") != "view") return;	const VERSION = "03.06.19";	var level = "==";	// Setup links.	$("#p-cactions ul").append("PH2H");	$("#ca-hm").on("click", function {		mw.notify("Starting conversion process.", "info"); $.ajax(		{			url : mw.util.getUrl(mw.config.get("wgPageName")) + "?action=raw", dataType : "text"		}) .fail(function(result) { mw.notify("Failed to load: " + result, "error"); }) .done(function(text)		{			// Split the page into lines of text.			text = text.split("\n");			// Run a regex (or few) over each line.			var modified = false;			for(var i = 0; i < text.length; i++)			{				if(text[i].match(/=====[^=]*=====/g))				{					level = "======";				}				else if(text[i].match(/====[^=]*====/g))				{					level = "=====";				}				else if(text[i].match(/===[^=]*===/g))				{					level = "====";				}				else if(text[i].match(/==[^=]*==/g))				{					level = "===";				}				else				{					// Don't touch definition lists ...					if(text[i].match(/^;.*/g) && !text[i + 1].match(/^:.*/g))					{						text[i] = text[i].replace(/^;.*/g, hm_replace);						modified = true;					}				}			}			// If nothing is modified, abort.			if(!modified)			{				mw.notify("There are no pseudo-headings to convert!", "warn");				return;			}			mw.notify("Completed. Showing preview.");			// Put it back together! text = text.join("\n"); // Setup the preview. new mw.Api.parse(text) .fail(function(result) { mw.notify("Failed to parse preview: " + result, "error"); }) .done(function(data)			{				$("#mw-content-text").html(data).prepend(" " + " This is a preview of the changes you will make. " + "Submit " + "Cancel ");				$("#hd-submit").on("click", function {					// Post edited text. api.post(					{						"action" : "edit",						"title" : mw.config.get("wgPageName"),						"text" : text,						"summary" : "Replacing pseudo-headings with headings using Headmaster V" + VERSION + ".",						"minor" : true,						"token" : mw.user.tokens.get("editToken")					}) .fail(function(result) { mw.notify("Failed to post: " + result, "error"); }) .done(function					{						mw.notify("Success! Refreshing.", "info");						window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));					}); });				$("#hd-cancel").on("click", function {					window.location.href = mw.util.getUrl(mw.config.get("wgPageName")); });			});		});	});	function hm_replace(match) {		// If there is a trailing ':', destroy it! if(match[match.length - 1] == ":") {			return level + " " + match.substring(1, match.length - 1) + "  " + level; }		return level + " " + match.substr(1) + " " + level; } }); //