AFK macro

Toggle with F8, periodically sends keyboard inputs in to prevent AFK kicking

Terá de instalar uma extensão como Tampermonkey, Greasemonkey ou Violentmonkey para instalar este script.

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

Terá de instalar uma extensão como Tampermonkey ou Violentmonkey para instalar este script.

Terá de instalar uma extensão como Tampermonkey ou Userscripts para instalar este script.

Terá de instalar uma extensão como Tampermonkey para instalar este script.

Terá de instalar uma extensão de gestão de scripts de utilizador para instalar este script.

(Já tenho um gestor de scripts de utilizador, deixe-me instalá-lo!)

Terá de instalar uma extensão como Stylus para instalar este estilo.

Terá de instalar uma extensão como Stylus para instalar este estilo.

Terá de instalar uma extensão como Stylus para instalar este estilo.

Terá de instalar uma extensão de gestão de estilos de utilizador para instalar este estilo.

Terá de instalar uma extensão de gestão de estilos de utilizador para instalar este estilo.

Terá de instalar uma extensão de gestão de estilos de utilizador para instalar este estilo.

(Já tenho um gestor de estilos de utilizador, deixe-me instalá-lo!)

// ==UserScript==
// @name         AFK macro
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Toggle with F8, periodically sends keyboard inputs in to prevent AFK kicking
// @author       Qwerty Matthew
// @match        https://minefun.io/*
// @match        https://*.minefun.io/*
// @match        http://minefun.io/*
// @match        http://*.minefun.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=minefun.io
// @grant        none
// @run-at       document-idle
// @license      ISC
// ==/UserScript==
// ==UserScript==

(function () {
    'use strict';

    let afkActive = false;
    let timerId = null;
    let notifyTimeout = null;

    // Create Screen Notification Overlay
    const notifyBox = document.createElement('div');
    notifyBox.style.position = 'fixed';
    notifyBox.style.bottom = '20px';
    notifyBox.style.left = '20px';
    notifyBox.style.padding = '10px 18px';
    notifyBox.style.borderRadius = '8px';
    notifyBox.style.fontWeight = 'bold';
    notifyBox.style.fontSize = '14px';
    notifyBox.style.fontFamily = 'sans-serif';
    notifyBox.style.color = '#ffffff';
    notifyBox.style.zIndex = '999999';
    notifyBox.style.display = 'none';
    notifyBox.style.opacity = '0';
    notifyBox.style.transition = 'opacity 0.2s ease';
    notifyBox.style.pointerEvents = 'none';
    document.body.appendChild(notifyBox);

    function showNotification(text, isActive) {
        if (notifyTimeout) {
            clearTimeout(notifyTimeout);
        }

        notifyBox.textContent = text;
        notifyBox.style.backgroundColor = isActive ? 'rgba(46, 204, 113, 0.85)' : 'rgba(231, 76, 60, 0.85)';
        notifyBox.style.display = 'block';

        setTimeout(() => {
            notifyBox.style.opacity = '1';
        }, 10);

        // Hide after 1.5 seconds
        notifyTimeout = setTimeout(() => {
            notifyBox.style.opacity = '0';
            setTimeout(() => {
                notifyBox.style.display = 'none';
            }, 200);
        }, 1500);
    }

    function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function scheduleNextAction() {
        if (!afkActive) return;
        const nextInterval = getRandomInt(30000, 45000);
        timerId = setTimeout(triggerAFKMovement, nextInterval);
    }

    function triggerAFKMovement() {
        if (!afkActive) return;

        const keyCode = Math.random() > 0.5 ? 'KeyW' : 'KeyS';
        const target = document.querySelector('canvas') || window;

        target.dispatchEvent(new KeyboardEvent('keydown', { code: keyCode, key: keyCode === 'KeyW' ? 'w' : 's', bubbles: true }));

        setTimeout(() => {
            target.dispatchEvent(new KeyboardEvent('keyup', { code: keyCode, key: keyCode === 'KeyW' ? 'w' : 's', bubbles: true }));
        }, getRandomInt(100, 200));

        if (Math.random() > 0.5) {
            setTimeout(() => {
                target.dispatchEvent(new KeyboardEvent('keydown', { code: 'Space', key: ' ', bubbles: true }));
                setTimeout(() => {
                    target.dispatchEvent(new KeyboardEvent('keyup', { code: 'Space', key: ' ', bubbles: true }));
                }, getRandomInt(80, 150));
            }, 250);
        }

        scheduleNextAction();
    }

    window.addEventListener('keydown', (e) => {
        if (document.activeElement && (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA')) {
            return;
        }

        if (e.key === 'F8') {
            e.preventDefault();
            afkActive = !afkActive;

            if (afkActive) {
                showNotification('AFK Macro: ACTIVE', true);
                scheduleNextAction();
            } else {
                showNotification('AFK Macro: OFF', false);
                clearTimeout(timerId);
            }
        }
    });
})();