_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

User:Notacardoor/fixindents.js

In this article, we will explore in detail User:Notacardoor/fixindents.js, a topic of great relevance in contemporary society. User:Notacardoor/fixindents.js has captured the attention of numerous experts and academics due to its significant impact on various aspects of daily life. Through meticulous analysis and the collection of empirical evidence, we will seek to shed light on the different nuances and facets that characterize User:Notacardoor/fixindents.js, in order to provide a comprehensive and enriching vision on this topic. In addition, the practical and theoretical implications of User:Notacardoor/fixindents.js will be examined, in order to promote a deeper and more detailed understanding of its importance in the current landscape.
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://en.wikipedia.org/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://en.wikipedia.org/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://en.wikipedia.org/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();
    });
});