Tag Incomings

Tags new incomings

当前为 2019-09-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Tag Incomings
// @version      2.1.5
// @description  Tags new incomings
// @author       FunnyPocketBook
// @match        https://*/game.php?*mode=incomings*
// @grant        none
// @namespace https://greasyfork.org/users/151096
// ==/UserScript==
let attack = "attack"; // Change this to your language
let support = "support"; // Change this to your language
let noble = "noble" // Change this to your language
const wait = 10000; // How often the script checks for new attacks in milliseconds

const domain = document.domain;
if(domain.includes("tribalwars.com.pt") || domain.includes("tribalwars.com.br")) {
    attack = "ataque";
    support = "apoio";
    noble = "nobre";
}

const incRow = document.querySelector("#incomings_table").rows.length;
const timeout = Math.max(60000, incRow * 31);
setTimeout(function() {
    "use strict";
    tagger();
}, wait);

setTimeout(function() {
    window.location.reload();
}, timeout);

function tagger() {
    "use strict";
    let attackingVillage = []; // Store all attacking villages
    let villNr = []; // Store number of attacks per attacking village, same index as attackingVillage
    let index;
    for (let 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]++;
        }
        let incName = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a:nth-child(1) > span.quickedit-label").innerHTML;
        let 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
    let j = 2;
    let tagInterval = setInterval(function() { // Interval instead of for-loop to control the server requests
        let 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
        const attVill = attackingVillage.indexOf($("#incomings_table > tbody > tr:nth-child(" + j + ") > td:nth-child(3) > a").text());
        if(!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();
            const 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(Number.isInteger(parseInt(incName.slice(-1)))) {
                incLabel.value = incLabel.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);
            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();
            }
        }
    }, 30);
}