GlarMenu [MiniHack]

( glar.io ) AutoRespawn, Qblock, AutoHat [ E - Speedhat, J - hotkayhat, RightCLick - tank ]

// ==UserScript==
// @name         GlarMenu [MiniHack]
// @namespace    http://tampermonkey.net/
// @version      v0.4
// @description  ( glar.io ) AutoRespawn, Qblock, AutoHat [ E - Speedhat, J - hotkayhat, RightCLick - tank ]
// @author       super_kanal
// @match        glar.io/*
// @grant        GM_addStyle
// ==/UserScript==

//       v0.5
// + Improved Hat Selection with Close Button Click
// + Automatic Menu Hiding and Showing
// + Support for Russian Keyboard Layouts
// + Enhanced Error Handling for Hat Selection
//
//
//      v0.4
// + Quick Hat Selection with Close Button Click
// + Improved Hat Selection Speed
// + Updated styles and functionality
//
//
//       v0.3
// + VisualLobby
// + Qblock [z,x,c]
// + White Leaderboard [ Hoobo ]
// + AutoHat [ E - Speedhat, J - hotkayhat, RightCLick - tank ]
//
//
//       v0.2
// + AutoHat
// + Qblock
// + Yellow Leaderboard
//
//
//       v0.1
// + Menu
// + AutoRespawn

window.addEventListener('load', function() {
    var lobbyLayout = document.getElementById('lobby-layout');
    if (lobbyLayout) {
        lobbyLayout.style.backgroundImage = 'url("https://wallbox.ru/resize/1920x1080/wallpapers/main/201132/tekstury-53f489588ab6.jpg")';
        lobbyLayout.style.backgroundSize = 'cover';
        lobbyLayout.style.backgroundPosition = 'center';
    }
});

window.addEventListener('load', function() {
    const titleLogo = document.querySelector('#title-logo');
    if (titleLogo) {
        titleLogo.style.fontFamily = "'Hobo Std', sans-serif";
        titleLogo.style.color = "white";
        titleLogo.style.webkitTextStroke = "1px black";
        titleLogo.style.textStroke = "1px black";
        titleLogo.style.fontSize = "800%";

        const subtitle = document.createElement('div');
        subtitle.textContent = "";
        subtitle.style.fontFamily = "'Hobo Std', sans-serif";
        subtitle.style.color = "RED";
        subtitle.style.webkitTextStroke = "1px black";
        subtitle.style.textStroke = "1px black";
        subtitle.style.fontSize = "250%";
        titleLogo.parentNode.insertBefore(subtitle, titleLogo.nextSibling);
    }

    const loginButton = document.querySelector('#login-button');
    if (loginButton) {
        loginButton.style.backgroundColor = "red";
    }

    const loginElement = document.querySelector('#login');
    if (loginElement) {
        loginElement.style.border = "2px solid red";
    }
});

const link = document.createElement('link');
link.href = 'https://fonts.cdnfonts.com/css/hobo-std';
link.rel = 'stylesheet';
document.head.appendChild(link);

(function() {
    'use strict';

    let menu = document.createElement('div');
    menu.id = 'glarMenu';
    document.body.appendChild(menu);

    GM_addStyle(`
        #glarMenu {
            position: fixed;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            width: 240px;
            height: 220px;
            background-color: rgba(255, 165, 0, 0.7);
            border: 2px solid white;
            border-radius: 8px;
            opacity: 0;
            transition: opacity 0.3s ease;
            z-index: 9999;
            pointer-events: none;
        }

        #glarMenu.open {
            opacity: 1;
            pointer-events: auto;
        }

        #glarMenu label {
            display: block;
            margin-bottom: 10px;
            color: white;
            font-size: 14px;
        }

        #glarMenu label input[type="checkbox"] {
            vertical-align: middle;
            margin-right: 5px;
        }

        #clanSpamContainer {
            display: flex;
            align-items: center;
        }

        #clanSpamCheckbox {
            flex-shrink: 0;
        }

        #ui-input-team-name {
            width: 50%;
            margin-left: 10px;
            margin-right: 5px;
        }

        .ui-leaderboard .ui-leaderboard-player {
            color: white;
        }
    `);

    let world = window.world;
    let autoRespawnCheckbox = document.createElement('label');
    autoRespawnCheckbox.innerHTML = `
        <input type="checkbox" id="autoRespawnCheckbox">
        AutoRespawn
    `;
    menu.appendChild(autoRespawnCheckbox);

    function toggleMenu(event) {
        if (event.key === 'Escape') {
            menu.classList.toggle('open');
        }
    }

    document.addEventListener('keydown', toggleMenu);

    function checkAndAutoRespawn() {
        if (document.querySelector('#autoRespawnCheckbox').checked) {
            let spawnText = document.querySelector('body').innerText.includes('Spawn');
            let respawnButton = document.querySelector('#respawn-button');
            if (spawnText && respawnButton) {
                let rect = respawnButton.getBoundingClientRect();
                if (rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
                    rect.left >= 0 && rect.right <= (window.innerWidth || document.documentElement.clientWidth)) {
                    respawnButton.click();
                    showChatMessage();
                }
            }
        }
    }

    function showChatMessage() {
        let chatMessage = document.createElement('div');
        chatMessage.classList.add('chat');
        chatMessage.textContent = 'MiniHack ;)';
        document.querySelector('#e79639f54be').appendChild(chatMessage);

        setTimeout(() => {
            chatMessage.style.opacity = '0';
        }, 5000);

        setTimeout(() => {
            chatMessage.remove();
        }, 5500);
    }

    document.querySelector('#autoRespawnCheckbox').addEventListener('change', checkAndAutoRespawn);
    setInterval(checkAndAutoRespawn, 500);

    const keyToBlockId = {
        '1': '10',
        '2': '11',
        '3': '12',
        '4': '31',
        '5': '19',
        '6': '30',
        '7': '29',
        '8': '18',
        'z': '32',
        'x': '16',
        'c': '13',
        'с': '13',
        'я': '32',
        'ч': '16',
    };

    function clickBlockById(blockId) {
        const block = document.querySelector(`#bottom-panel-build .item[id="${blockId}"], #left-panel .item[id="${blockId}"]`);
        if (block) {
            block.click();
        }
    }

    let lastSelectedKey = '';
    let currentInputActive = false;

    document.addEventListener('keydown', (event) => {
        if (!currentInputActive) {
            const key = event.key.toLowerCase();
            if (key === 'q' || key === 'й') {
                if (lastSelectedKey && keyToBlockId[lastSelectedKey]) {
                    clickBlockById(keyToBlockId[lastSelectedKey]);
                }
            }

            if (keyToBlockId[key]) {
                lastSelectedKey = key;
            }
        }
    });

    document.addEventListener('keydown', function(e) {
        if (!currentInputActive) {
            if (e.keyCode === 69) {
                toggleHat('https://assets.glar.io/uploads/entity_asset/image/104/chesshat.png', 'https://glar.io/assets/b56e2935102053e9521880c67032d833.png');
            }
            if (e.keyCode === 74) {
                toggleHat('https://assets.glar.io/uploads/entity_asset/image/67/hockeyhat.png', 'https://glar.io/assets/b56e2935102053e9521880c67032d833.png');
            }
        }
    });

    document.addEventListener('mousedown', function(e) {
        if (!currentInputActive && e.button === 2) {
            toggleHat('https://assets.glar.io/uploads/entity_asset/image/57/tankhat.png', 'https://glar.io/assets/b56e2935102053e9521880c67032d833.png');
        }
    });

    function toggleHat(primaryHatUrl, secondaryHatUrl) {
        const hats = document.querySelectorAll('.ui-hat-list-item');
        let hatFound = false;

        hats.forEach(function(hat) {
            const hatImageUrl = hat.querySelector('img').src;
            if (hatImageUrl === primaryHatUrl || hatImageUrl === secondaryHatUrl) {
                hatFound = true;
                hat.click();
                setTimeout(closeHatList, 0);
            }
        });

        if (!hatFound) {
            hats.forEach(function(hat) {
                const hatImageUrl = hat.querySelector('img').src;
                if (hatImageUrl === 'https://glar.io/assets/b56e2935102053e9521880c67032d833.png') {
                    hat.click();
                    setTimeout(closeHatList, 0);
                }
            });
        }
    }

    function closeHatList() {
        const closeButton = document.getElementById('ui-hat-close-button');
        if (closeButton) {
            closeButton.click();
        }
    }

    function checkInputElement() {
        const inputElement = document.querySelector('.d631d361bc3 input[type="text"]');
        currentInputActive = inputElement && inputElement.offsetParent !== null;
    }

    setInterval(checkInputElement, 0);
})();