Jump to content

User:Awesome Aasim/subpageify.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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] = [$('<a id="subpageify-"' + $(this).prop("id") + '" data-section="' + $(this).text() + '" data-section-number="' + section + '"href="javascript:void(0)">Subpageify</a>'), result.parse.wikitext["*"]];
						$(this).parent().find(".mw-editsection").eq(0).append('<span class="mw-editsection-bracket">[</span>').append(subpageify.sections[section][0]).append('<span class="mw-editsection-bracket">]</span>');
						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, '<div id="subpageify-output"></div>'));
												$("#subpageify-output").append('<p style="color:green;">Removing section from page...</p>');
												$.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 [[User:Awesome_Aasim/subpageify.js|subpageify]]",
														page: mw.config.get("wgPageName")
													}).done(function(result) {
														if (result.error) {
															$("#subpageify-output").append('<p style="color:red;">Error: ' + result.error.info + '</p>');
														} else {
															$("#subpageify-output").append('<p style="color:green;">Adding section to subpage...</p>');
															$.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('<p style="color:red;">Error: ' + result.error.info + '</p>');
																	} else {
																		$("#subpageify-output").append('<p style="color:green;">Done.</p>');
																	}
																});
															});
														}
													});
												});
											});
										}
									}
								}).fail(function() {
									alert("Connection lost.");
								});
							}
						});
					}
				}).fail(function() {
					alert("Subpageify failed to load correctly. You may not be able to archive all the sections.");
				});
			}
		});
	});
}