Tag Incomings

Checks for new incomings

Versión del día 03/04/2018. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Tag Incomings
// @version      1.1
// @description  Checks for new incomings
// @author       FunnyPocketBook
// @match        https://*/game.php?village=*&screen=overview_villages&mode=incomings*
// @grant        none
// @namespace https://greasyfork.org/users/151096
// ==/UserScript==


setInterval(function() {
    "use strict";
    tagger();
}, 10000);

function tagger() {
	"use strict";
    var incRow = document.querySelector("#incomings_table").rows.length;
    var attackingVillage = []; // Store all attacking villages
    var villNr = []; // Store number of attacks per attacking village, same index as attackingVillage
    var index;
	for (var i = 2; i < incRow; i++) {
        index = attackingVillage.indexOf($("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(3) > a").text()); // See if the attacking village is already in the array
        if(index === -1) { // If attacking village is not in array, push it into array
            attackingVillage.push($("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(3) > a").text());
            villNr.push(1);
        } else { // If attacking village is in array, increase attack number
            villNr[index]++;
        }
		var incName = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a:nth-child(1) > span.quickedit-label").innerHTML;
        var nobleIcon = "";
        // If icon exist, save it in nobleIcon
        if (document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a:nth-child(1) > span:nth-child(2) > img")) {
            nobleIcon = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a:nth-child(1) > span:nth-child(2) > img").getAttribute("src");
        }
        incName = incName.replace(/(\r\n|\n|\r)/gm,"").replace(/ /g,"").toLowerCase(); // Remove all whitespace from attack name and make it lowercase
        // If the attack has the noble icon and is not already tagged as noble, tag it as noble
        if (nobleIcon.includes("snob.png") && !incName.includes("noble")) {
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a.rename-icon").click();
            document.querySelector('#incomings_table > tbody > tr:nth-child(' + i + ') > td:nth-child(1) > span > span.quickedit-edit > input[type="text"]:nth-child(1)').value = "Noble";
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span.quickedit-edit > input.btn").click();
        } 
        // If the attack is labelled with attack or support, click the checkbox so the system automatically tags the inc
        else if (incName.includes("attack") || incName.includes("support")) {
			document.querySelector('#incomings_table > tbody > tr:nth-child(' + i + ') > td:nth-child(1) > input[type="checkbox"]:nth-child(2)').click();
		}
    }
    
    // Tag the attacks with the number of attacks per attacking village
    var i = 2;
    var tagInterval = setInterval(function() { // Interval instead of for-loop to control the server requests
        var incName = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a:nth-child(1) > span.quickedit-label").innerHTML; // Get inc name
        incName = incName.replace(/(\r\n|\n|\r)/gm,"").replace(/ /g,"").toLowerCase(); // Remove all whitespace from attack name and make it lowercase
        if(isNaN(parseInt(incName.slice(-1))) && !incName.includes("attack")) { // If the last character in the inc name is not a number and the incName does not include "attack", append the number
            index = attackingVillage.indexOf($("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(3) > a").text()); // See which index the attack has
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a.rename-icon").click();
            document.querySelector('#incomings_table > tbody > tr:nth-child(' + i + ') > td:nth-child(1) > span > span.quickedit-edit > input[type="text"]:nth-child(1)').value += " " + villNr[index];
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span.quickedit-edit > input.btn").click();
        }
        i++;
        if(i >= incRow) { // Stop interval, just like the for-loop condition
            clearInterval(tagInterval);
        }
    }, 30);

    // Wait 300ms, click the label button
	setTimeout(function() {
        if(document.querySelector("#incomings_table > tbody > tr:nth-child(" + incRow + ") > th:nth-child(2) > input:nth-child(2)").attr("value").toLowerCase === "label") {
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + incRow + ") > th:nth-child(2) > input:nth-child(2)").click();
        } else {
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + incRow + ") > th:nth-child(2) > input:nth-child(3)").click();
        }
    }, 2000);
}