pid0r / Автокопание

// ==UserScript==
// @name         Автокопание
// @namespace    http://tampermonkey.net/
// @version      1.0.0.0
// @description  Копаем приятнее
// @author       pid0r
// @match        https://catwar.su/cw3/
// @license      MIT; https://opensource.org/licenses/MIT
// ==/UserScript==

(function() {
    'use strict';

    let intId = null;
    let cont;

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

    function clkBtn(dataId) {
        const btn = document.querySelector(`[data-id="${dataId}"]`);
        if (btn) {
            btn.click();
        }
    }

    function start() {
        function cyc() {
            clkBtn('17');
            setTimeout(() => {
                clkBtn('3');
                const int3 = rnd(101 * 1000, 140 * 1000);
                setTimeout(cyc, int3);
            }, rnd(301 * 1000, 340 * 1000));
        }
        cyc();
    }

    function createUI() {
        cont = document.createElement('div');
        cont.style.position = 'fixed';
        cont.style.top = localStorage.getItem('contTop') || '10px';
        cont.style.left = localStorage.getItem('contLeft') || '10px';
        cont.style.zIndex = '9999';
        cont.style.backgroundColor = 'rgb(34, 34, 34)';
        cont.style.color = 'rgb(131, 131, 131)';
        cont.style.borderRadius = '10px';
        cont.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.3)';
        cont.style.padding = '10px';
        cont.style.width = '160px';
        cont.style.cursor = 'move';

        const startBtn = document.createElement('button');
        startBtn.innerText = 'Старт';
        startBtn.style.width = '100%';
        startBtn.style.marginBottom = '10px';
        startBtn.style.backgroundColor = 'rgb(17, 17, 17)';
        startBtn.style.color = 'rgb(131, 131, 131)';
        startBtn.style.border = 'none';
        startBtn.style.borderRadius = '5px';
        startBtn.style.cursor = 'pointer';

        startBtn.onmousedown = () => {
            startBtn.style.backgroundColor = 'rgb(14, 14, 14)';
        };

        startBtn.onmouseup = () => {
            startBtn.style.backgroundColor = 'rgb(17, 17, 17)';
        };

        startBtn.onclick = () => {
            if (!intId) {
                start();
                startBtn.disabled = true;
                stopBtn.disabled = false;
            }
        };

        const stopBtn = document.createElement('button');
        stopBtn.innerText = 'Стоп';
        stopBtn.style.width = '100%';
        stopBtn.style.backgroundColor = 'rgb(17, 17, 17)';
        stopBtn.style.color = 'rgb(131, 131, 131)';
        stopBtn.style.border = 'none';
        stopBtn.style.borderRadius = '5px';
        stopBtn.style.cursor = 'pointer';

        stopBtn.onmousedown = () => {
            stopBtn.style.backgroundColor = 'rgb(14, 14, 14)';
        };

        stopBtn.onmouseup = () => {
            stopBtn.style.backgroundColor = 'rgb(17, 17, 17)';
        };

        stopBtn.onclick = () => {
            if (intId) {
                clearInterval(intId);
                intId = null;
                startBtn.disabled = false;
                stopBtn.disabled = true;
            }
        };

        cont.appendChild(startBtn);
        cont.appendChild(stopBtn);
        document.body.appendChild(cont);

        let drag = false;
        let offX, offY;

        cont.addEventListener('mousedown', function(e) {
            drag = true;
            offX = e.clientX - cont.getBoundingClientRect().left;
            offY = e.clientY - cont.getBoundingClientRect().top;
            cont.style.cursor = 'grabbing';
        });

        document.addEventListener('mousemove', function(e) {
            if (drag) {
                cont.style.left = `${e.clientX - offX}px`;
                cont.style.top = `${e.clientY - offY}px`;
            }
        });

        document.addEventListener('mouseup', function() {
            if (drag) {
                drag = false;
                cont.style.cursor = 'move';
                localStorage.setItem('contTop', cont.style.top);
                localStorage.setItem('contLeft', cont.style.left);
            }
        });

        cont.addEventListener('touchstart', function(e) {
            drag = true;
            const touch = e.touches[0];
            offX = touch.clientX - cont.getBoundingClientRect().left;
            offY = touch.clientY - cont.getBoundingClientRect().top;
            cont.style.cursor = 'grabbing';
        });

        document.addEventListener('touchmove', function(e) {
            if (drag) {
                const touch = e.touches[0];
                cont.style.left = `${touch.clientX - offX}px`;
                cont.style.top = `${touch.clientY - offY}px`;
            }
        });

        document.addEventListener('touchend', function() {
            if (drag) {
                drag = false;
                cont.style.cursor = 'move';
                localStorage.setItem('contTop', cont.style.top);
                localStorage.setItem('contLeft', cont.style.left);
            }
        });
    }

    createUI();
})();