Jump to content

User:HarJIT/Scripts/setu.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.
// <nowiki>
// XXX document this here
// Adds a button below the source editor.
//
// I am not selling this and make no guarantees of safety, fitness or that it won't mangle the content.
// You are advised to double check that the scripted process has produced the desired results, and
// clean up where necessary.
//
// Usage: mw.loader.load("//en.wikipedia.org/w/index.php?action=raw&ctype=text/javascript&title=User:HarJIT/Scripts/setu.js");
//
// Canonical: [[m:w:User:HarJIT/Scripts/setu.js]]

(() => {

String.prototype.pysplit = function () {
    if (arguments.length == 1) {
        return this.split(arguments[0]);
    }
    var temp = this.split(arguments[0], arguments[1]);
    var heading = temp.join(arguments[0]).length + arguments[0].length;
    temp[temp.length] = this.substring(heading);
    return temp;
};

String.prototype.startswith = function (s) {
    return this.substring(0, s.length) == s;
};

String.prototype.endswith = function (s) {
    return this.substring(this.length - s.length) == s;
};

String.prototype.contains = function (s) {
    return this.indexOf(s) >= 0;
};

var fix = function (inp) {
    var doz = inp.split("\n");
    var output = doz[0];
    var stick = 0;
    var position = -1;
    doz.slice(1).forEach((ii) => {
        var i = ii;
        if (i.contains("{{chset-left1")) {
            stick = parseInt(i.substring(1).trim().pysplit("|", 1)[1].trim(), 16);
            position = -1
        } else if (i.startswith("|")) {
            position += 1;
        }
        if (i.contains("{{chset-cell1")) { // not elif
            var byt = (stick << 4) | position;
            var splitted = i.pysplit("|", 3);
            if (splitted.length == 4) {
                var ubit = "";
                var codes = splitted[2].match(/U\+[0123456789ABCDEF]{4,6}/gi);
                if (codes !== null && codes.length === 1) {
                    var code = codes[0].substring(2);
                    var icode = parseInt(code, 16);
                    if (icode != byt && icode < 0xF0000 && (icode > 0xF8FF || icode < 0xE000)) {
                        ubit = "|u=" + code;
                    }
                }
                i = splitted[0] + "|" + splitted[1] + ubit + "|" + splitted[2] + "|" + splitted[3];
            }
        }
        output += "\n" + i;
    });
    return output;
};

jQuery(() => {
    if (jQuery("#editpage-copywarn").length) {
        var butn = document.createElement("input");
        butn.setAttribute("type", "button");
        butn.setAttribute("value", "Add |u=");
        var nxt = jQuery("#editpage-copywarn")[0];
        nxt.parentNode.insertBefore(butn, nxt);
        butn.onclick = () => {
            //var rprt = SUMMARY;
            var txt = jQuery("#wpTextbox1")[0];
            var vl = txt.value;
            var fx = fix(vl);
            if (fx != vl) {
                txt.value = fx;
                //jQuery("#wpSummary")[0].value = rprt;
            }
            
        }
    }
});

})();

// End: [[m:w:User:HarJIT/Scripts/setu.js]]
// </nowiki>