Snippets

This page describes some user scripts that can be useful to @require in userscripts. Warning, these individual snippets are not approved. This because by themselves they do not actually do anything. Any script created using these scripts should be approved before usage/distribution.

twAjax

twAjax provides a simple and approved way of retrieving data from the game. This script is published at https://forum.tribalwars.nl/index.php?threads/ajax-requests-openen-tabs.194608/ and not created by me, I simply poured it into a userscript for simple inclusion.

// @require 	https://daniel.dmvandenberg.nl/site/wp-content/uploads/userscripts/TWAjax.user.js
//e.g.
let sResponse = await $.twAjax(`/game.php?village=${iVillageId}&screen=train&mode=train`);

Retrieve troops

Retrieves the troops for the given village (either total or currently available).

// @require      https://daniel.dmvandenberg.nl/site/wp-content/uploads/userscripts/TWAjax.user.js
// @require 	https://daniel.dmvandenberg.nl/site/wp-content/uploads/userscripts/Retrieve%20units.user.js
//e.g.
let oTroops = window.doRetrieveUnits();

UserScript

// ==UserScript==
// @name         Retrieve units
// @namespace    https://daniel.dmvandenberg.nl/scripting-tribal-wars/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://*.tribalwars.*/*
// @grant        none
// @require 	 https://daniel.dmvandenberg.nl/site/wp-content/uploads/userscripts/TWAjax.user.js
// ==/UserScript==

(function() {
    'use strict';

    let $ = window.$;
    window.doRetrieveUnits = async (bTotal = true, iVillageId = window.game_data.village.id) =>{
        let e = await $.twAjax(`/game.php?village=${iVillageId}&screen=train&mode=train`);
        let eResponse = document.createElement("html");
        eResponse.innerHTML = e;
        let eForm = eResponse.querySelector("#train_form");
        let aUnitRows = Array.from(eForm.querySelectorAll(".unit_link"));
        let oTroops = aUnitRows.map((e) => {
            return {
                "sUnit": e.getAttribute('data-unit'),
                "iCount": e.closest("tr").querySelector("td:nth-child(3)").innerText.split("/")[bTotal ? 1 : 0] * 1
            }
        }).reduce((oPrev, oCurr) => {
            oPrev[oCurr.sUnit] = oCurr.iCount;
            return oPrev;
        }, {});
        return oTroops;
    };
})();

Get village object

Retrieves the village data, usually found in window.game_data, for any given village id.

// @require      https://daniel.dmvandenberg.nl/site/wp-content/uploads/userscripts/TWAjax.user.js
// @require 	https://daniel.dmvandenberg.nl/site/wp-content/uploads/userscripts/Get%20village%20data.user.js
//e.g.
let oVillageData = window.getVillageData(1234);

Userscript

// ==UserScript==
// @name         Get village data
// @namespace    https://daniel.dmvandenberg.nl/scripting-tribal-wars/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @include      https://*.tribalwars.*/game.php?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window.GetVillageData = async (iVillageId, bAllowCache = true)=>{
        let oVillageData = undefined;
        if (window.game_data.village.id == iVillageId){
            oVillageData = window.game_data;
        }
        else{
            if (!window.localStorage[`65a01a28-1743-4cf6-bb3b-e60ed745d3a0_villagedata_${iVillageId}`] || !bAllowCache){
                let eRoot = document.createElement("html");
                eRoot.innerHTML = await window.$.twAjax(`/game.php?village=${iVillageId}`);
                let sVillageData = Array.from(eRoot.querySelectorAll("script")).find((e)=>{return e.innerHTML.indexOf("wood_float") != -1;}).innerText.split("\n").find((s)=>{return s.indexOf("wood_float") != -1;});
                sVillageData = sVillageData.replace("TribalWars.updateGameData(","").replace(");","").trim();
                oVillageData = JSON.parse(sVillageData);
            }
            else{
                oVillageData = JSON.parse(window.localStorage[`65a01a28-1743-4cf6-bb3b-e60ed745d3a0_villagedata_${iVillageId}`]);
            }
        }
        window.localStorage[`65a01a28-1743-4cf6-bb3b-e60ed745d3a0_villagedata_${oVillageData.village.id}`] = JSON.stringify(oVillageData);
        return oVillageData;
    };
})();