Hello There, Guest! Register

Commands Withdrawl/Send-Back Troops
#1
This is a super helpful Quality of life script
I know we all have dealt with manually pulling troops back or sending troops back out of villages. It is tedious once you get 10,50,100 or more villages so much so that many just get left to rot.

run this script on the Combined overview page. 
you could have a certain group selected if you would like but make sure the =combined is at the end of the url if it is not reclick the Combined link once you have the correct group selected.


Code:
javascript:(function() {
    const url = window.location.href;
    const serverMatch = url.match(/\/\/(w\d+)\.infernal-wars\.com/);
    const villageMatch = url.match(/village=(\d+)/);
    if (!serverMatch || !villageMatch || !url.endsWith("=combined")) {
        alert("Please make sure you are on the combined page.");
        return;
    }

    const server = serverMatch[1];
    const currentVillageID = villageMatch[1];

    let villageIDs = new Set();
    let currentPage = 1;
    const popup = createPopup();
    
    async function fetchVillageIDs(page = 1) {
        const pageUrl = `https://${server}.infernal-wars.com/game.php?village=${currentVillageID}&screen=overview_villages&mode=combined&page=${page}`;
        const response = await fetch(pageUrl);
        const text = await response.text();
        const parser = new DOMParser();
        const doc = parser.parseFromString(text, "text/html");

        const villageLinks = Array.from(doc.querySelectorAll("#combined_table a"));
        villageLinks.forEach(link => {
            const idMatch = link.href.match(/village=(\d+)/);
            if (idMatch) villageIDs.add(idMatch[1]);
        });

        const nextPageLink = doc.querySelector(`a[href*="page=${page + 1}"]`);
        if (nextPageLink) {
            updatePopup(`Fetching villages from page ${page + 1}...`);
            await fetchVillageIDs(page + 1);
        } else {
            updatePopup(`Total Unique Villages Found: ${villageIDs.size}`);
            processVillages();
        }
    }

    function processVillages() {
        const villageArray = Array.from(villageIDs);
        let index = 0;

        function processNextVillage() {
            if (index >= villageArray.length) {
                updatePopup("All villages processed.");
                return;
            }

            const villageID = villageArray[index++];
            updatePopup(`Processing village ID: ${villageID} (${index}/${villageArray.length})`);
            const villageUrl = `https://${server}.infernal-wars.com/game.php?village=${villageID}&screen=place&mode=units`;
            
            const iframe = document.createElement("iframe");
            iframe.style.display = "none";
            iframe.src = villageUrl;
            document.body.appendChild(iframe);

            iframe.onload = () => {
                const checkboxes = iframe.contentDocument.querySelectorAll("input[type='checkbox'][name^='id_']");
                if (checkboxes.length > 0) {
                    checkboxes.forEach(checkbox => checkbox.checked = true);
                    const sendBackButton = iframe.contentDocument.querySelector("input[type='submit'][name='back']");
                    if (sendBackButton) sendBackButton.click();
                }
                
                setTimeout(() => {
                    document.body.removeChild(iframe);
                    processNextVillage();
                }, 100);
            };
        }

        processNextVillage();
    }

    function createPopup() {
        const popup = document.createElement("div");
        popup.style.position = "fixed";
        popup.style.bottom = "10px";
        popup.style.right = "10px";
        popup.style.width = "200px";
        popup.style.padding = "10px";
        popup.style.backgroundColor = "#333";
        popup.style.color = "#fff";
        popup.style.borderRadius = "5px";
        popup.style.zIndex = "1000";
        popup.innerHTML = "<h4>Processing...</h4><p id='popupText'></p>";
        document.body.appendChild(popup);
        return popup;
    }

    function updatePopup(message) {
        const popupText = document.getElementById("popupText");
        if (popupText) popupText.innerText = message;
    }

    fetchVillageIDs(currentPage);
})();
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
Current time: 21-11-2024, 12:33