List Mass Village Renamer

Discussion in 'Scripts & independent tools' started by FunnyPocketBook, Feb 11, 2018.

Share This Page

  1. FunnyPocketBook

    FunnyPocketBook New Member

    Joined:
    Feb 11, 2018
    Messages:
    3
    Likes Received:
    0
    Code:
    // Parent element to append new elements to
    var parentThingie = document.getElementById("inner-border");
    
    // Textarea to put in the village names
    var villNames = document.createElement("textarea");
    villNames.setAttribute("id", "villNames");
    villNames.setAttribute("type", "text");
    villNames.setAttribute("placeholder", 'Village names');
    villNames.setAttribute("style", "margin-top:10px; display: inline;");
    villNames.setAttribute("rows", "3");
    parentThingie.parentNode.insertBefore(villNames, parentThingie);
    
    // Input field for the separator
    var separator = document.createElement("input");
    separator.setAttribute("id", "separator");
    separator.setAttribute("type", "text");
    separator.setAttribute("placeholder", 'Village name separator. If separated by line breaks, type enter');
    separator.setAttribute("style", "margin-top:10px;width:300px");
    parentThingie.parentNode.insertBefore(separator, parentThingie);
    
    var okButton = document.createElement("button");
    okButton.innerHTML = "OK";
    okButton.setAttribute("id", "okButton");
    okButton.setAttribute("style", "margin:10px;");
    okButton.setAttribute("class", "btn");
    
    var restoreButton = document.createElement("button");
    restoreButton.innerHTML = "Restore old names";
    restoreButton.setAttribute("id", "restoreButton");
    restoreButton.setAttribute("style", "margin:10px;");
    restoreButton.setAttribute("class", "btn");
    
    parentThingie.insertAdjacentElement('afterbegin', restoreButton);
    parentThingie.insertAdjacentElement('afterbegin', okButton);
    parentThingie.insertAdjacentElement('afterbegin', separator);
    parentThingie.insertAdjacentElement('afterbegin', villNames);
    
    // Hitting enter while the separator input field is selected will start the village renamer
    document.getElementById("separator").addEventListener("keyup", function(event) {
        "use strict";
        event.preventDefault();
        if (event.keyCode === 13) {
            document.getElementById("okButton").click();
        }
    });
    
    var input,
        separator,
        villName;
    window.oldNames = [];
    
    document.getElementById("okButton").onclick = function changeVillName() {
        "use strict";
        // Get list of village names and remove all line breaks and replace them by ¬¬¬¬. The probability that someone uses ¬¬¬¬ in the village name is very unlikely, so there shouldn't be any issues with that.
        input = $("#villNames").val().replace(/(\r\n|\n|\r)/gm, "¬¬¬¬");
        // Get the symbol or word or whatever it is that separates the village names
        separator = $("#separator").val();
        // Two cases, either the separator is line breaks or it's not line breaks
        // If the separator is not line breaks, the separator will be replaced by ¬¬¬¬ 
        if ($("#separator").val() !== "enter") {
            villName = input.split(separator).join("¬¬¬¬");
            villName = villName.split("¬¬¬¬");
        } else {
            villName = input.split("¬¬¬¬");
        }
        // In case of multiple line breaks there will be some elements in the villName array that are empty strings and we don't want them
        for (var i = 0; i < villName.length; i++) {
            if (villName[i] === "") {
                villName.splice(i, 1);
                i = i - 1;
            }
        }
        var tableLength = 0;
        var table;
    
        // Depending on which overview tab is used, change table
        if (document.querySelector("#combined_table") !== null) {
            tableLength = document.querySelector("#combined_table").rows.length;
            table = "#combined_table ";
        }
        if (document.querySelector("#production_table") !== null) {
            tableLength = document.querySelector("#production_table").rows.length;
            table = "#production_table ";
        }
        if (document.querySelector("#units_table") !== null) {
            tableLength = document.querySelector("#units_table").rows.length;
            table = "#units_table ";
        }
        if (document.querySelector("#buildings_table") !== null) {
            tableLength = document.querySelector("#buildings_table").rows.length;
            table = "#buildings_table ";
        }
        if (document.querySelector("#techs_table") !== null) {
            tableLength = document.querySelector("#techs_table").rows.length;
            table = "#techs_table ";
        }
        var j = 2;
        // Every 100ms, rename a village
        var intervalVill = setInterval(function() {
            // Clicks the button to be able to change the village name
            document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span > a.rename-icon').click();
            // Store the current village name in order to be able to reverse the renaming process
            var text = document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span.quickedit-edit > input[type="text"]:nth-child(1)').value;
            window.oldNames[j - 2] = text;
            // Put in the new village name
            document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span.quickedit-edit > input[type="text"]:nth-child(1)').value = villName[j - 2];
            // Confirm the new village name
            document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span.quickedit-edit > input.btn').click();
            text = "";
            j++;
            // Stop the interval if the end of the table has been reached or there aren't any more new village names
            if (j === tableLength + 1 || j > villName.length + 1) {
                clearInterval(intervalVill);
            }
        }, 100);
    };
    
    // Basically the same as the renaming process of function changeVillName but it will just restore the old village names
    document.getElementById("restoreButton").onclick = function restoreOld() {
        "use strict";
        var table;
        var tableLength = 0;
        if (document.querySelector("#combined_table") !== null) {
            tableLength = document.querySelector("#combined_table").rows.length;
            table = "#combined_table ";
        }
        if (document.querySelector("#production_table") !== null) {
            tableLength = document.querySelector("#production_table").rows.length;
            table = "#production_table ";
        }
        if (document.querySelector("#units_table") !== null) {
            tableLength = document.querySelector("#units_table").rows.length;
            table = "#units_table ";
        }
        if (document.querySelector("#buildings_table") !== null) {
            tableLength = document.querySelector("#buildings_table").rows.length;
            table = "#buildings_table ";
        }
        if (document.querySelector("#techs_table") !== null) {
            tableLength = document.querySelector("#techs_table").rows.length;
            table = "#techs_table ";
        }
        var j = 2;
        var intervalVill = setInterval(function() {
            document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span > a.rename-icon').click();
            var text = document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span.quickedit-edit > input[type="text"]:nth-child(1)').value;
            window.oldNames[j - 2] = text;
            document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span.quickedit-edit > input[type="text"]:nth-child(1)').value = villName[j - 2];
            document.querySelector(table + ' > tbody > tr:nth-child(' + j + ') > td:nth-child(2) > span > span.quickedit-edit > input.btn').click();
            j++;
            if (j === tableLength + 1 || j > villName.length + 1) {
                clearInterval(intervalVill);
            }
        }, 100);
    };
    
    Filename: List Mass Village Renamer
    Description: The difference to the other renamer is that with this script it is possible to rename the villages if you already have a list of village names ready, for example a list of country names. The villages are renamed from top to bottom.
    Obviously, every village name has to be separated from another village name by a unique separator. For example, my village name input is "Vill01 Vill02 Vill03", the separator is a simple white space, so I simply hit the space bar in the separator field. This gives my first village the name "Vill01", the second "Vill02" etc.
    If my village name input is "Vill 01 # Vill 02 # Vill 03", my separator is " # " (spacebar # spacebar). This gives my first village the name "Vill 01", the second "Vill 02" etc.
    It is also possible to have line breaks as separator, such as:
    "Vill 01
    Vill 02
    Vill 03"
    In that case I put the word "enter" without double quotes. This gives my first village the name "Vill 01", the second "Vill 02" etc.

    The button "Restore old names" restores the old village names but only if the page hasn't been refreshed. This is because the order of the villages might change when new village names are assigned to each village, so it doesn't make much sense to rename the villages in the same order as the villages were listed before the order has changed.



    [​IMG]