AFK macro

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

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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