User:Awesome Aasim/subpageify.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Awesome Aasim/subpageify. |
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.");
});
}
});
});
}