Auto GK equip

Envía rápidamente !gk al presionar K en HaxBall

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Auto GK equip
// @namespace    haxball-gk
// @version      3.1
// @author       MIZUELMASCAPITOFORRO/NICK HAXBALL
// @description  Envía rápidamente !gk al presionar K en HaxBall
// @match        https://www.haxball.com/*
// @grant        none
// @license      MIZUELMASCAPITOFORRO
// ==/UserScript==

(function () {
    'use strict';

    const TECLA = 'k';
    const COMANDO = '!gk';

    document.addEventListener('keydown', function (e) {

        // Solo reaccionar a K
        if (e.key.toLowerCase() !== TECLA || e.repeat) {
            return;
        }

        // Si ya estás escribiendo en un campo, no hacer nada
        const active = document.activeElement;

        if (
            active &&
            (
                active.tagName === 'INPUT' ||
                active.tagName === 'TEXTAREA' ||
                active.isContentEditable
            )
        ) {
            return;
        }

        e.preventDefault();

        // Abrir el chat
        document.dispatchEvent(new KeyboardEvent('keydown', {
            key: 'Enter',
            code: 'Enter',
            keyCode: 13,
            which: 13,
            bubbles: true
        }));

        setTimeout(() => {

            // Buscar específicamente elementos que puedan ser el chat
            const inputs = Array.from(document.querySelectorAll(
                'input, textarea, [contenteditable="true"]'
            ));

            const input = inputs.find(el => {
                const rect = el.getBoundingClientRect();
                return rect.width > 0 &&
                       rect.height > 0 &&
                       getComputedStyle(el).visibility !== 'hidden';
            });

            if (!input) {
                console.log('[Auto GK] No se encontró el chat.');
                return;
            }

            input.focus();

            // Escribir el comando
            if (input.isContentEditable) {
                input.textContent = COMANDO;
            } else {
                input.value = COMANDO;
            }

            input.dispatchEvent(new Event('input', {
                bubbles: true
            }));

            // Enviar
            setTimeout(() => {

                input.dispatchEvent(new KeyboardEvent('keydown', {
                    key: 'Enter',
                    code: 'Enter',
                    keyCode: 13,
                    which: 13,
                    bubbles: true
                }));

                input.dispatchEvent(new KeyboardEvent('keyup', {
                    key: 'Enter',
                    code: 'Enter',
                    keyCode: 13,
                    which: 13,
                    bubbles: true
                }));

            }, 5);

        }, 20);

    });

    console.log('[Auto GK] Activado - K = !gk');

})();