Hello There, Guest! Register

Share your scripts!
#3
This script automates both! Run using this script loaded in the quick bar on the targets user page. Choose which script you want and copy it - then add it to the quickbar. The fake or spy script can be used in the rally point, click once to load the attack, click again to send the attack.

[Image: final-script-generator.png]

Code:
javascript:async function fetchAllUserVillageCoordinates() {
    try {
        const playerId = extractPlayerIdFromUrl();
        if (!playerId) {
            throw new Error('Player ID not found in URL');
        }

        const initialUrl = `https://w1.infernal-wars.com/game.php?village=3&screen=info_player&id=${playerId}&page=1`;
        let nextPageUrl = initialUrl;
        let allCoordinates = [];
        let visitedPages = new Set();

        while (nextPageUrl && !visitedPages.has(nextPageUrl)) {
            visitedPages.add(nextPageUrl);
            const response = await fetchWithRetry(nextPageUrl);

            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }

            const html = await response.text();
            const coordinates = parsePlayerCoordinates(html);

            allCoordinates = allCoordinates.concat(coordinates);

            nextPageUrl = getNextPageUrl(html, nextPageUrl);
        }

        const uniqueCoordinates = Array.from(new Set(allCoordinates));
        const coordinatesToDisplay = uniqueCoordinates.slice(1);
        const coordinatesText = coordinatesToDisplay.join(' ');

        displayFinalScriptPopup(coordinatesText, coordinatesToDisplay.length);
    } catch (error) {
        console.error('Error fetching user villages:', error.message);
        displayDebuggingPopup(error.message);
    }
}

async function fetchWithRetry(url, retries = 3) {
    for (let attempt = 1; attempt <= retries; attempt++) {
        try {
            const response = await fetch(url);
            if (!response.ok && attempt < retries) {
                console.warn(`Retry attempt ${attempt} for URL: ${url}`);
                continue;
            }
            return response;
        } catch (error) {
            if (attempt === retries) throw error;
        }
    }
}

function parsePlayerCoordinates(html) {
    const regex = /\((\d+)\|(\d+)\)/g;
    let match;
    const coordinates = [];

    while ((match = regex.exec(html)) !== null) {
        const x = match[1];
        const y = match[2];
        coordinates.push(`${x}|${y}`);
    }

    return coordinates;
}

function getNextPageUrl(html, currentPageUrl) {
    const tempElement = document.createElement('div');
    tempElement.innerHTML = html;

    const currentPageMatch = currentPageUrl.match(/page=(\d+)/);
    const currentPage = currentPageMatch ? parseInt(currentPageMatch[1]) : 1;

    const nextPageElement = tempElement.querySelector(`a[href*="page=${currentPage + 1}"]`);

    return nextPageElement ? nextPageElement.href : null;
}

function extractPlayerIdFromUrl() {
    const urlParams = new URLSearchParams(window.location.search);
    return urlParams.get('id');
}

function generateAttackScript(coordinatesText, unitCounts, isSupport = false) {
    const scriptType = isSupport ? 'support' : 'attack';
    return `javascript:
var index = localStorage.getItem('currentIndex') ? parseInt(localStorage.getItem('currentIndex')) : 0;
var doc = document;

function sendNextCommand() {
    var currentCoords = coords[index];
    index = (index + 1) % coords.length;

    $('#place_target').find('input').val(currentCoords.x + '|' + currentCoords.y);
    doc.forms[0].x.value = currentCoords.x;
    doc.forms[0].y.value = currentCoords.y;

    ${Object.keys(unitCounts).map(unit => `doc.forms[0].unit_input_${unit}.value = ${unitCounts[unit]};`).join('\n')}

    $('#target_${scriptType}').click();

    localStorage.setItem('currentIndex', index);

    var url = doc.URL;
    if (url.indexOf('screen=place') == -1) {
        alert('Use the script in the rally point page!');
        return;
    }
}

if (window.frames.length > 0 && window.main != null) {
    doc = window.main.document;
}

var url = doc.URL;

if (url.indexOf('try=confirm') > 0) {
    document.getElementsByName("submit")[0].click();
}

if (url.indexOf('screen=place') == -1) {
    alert('Use the script in the rally point page!');
} else {
    var coordsString = '${coordinatesText}';

    var coordsPairs = coordsString.split(' ');

    function parseCoordinates(coordString) {
        var parts = coordString.split('|');
        if (parts.length === 2 && !isNaN(parts[0]) && !isNaN(parts[1])) {
            return { x: parseInt(parts[0]), y: parseInt(parts[1]) };
        }
        return null;
    }

    var coords = coordsPairs.map(parseCoordinates).filter(function(coord) {
        return coord !== null;
    });

    sendNextCommand();
}`;
}

function displayFinalScriptPopup(coordinatesText, coordinateCount) {
    const units = [
        { name: 'spear', label: 'Spear', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_spear.png?1' },
        { name: 'sword', label: 'Sword', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_sword.png?1' },
        { name: 'axe', label: 'Axe', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_axe.png?1' },
        { name: 'spy', label: 'Spy', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_spy.png?1' },
        { name: 'light', label: 'Light', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_light.png?1' },
        { name: 'heavy', label: 'Heavy', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_heavy.png?1' },
        { name: 'ram', label: 'Ram', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_ram.png?1' },
        { name: 'catapult', label: 'Catapult', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_catapult.png?1' },
        { name: 'snob', label: 'Snob', icon: 'https://cdn.infernal-wars.com/graphic/unit/unit_snob.png' }
    ];

    const popup = document.createElement('div');
    popup.className = 'final-script-popup';
    popup.style.position = 'fixed';
    popup.style.top = '50%';
    popup.style.left = '50%';
    popup.style.transform = 'translate(-50%, -50%)';
    popup.style.width = '700px';
    popup.style.height = '650px';
    popup.style.border = '2px solid #DED3B9';
    popup.style.backgroundColor = '#F4E4BC';
    popup.style.boxShadow = '0 4px 20px rgba(0,0,0,0.2)';
    popup.style.zIndex = '10000';
    popup.style.padding = '20px';
    popup.style.borderRadius = '10px';
    popup.style.display = 'flex';
    popup.style.flexDirection = 'column';
    popup.style.fontFamily = 'Arial, sans-serif';

    let offsetX = 0;
    let offsetY = 0;
    let isDragging = false;

    popup.addEventListener('mousedown', startDrag);

    function startDrag(e) {
        if (e.target === popup) {
            isDragging = true;
            offsetX = e.clientX - popup.getBoundingClientRect().left;
            offsetY = e.clientY - popup.getBoundingClientRect().top;
            document.addEventListener('mousemove', drag);
            document.addEventListener('mouseup', stopDrag);
        }
    }

    function drag(e) {
        if (isDragging) {
            popup.style.left = `${e.clientX - offsetX}px`;
            popup.style.top = `${e.clientY - offsetY}px`;
        }
    }

    function stopDrag() {
        isDragging = false;
        document.removeEventListener('mousemove', drag);
        document.removeEventListener('mouseup', stopDrag);
    }

    const header = document.createElement('div');
    header.style.display = 'flex';
    header.style.justifyContent = 'space-between';
    header.style.alignItems = 'center';
    header.style.marginBottom = '10px';
    header.style.borderBottom = '1px solid #DED3B9';
    header.style.paddingBottom = '10px';

    const title = document.createElement('h3');
    title.textContent = 'Generate Attack/Support Script';
    title.style.margin = '0';
    title.style.color = '#5D4037';
    header.appendChild(title);

    const closeButton = document.createElement('button');
    closeButton.textContent = '×';
    closeButton.style.border = 'none';
    closeButton.style.background = 'none';
    closeButton.style.fontSize = '24px';
    closeButton.style.cursor = 'pointer';
    closeButton.addEventListener('click', () => {
        popup.style.display = 'none';
    });
    header.appendChild(closeButton);

    popup.appendChild(header);

    const coordinatesInfo = document.createElement('p');
    coordinatesInfo.textContent = `Number of coordinates generated: ${coordinateCount}`;
    coordinatesInfo.style.marginBottom = '10px';
    coordinatesInfo.style.color = '#333';
    popup.appendChild(coordinatesInfo);

    const scriptTypeSelect = document.createElement('select');
    scriptTypeSelect.style.marginBottom = '10px';
    scriptTypeSelect.style.border = '1px solid #DED3B9';
    scriptTypeSelect.style.padding = '5px';
    const scriptTypes = [
        { name: 'Attack Script', value: 'attack' },
        { name: 'Support Script', value: 'support' },
    ];
    scriptTypes.forEach(type => {
        const option = document.createElement('option');
        option.value = type.value;
        option.textContent = type.name;
        scriptTypeSelect.appendChild(option);
    });
    popup.appendChild(scriptTypeSelect);

    const unitInputsContainer = document.createElement('div');
    unitInputsContainer.style.display = 'grid';
    unitInputsContainer.style.gridTemplateColumns = 'repeat(3, 1fr)';
    unitInputsContainer.style.gap = '10px';
    unitInputsContainer.style.marginBottom = '10px';

    units.forEach(unit => {
        const unitContainer = document.createElement('div');
        unitContainer.style.display = 'flex';
        unitContainer.style.flexDirection = 'column';
        unitContainer.style.alignItems = 'center';

        const unitLabel = document.createElement('label');
        unitLabel.textContent = unit.label;
        unitLabel.style.marginBottom = '5px';
        unitLabel.style.color = '#5D4037';
        unitContainer.appendChild(unitLabel);

        const unitImage = document.createElement('img');
        unitImage.src = unit.icon;
        unitImage.alt = unit.label;
        unitImage.style.width = '30px';
        unitImage.style.height = '30px';
        unitImage.style.marginBottom = '5px';
        unitContainer.appendChild(unitImage);

        const unitInput = document.createElement('input');
        unitInput.type = 'number';
        unitInput.min = '0';
        unitInput.value = '0';
        unitInput.style.border = '1px solid #DED3B9';
        unitInput.style.padding = '5px';
        unitInput.style.width = '100%';
        unitInput.id = `unit_${unit.name}`;
        unitContainer.appendChild(unitInput);

        unitInputsContainer.appendChild(unitContainer);
    });
    popup.appendChild(unitInputsContainer);

    const scriptArea = document.createElement('textarea');
    scriptArea.setAttribute('readonly', '');
    scriptArea.style.width = '100%';
    scriptArea.style.height = 'calc(100% - 370px)';
    scriptArea.style.border = '1px solid #DED3B9';
    scriptArea.style.borderRadius = '4px';
    scriptArea.style.padding = '10px';
    scriptArea.style.boxSizing = 'border-box';
    scriptArea.style.marginBottom = '10px';
    scriptArea.style.fontFamily = 'Courier New, monospace';
    scriptArea.style.backgroundColor = '#FAF3E0';
    popup.appendChild(scriptArea);

    const buttonContainer = document.createElement('div');
    buttonContainer.style.display = 'flex';
    buttonContainer.style.justifyContent = 'space-between';

    const generateButton = document.createElement('button');
    generateButton.textContent = 'Generate Script';
    generateButton.style.flex = '1';
    generateButton.style.marginRight = '10px';
    generateButton.style.padding = '10px';
    generateButton.style.border = '1px solid #DED3B9';
    generateButton.style.backgroundColor = '#8D6E63';
    generateButton.style.color = '#fff';
    generateButton.style.borderRadius = '4px';
    generateButton.style.cursor = 'pointer';
    generateButton.addEventListener('click', () => {
        const scriptType = scriptTypeSelect.value;
        let isSupport = scriptType === 'support';
        let unitCounts = {};

        units.forEach(unit => {
            const unitInput = document.getElementById(`unit_${unit.name}`);
            const unitCount = parseInt(unitInput.value, 10);
            if (unitCount > 0) {
                unitCounts[unit.name] = unitCount;
            }
        });

        const finalScript = generateAttackScript(coordinatesText, unitCounts, isSupport);
        scriptArea.value = finalScript;
    });
    buttonContainer.appendChild(generateButton);

    const copyButton = document.createElement('button');
    copyButton.textContent = 'Copy Script';
    copyButton.style.flex = '1';
    copyButton.style.padding = '10px';
    copyButton.style.border = '1px solid #DED3B9';
    copyButton.style.backgroundColor = '#8D6E63';
    copyButton.style.color = '#fff';
    copyButton.style.borderRadius = '4px';
    copyButton.style.cursor = 'pointer';
    copyButton.addEventListener('click', () => {
        scriptArea.select();
        document.execCommand('copy');
        alert('Script copied to clipboard!');
    });
    buttonContainer.appendChild(copyButton);

    popup.appendChild(buttonContainer);

    document.body.appendChild(popup);
}

function displayDebuggingPopup(errorMessage) {
    const popup = document.createElement('div');
    popup.className = 'debugging-popup';
    popup.style.position = 'fixed';
    popup.style.top = '50%';
    popup.style.left = '50%';
    popup.style.transform = 'translate(-50%, -50%)';
    popup.style.width = '400px';
    popup.style.height = '200px';
    popup.style.border = '1px solid #8D6E63';
    popup.style.backgroundColor = '#F4E4BC';
    popup.style.boxShadow = '0 4px 20px rgba(0,0,0,0.2)';
    popup.style.zIndex = '10000';
    popup.style.padding = '20px';
    popup.style.borderRadius = '10px';
    popup.style.display = 'flex';
    popup.style.flexDirection = 'column';
    popup.style.justifyContent = 'center';
    popup.style.alignItems = 'center';
    popup.style.fontFamily = 'Arial, sans-serif';

    const errorText = document.createElement('p');
    errorText.textContent = `Error: ${errorMessage}`;
    errorText.style.color = '#8D6E63';
    errorText.style.marginBottom = '20px';
    popup.appendChild(errorText);

    const closeButton = document.createElement('button');
    closeButton.textContent = 'Close';
    closeButton.style.border = '1px solid #8D6E63';
    closeButton.style.background = '#8D6E63';
    closeButton.style.color = '#fff';
    closeButton.style.padding = '10px 20px';
    closeButton.style.borderRadius = '4px';
    closeButton.style.cursor = 'pointer';
    closeButton.addEventListener('click', () => {
        popup.style.display = 'none';
    });
    popup.appendChild(closeButton);

    document.body.appendChild(popup);
}

fetchAllUserVillageCoordinates();
[Image: Chase-Banner.png]
Reply


Messages In This Thread
Share your scripts! - by Chase - 12-07-2024, 10:51
RE: Share your scripts! - by Chase - 14-07-2024, 04:46
RE: Share your scripts! - by Chase - 14-07-2024, 05:07
RE: Share your scripts! - by Chase - 17-07-2024, 13:24

Forum Jump:


Users browsing this thread: 1 Guest(s)
Current time: 22-10-2024, 10:24