AFK macro

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

您需要先安裝使用者腳本管理器擴展,如 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         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);
            }
        }
    });
})();