Hello There, Guest! Register

Map/Coord Coordgrabber by continent
#1
Author : Carbon
Contributors : Nik12111
Quickbar Entry  :
Code:
javascript:(function () {
    if (!window.location.href.endsWith('=ranking')) {
        alert('Please run this script on the =ranking page.');
        return;
    }

    const urlParams = new URLSearchParams(window.location.search);
    const server = window.location.hostname;
    const village = urlParams.get('village');

    async function fetchData(url) {
        const response = await fetch(url);
        const text = await response.text();
        const parser = new DOMParser();
        return parser.parseFromString(text, 'text/html');
    }

    async function collectPlayerData() {
        let currentPage = 1;
        let players = {};

        while (true) {
            const url = `${window.location.origin}/game.php?village=${village}&screen=ranking&page=${currentPage}`;
            const doc = await fetchData(url);

            const playerLinks = doc.querySelectorAll('a[href*="screen=info_player&id="]');
            playerLinks.forEach(link => {
                const playerId = new URL(link.href).searchParams.get('id');
                players[link.textContent.trim()] = playerId;
            });

            const nextPageLink = doc.querySelector(`a[href*="screen=ranking&page=${currentPage + 1}"]`);
            if (!nextPageLink) break;
            currentPage++;
        }

        return players;
    }

    function createPopup(title, content) {
        const popup = document.createElement('div');
        popup.style.position = 'fixed';
        popup.style.top = '50%';
        popup.style.left = '50%';
        popup.style.transform = 'translate(-50%, -50%)';
        popup.style.background = '#f4e2d0';
        popup.style.border = '2px solid #b38b6d';
        popup.style.padding = '20px';
        popup.style.borderRadius = '8px';
        popup.style.zIndex = '10000';
        popup.style.width = '400px';
        popup.style.boxShadow = '0px 0px 15px rgba(0, 0, 0, 0.2)';

        const titleEl = document.createElement('h2');
        titleEl.textContent = title;
        titleEl.style.textAlign = 'center';
        titleEl.style.color = '#5a3d2a';
        titleEl.style.marginBottom = '10px';

        popup.appendChild(titleEl);
        popup.appendChild(content);
        document.body.appendChild(popup);
        return popup;
    }

    function showPlayerSelectionPopup(players) {
        const content = document.createElement('div');

        const dropdown = document.createElement('select');
        dropdown.style.width = '100%';
        dropdown.style.marginBottom = '10px';
        Object.keys(players).forEach(playerName => {
            const option = document.createElement('option');
            option.value = players[playerName];
            option.textContent = playerName;
            dropdown.appendChild(option);
        });

        const cancelButton = document.createElement('button');
        cancelButton.textContent = 'Cancel';
        cancelButton.style.background = '#8b5e3c';
        cancelButton.style.color = 'white';
        cancelButton.style.border = 'none';
        cancelButton.style.padding = '8px 12px';
        cancelButton.style.marginRight = '10px';
        cancelButton.style.cursor = 'pointer';
        cancelButton.style.borderRadius = '4px';
        cancelButton.onclick = () => document.body.removeChild(popup);

        const grabberButton = document.createElement('button');
        grabberButton.textContent = 'Grab Coordinates';
        grabberButton.style.background = '#8b5e3c';
        grabberButton.style.color = 'white';
        grabberButton.style.border = 'none';
        grabberButton.style.padding = '8px 12px';
        grabberButton.style.cursor = 'pointer';
        grabberButton.style.borderRadius = '4px';
        grabberButton.onclick = async () => {
            const selectedPlayerId = dropdown.value;
            const coordinates = await fetchCoordinates(selectedPlayerId);
            showCoordinatesPopup(coordinates);
            document.body.removeChild(popup);
        };

        content.appendChild(dropdown);
        content.appendChild(cancelButton);
        content.appendChild(grabberButton);

        const popup = createPopup('Carbons Coordinate Grabber', content);
    }

    async function fetchCoordinates(playerId) {
        let currentPage = 1;
        let coordinates = {};

        while (true) {
            const url = `${window.location.origin}/game.php?village=${village}&screen=info_player&id=${playerId}&page=${currentPage}`;
            const doc = await fetchData(url);

            const coordLinks = doc.querySelectorAll('a[href*="screen=info_village&id="]');
            coordLinks.forEach(link => {
                const continentText = link.textContent.match(/\s(K\d+)/);
                const coordText = link.textContent.match(/\((\d+\|\d+)\)/);
                if (coordText) {
                    if (!coordinates[continentText[1]]) {
                        coordinates[continentText[1]] = [];
                    }
                    coordinates[continentText[1]].push(coordText[1]);
                }
            });

            const nextPageLink = doc.querySelector(`a[href*="screen=info_player&id=${playerId}&page=${currentPage + 1}"]`);
            if (!nextPageLink) break;
            currentPage++;
        }

        return coordinates;
    }

    function showCoordinatesPopup(coordinates) {
        let coordList = '';
        const content = document.createElement('div');
        Object.entries(coordinates).forEach((continent) => {
            coordList += ' ' + continent[1].join(' ');
            const headerDiv = document.createElement('div');
            headerDiv.style.display = 'flex';
            headerDiv.style.width = '360px';
            headerDiv.style.alignItems = 'center';
            headerDiv.style.justifyContent = 'space-between';
            headerDiv.style.marginBottom = '2px';

            const subTitleEl = document.createElement('h3');
            subTitleEl.textContent = continent[0];
            subTitleEl.style.textAlign = 'left';
            subTitleEl.style.color = '#5a3d2a';
            subTitleEl.style.marginBottom = '10px';
            headerDiv.appendChild(subTitleEl);

            const textArea = document.createElement('textarea');
            textArea.style.width = '360px';
            textArea.style.height = '150px';
            textArea.value = continent[1].join(' ');
            textArea.style.marginBottom = '10px';
            textArea.style.border = '1px solid #b38b6d';
            textArea.style.borderRadius = '4px';
            textArea.style.padding = '5px';

            const copyButton = document.createElement('button');
            copyButton.textContent = 'Copy';
            copyButton.style.background = '#8b5e3c';
            copyButton.style.color = 'white';
            copyButton.style.border = 'none';
            copyButton.style.padding = '8px 12px';
            copyButton.style.cursor = 'pointer';
            copyButton.style.borderRadius = '2px';
            copyButton.onclick = () => {
                navigator.clipboard.writeText(textArea.value);
                document.body.removeChild(popup);
                UI.SuccessMessage(`Coordinates of ${continent[0]} copied to clipboard.`)
            };

            headerDiv.appendChild(copyButton);
            content.appendChild(headerDiv);
            content.appendChild(textArea);
        });

        const cancelButton = document.createElement('button');
        cancelButton.textContent = 'Cancel';
        cancelButton.style.background = '#8b5e3c';
        cancelButton.style.color = 'white';
        cancelButton.style.border = 'none';
        cancelButton.style.padding = '8px 12px';
        cancelButton.style.marginRight = '10px';
        cancelButton.style.cursor = 'pointer';
        cancelButton.style.borderRadius = '4px';
        cancelButton.onclick = () => document.body.removeChild(popup);

        const copyButton = document.createElement('button');
        copyButton.textContent = 'Copy all';
        copyButton.style.background = '#8b5e3c';
        copyButton.style.color = 'white';
        copyButton.style.border = 'none';
        copyButton.style.padding = '8px 12px';
        copyButton.style.cursor = 'pointer';
        copyButton.style.borderRadius = '4px';
        copyButton.onclick = () => {
            navigator.clipboard.writeText(coordList);
            document.body.removeChild(popup);
            UI.SuccessMessage(`All Coordinates copied to clipboard.`)
        };

        content.appendChild(cancelButton);
        content.appendChild(copyButton);

        const popup = createPopup('Carbons Coordinate Grabber', content);
    }

    async function main() {
        const players = await collectPlayerData();
        if (Object.keys(players).length === 0) {
            alert('No players found.');
            return;
        }
        showPlayerSelectionPopup(players);
    }

    main();
})();


Description : Updates the script to also show the coordinates by continent. Also changes the alert to a more friendly message.


Attached Files Thumbnail(s)
       
Reply
#2
very nice coord grabber for continent grabbing awesome work
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
Current time: 02-04-2025, 03:37