User:Guywan/Scripts/ListAlpha.js

$(function() { if(mw.config.get("wgAction") != "view") { return; } var version = "02.06.19"; var text; // Store line

$(function()
{
	if(mw.config.get("wgAction") != "view")
	{
		return;
	}
	
	var version = "02.06.19";
	
	var text;
	
	// Store line and section numbers.
	var lineList = [[]];
	
	$("#p-cactions ul").append("<li id='ca-listalpha'><a href='#' title='Alphabetize list entries'>List Alphabetizer</a></li>");
	$("#ca-listalpha").on("click", function()
	{
		mw.notify("Starting alphabetization.", "info");
		
		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 listFound = false, curSection = 0;
		for(var i = 0; i < text.length; i++)
		{
			if(text[i].match(/^==+.+==+$/g))
			{
				// We've reached a new section.
				curSection++;
				
				lineList.push([i]);
			}
			else if(text[i].match(/^\*.*/g))
			{
				lineList[curSection].push(i);
				
				if(!listFound)
				{
					listFound = true;
				}
			}
		}
		
		// If no list was found, abort.
		if(!listFound)
		{
			mw.notify("No lists to alphabetize!", "warn");
			return;
		}
		
		mw.notify("Completed. Showing preview.");
		
		var pext = "";
		for(var y = 0; y < lineList.length; y++)
		{
			if(lineList[y].length > 1)
			{
				pext += text[lineList[0][0]] + " <a id='" + y + ":" + x +"'>Remove all items in section</a>\n\n";
				
				for(var x = 1; x < lineList[y].length; x++)
				{
					pext += text[lineList[y][x]] + "<a id='" + y + ":" + x +"'>Remove item</a>\n";
				}
			}
		}
		
		// Setup the preview.
		api = new mw.Api();
		
		/*
		 * Very WET. Suggestions are welcome.
		 */
		api.parse(pext)
		.fail(function(result) { mw.notify("Failed to parse preview: " + result, "error"); } )
		.done(function(data)
		{
			// ==================
			// PRE-PREVIEW (^_^.)
			// ==================
			
			$("#mw-content-text").html(data).prepend("<div style='border-bottom:1px solid #a2a9b1;text-align:center'>"
			+ "<h3>The following is an overview of the items that will be alphabetized</h3><br/>"
			+ "<h3>Remove items that you do not wish to be alphabetized</h3><br/>"
			+ "<button id='listalpha-submit' class='mw-ui-button mw-ui-progressive'>Proceed</button>"
			+ "<button id='listalpha-cancel' class='mw-ui-button mw-ui-quiet mw-ui-destructive'>Cancel</button></div>");
			
			$("#listalpha-submit").on("click", function()
			{
				// Put it back together!
				text = text.join("\n");
				
				api.parse(text)
				.fail(function(result) { mw.notify("Failed to parse preview: " + result, "error"); } )
				.done(function(data)
				{
					// ===============
					// PREVIEW \(^o^)/
					// ===============
					
					$("#mw-content-text").html(data).prepend("<div style='border-bottom:1px solid #a2a9b1;text-align:center'>"
					+ "<h3>This is a <strong>preview</strong> of the changes you will make.</h3><br/>"
					+ "<button id='listalpha-submit' class='mw-ui-button mw-ui-progressive'>Submit</button>"
					+ "<button id='listalpha-cancel' class='mw-ui-button mw-ui-quiet mw-ui-destructive'>Cancel</button></div>");
					
					$("#listalpha-submit").on("click", function()
					{
						// Post edited text.
						api.post(
						{
							"action" : "edit",
							"title" : mw.config.get("wgPageName"),
							"text" : text,
							"summary" : "Alphabetizing lists with [[User:Guywan/Scripts/ListAlpha|ListAlpha]] V" + version + ".",
							"token" : mw.user.tokens.get("csrtToken")
						})
						.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"));
						});
					});
					
					$("#listalpha-cancel").on("click", function()
					{
						window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));
					});
				});
			});
			
			$("#listalpha-cancel").on("click", function()
			{
				window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));
			});
		});
	});
	
	function getWikitext(page, params)
	{
		// Get page wikitext.
		return $.ajax(
		{
			url : mw.util.getUrl(page) + params,
			data : "text",
			async : false
		}).responseText;
	}
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.