User:Guywan/Scripts/LEA.js

// // $(function {	if(mw.config.get("wgAction") != "view") return;	const version = "24.03.20";	// Add portlet link.	$("#p-cactions ul").append("List Entry Annotator");	$("#ca-lea").on("click", => {		mw.notify("Starting annotation procedure."); window.setTimeout( =>		{			var reg = new RegExp(/^\*+\s*\[\[([^\]|]+).*?\]\](|(\s*\(.+?\)))$/g);			var text = getWikitext(mw.config.get("wgPageName"), "?action=raw");			// Split the page into lines of text.			text = text.split("\n");			// Run a regex over each line.			var linksFound = false;			for(var i = 0; i < text.length; i++)			{				if(text[i].match(reg))				{					var targetText = getWikitext(reg.exec(text[i])[1], "?action=raw&section=0");					targetText = targetText.split("\n");					for(var k = 0; k < targetText.length; k++)					{						// Find the first paragraph. Likely to always be successful.						if(targetText[k].match(/^[A-Za-z0-9'"(]/g))						{							//										Filter out HTML comments.							text[i] += " – " + targetText[k].replace(/|(<!--.*?$)/g, "");							break;						}					}

linksFound = true; }			}			// If no list items were found, abort. if(!linksFound) {				mw.notify("No lists to annotate!"); return; }			mw.notify("Completed. Showing preview."); // Put it back together! text = text.join("\n"); // Setup the preview. api = new mw.Api; api.parse(text) .fail(function(result) { mw.notify("Failed to parse preview.", {type: "error"}); } ) .done(function(data)			{				$("#mw-content-text").html(data).prepend(" " + " This is a preview of the changes you will make. " + "Submit " + "Cancel ");				$("#lea-submit").on("click", => {					// Post edited text. api.post(					{						"action": "edit",						"title": mw.config.get("wgPageName"),						"text": text,						"summary": "Annotating lists with List Entry Annotator V" + version + ". Content copied from respective articles.",						"token": mw.user.tokens.get("csrfToken")					}) .fail( => { mw.notify("Failed to post!", {type: "error"}); }) .done( =>					{						mw.notify("Success! Refreshing.");						window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));					}); });				$("#lea-cancel").on("click", => {					window.location.href = mw.util.getUrl(mw.config.get("wgPageName")); });			});		}, 100);	});	function getWikitext(page, params) {		// Get page wikitext. return $.ajax(		{			url : mw.util.getUrl(page) + params,			data : "text",			async : false		}).responseText; } }); //