User:Awesome Aasim/subpageify.js

if (!subpageify) { var subpageify = {}; $(document).ready(function {		subpageify.sections = {};		$(".mw-headline").each(async function(i) { debugger; var section = (new URL($(this).parent.find(".mw-editsection").find("a").eq(0).prop("href"))).searchParams.get("section"); if (section[0] != "T") { await $.get(mw.config.get("wgScriptPath") + "/api.php", {					action: "parse",					format: "json",					prop: "wikitext",					section: section[0],					page: mw.config.get("wgPageName")				}).done(function(result) {					debugger;					if (result.error) {						console.error(result.error.info);						alert("Subpageify failed to load correctly. You may not be able to archive all the sections.");					} else {						subpageify.sections[section] = [$('Subpageify'), result.parse.wikitext["*"]];						$(this).parent.find(".mw-editsection").eq(0).append(' [ ').append(subpageify.sections[section][0]).append(' ] ');						subpageify.sections[section][0].click(function(e) {							e.preventDefault;							var section = $(this).prop("data-section");							var sectionNo = $(this).prop("data-section-number"); if (confirm("Move section " + $(this).prop("data-section") + " to subpage?")) { $.get(mw.config.get("wgScriptPath") + "/api.php", {									action: "parse",									format: "json",									prop: "wikitext|text",									section: $(this).prop("data-section-number"),									page: mw.config.get("wgPageName")								}).done(function(result) {									if (result.error) {										alert(result.error.info);									} else {										if (result.parse.wikitext["*"] != sections[subpageify.sections[sectionNo][1]]) {											alert("Page is not up to date.");											location.reload;										} else {											var wt = result.parse.wikitext["*"], t = result.parse.text["*"];											$.get(mw.config.get("wgScriptPath") + "/api.php", { action: "parse", format: "json", prop: "wikitext|text", page: mw.config.get("wgPageName") }).done(function(result) { var edittext = result.parse.wikitext["*"]; $("#mw-content-text").html(result.parse.text["*"].replace(t, ' ')); $("#subpageify-output").append('Removing section from page... '); $.get(mw.config.get("wgScriptPath") + "/api.php", {													action: "query",													format: "json",													meta: "tokens",													type: "csrf"												}).done(function(result) {													$.post(mw.config.get("wgScriptPath") + "/api.php", { action: "edit", format: "json", wikitext: edittext.replace(wt, ""), summary: "Moving section " + section + " to subpage using subpageify", page: mw.config.get("wgPageName") }).done(function(result) { if (result.error) { $("#subpageify-output").append('Error: ' + result.error.info + ' '); } else { $("#subpageify-output").append('Adding section to subpage... '); $.get(mw.config.get("wgScriptPath") + "/api.php", {																action: "query",																format: "json",																meta: "tokens",																type: "csrf"															}).done(function(result) {																var wtpos = 0;																for (var i in wt) {																	if (wt[i] == "\n") {																		wtpos = i;																	}																}																$.post(mw.config.get("wgScriptPath") + "/api.php", { action: "edit", format: "json", page: mw.config.get("wgPageName") + "/" + section, createonly: true, wikitext: wt.slice(start = wtpos + 1) }).done(function(result) { if (result.error) { $("#subpageify-output").append('Error: ' + result.error.info + ' '); } else { $("#subpageify-output").append('Done. '); }																});															});														}													});												});											});										}									}								}).fail(function {									alert("Connection lost.");								}); }						});					}				}).fail(function {					alert("Subpageify failed to load correctly. You may not be able to archive all the sections.");				}); }		});	}); }