Tag Incomings

Checks for new incomings

目前為 2018-04-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Tag Incomings
// @version      1.3.2
// @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 j = 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(" + j + ") > 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
        var attVill = attackingVillage.indexOf($("#incomings_table > tbody > tr:nth-child(" + j + ") > td:nth-child(3) > a").text());
        if(isNaN(parseInt(incName.slice(-1))) && !incName.includes("attack") || incName.slice(-1) != villNr[attVill]) { // 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(" + j + ") > td:nth-child(3) > a").text()); // See which index the attack has
            document.querySelector("#incomings_table > tbody > tr:nth-child(" + j + ") > td:nth-child(1) > span > span > a.rename-icon").click();
            var incLabel = document.querySelector('#incomings_table > tbody > tr:nth-child(' + j + ') > td:nth-child(1) > span > span.quickedit-edit > input[type="text"]:nth-child(1)');
            if(isNaN(parseInt(incName.slice(-1))) && incName.slice(-1) != villNr[attVill]) {
                incLabel.value = incLaabel.value.slice(0, -1);
            }
            document.querySelector('#incomings_table > tbody > tr:nth-child(' + j + ') > 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(" + j + ") > td:nth-child(1) > span > span.quickedit-edit > input.btn").click();
        }
        j++;
        if(j >= incRow) { // Stop interval, just like the for-loop condition
            clearInterval(tagInterval);
        }
    }, 30);

    // Wait 300ms, click the label button
	setTimeout(function() {
        if($("#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);
}