Auto GK equip

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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');

})();