User:Notacardoor/fixindents.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:Notacardoor/fixindents. |
function fixIndents(e) {
if (newHTML) {
document.querySelector(".mw-parser-output").innerHTML = newHTML;
mw.util.hidePortlet(portFixId);
mw.util.showPortlet(portUnfixId);
} else {
var pageName = mw.config.get("wgPageName");
var urlencodedPageName = mw.util.rawurlencode(pageName);
var revisionId = mw.config.get("wgRevisionId");
var getHTMLurl = "https://wiki.riteme.site/api/rest_v1/page/html/" + urlencodedPageName + "/" + revisionId;
$.ajax(getHTMLurl, {
type: "GET",
headers: {
accept: 'text/html; charset=utf-8"',
"Content-Type": "application/json",
},
})
.then(function (html, textStatus, jqXHR) {
console.log("original html");
console.log(html);
//html = tweakHTML(html);
return $.ajax(
"https://wiki.riteme.site/api/rest_v1/transform/html/to/wikitext/" +
urlencodedPageName +
"/" +
revisionId,
{
type: "POST",
headers: {
accept: "text/plain; charset=utf-8",
"Content-Type": "application/json",
"if-match": jqXHR.getResponseHeader("etag"),
},
data: JSON.stringify({
html: html,
scrub_wikitext: true,
}),
}
);
})
.then(function (wikitext, textStatus, jqXHR) {
console.log(wikitext);
return $.ajax(
"https://wiki.riteme.site/api/rest_v1/transform/wikitext/to/html/" +
urlencodedPageName +
"/" +
revisionId,
{
type: "POST",
headers: {
accept: "text/html; charset=utf-8",
"Content-Type": "application/json",
},
data: JSON.stringify({
body_only: true,
wikitext: wikitext,
}),
}
);
})
.then(function (data) {
console.log("final HTML");
console.log(data);
newHTML = data;
document.querySelector(".mw-parser-output").innerHTML = data;
mw.util.hidePortlet(portFixId);
mw.util.showPortlet(portUnfixId);
});
}
e.preventDefault();
}
function tweakHTML(html) {
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(html, "text/html");
var nodes = document.createTreeWalker(
htmlDoc.querySelector(".mw-parser-output"),
NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT,
function (n) {
if (n.nodeName === "PRE") {
return NodeFilter.FILTER_REJECT;
} else if (n.nodeName === "#text") {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_SKIP;
}
);
var node;
while ((node = nodes.nextNode())) {
if (!node.previousSibling) {
node.textContent = node.textContent.replace(/^\s+/, " ");
}
}
return htmlDoc.documentElement.outerHTML;
}
function shouldFix() {
if (mw.config.get("wgAction") !== "view") {
return false;
}
return mw.config.get("wgNamespaceNumber") % 2 === 1;
}
var portFixId = "ca-fix-indents";
var portUnfixId = "ca-unfix-indents";
var originalHTML; // cache the original html
var newHTML; // cache the fixed html
$(document).ready(function () {
if (!shouldFix()) {
return;
}
console.log(mw.config.get("wgPageName"), mw.config.get("wgRevisionId"));
originalHTML = document.querySelector(".mw-parser-output").innerHTML;
var portFix = mw.util.addPortletLink(
"p-cactions",
"#",
"Fix indents",
portFixId,
"Fix list indentation for screen readers"
);
var portUnfix = mw.util.addPortletLink(
"p-cactions",
"#",
"Unfix indents",
portUnfixId,
"Reset to original indentation"
);
mw.util.hidePortlet(portUnfixId);
$(portFix).on("click", fixIndents);
$(portUnfix).on("click", function (e) {
document.querySelector(".mw-parser-output").innerHTML = originalHTML;
mw.util.hidePortlet(portUnfixId);
mw.util.showPortlet(portFixId);
e.preventDefault();
});
});