黑雨|自动化操作

输入坐标和倒计时就可以自动化点击

// ==UserScript==
// @name         黑雨|自动化操作
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  输入坐标和倒计时就可以自动化点击
// @author       Your Name
// @match        *://*/*
// @grant        none
// @license      No License
// ==/UserScript==

(function() {
    'use strict';

    // 创建一个固定位置的UI框
    var positionBox = document.createElement('div');
    positionBox.style.position = 'fixed';
    positionBox.style.bottom = '10px';
    positionBox.style.right = '10px';
    positionBox.style.width = '300px';
    positionBox.style.height = '200px';
    positionBox.style.padding = '10px';
    positionBox.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
    positionBox.style.color = 'white';
    positionBox.style.borderRadius = '5px';
    positionBox.style.zIndex = '1000';
    positionBox.style.fontFamily = 'Arial, sans-serif';
    positionBox.style.fontSize = '14px';
    positionBox.style.cursor = 'move';  // 让鼠标显示为移动样式
    document.body.appendChild(positionBox);

    // 添加标题
    var title = document.createElement('div');
    title.textContent = '黑雨 | 自动化操作';
    title.style.fontSize = '16px';
    title.style.fontWeight = 'bold';
    title.style.marginBottom = '10px';
    positionBox.appendChild(title);

    // 创建坐标X输入框
    var coordXInput = document.createElement('input');
    coordXInput.type = 'number';
    coordXInput.placeholder = 'x坐标';
    coordXInput.style.display = 'inline-block';
    coordXInput.style.marginTop = '10px';
    coordXInput.style.width = 'calc(50% - 12px)'; // 减去 padding 和 margin
    coordXInput.style.marginRight = '4px';
    positionBox.appendChild(coordXInput);

    // 创建坐标Y输入框
    var coordYInput = document.createElement('input');
    coordYInput.type = 'number';
    coordYInput.placeholder = 'y坐标';
    coordYInput.style.display = 'inline-block';
    coordYInput.style.marginTop = '10px';
    coordYInput.style.width = 'calc(50% - 12px)'; // 减去 padding 和 margin
    coordYInput.style.marginLeft = '4px';
    positionBox.appendChild(coordYInput);

    // 创建倒计时输入框
    var countdownInput = document.createElement('input');
    countdownInput.type = 'number';
    countdownInput.placeholder = '输入倒计时(以毫秒为单位)';
    countdownInput.style.display = 'block';
    countdownInput.style.marginTop = '10px';
    countdownInput.style.width = 'calc(100% - 20px)'; // 减去 padding
    positionBox.appendChild(countdownInput);

    // 创建启动按钮
    var startButton = document.createElement('button');
    startButton.textContent = '执行';
    startButton.style.display = 'block';
    startButton.style.marginTop = '10px';
    startButton.style.width = 'calc(100% - 20px)'; // 减去 padding
    startButton.style.padding = '10px';
    startButton.style.backgroundColor = '#007bff';
    startButton.style.color = 'white';
    startButton.style.border = 'none';
    startButton.style.borderRadius = '5px';
    startButton.style.cursor = 'pointer';
    positionBox.appendChild(startButton);

    // 显示当前鼠标位置
    var mousePositionDisplay = document.createElement('div');
    mousePositionDisplay.style.marginTop = '10px';
    mousePositionDisplay.style.width = 'calc(100% - 20px)'; // 减去 padding
    positionBox.appendChild(mousePositionDisplay);

    document.addEventListener('mousemove', function(event) {
        var x = event.clientX;
        var y = event.clientY;
        mousePositionDisplay.textContent = '捕获的位置: x:' + x + ', y:' + y;
    });

    startButton.addEventListener('click', function() {
        var x = parseInt(coordXInput.value, 10);
        var y = parseInt(coordYInput.value, 10);
        var countdown = parseInt(countdownInput.value, 10);

        if (isNaN(x) || isNaN(y) || isNaN(countdown)) {
            alert('请输入有效坐标和倒计时。');
            return;
        }

        setTimeout(function() {
            simulateMouseClick(x, y);
        }, countdown);
    });

    function simulateMouseClick(x, y) {
        var event = new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window,
            clientX: x,
            clientY: y
        });
        var target = document.elementFromPoint(x, y);
        if (target) {
            target.dispatchEvent(event);
        }
    }

    // 使UI框可拖动
    positionBox.onmousedown = function(event) {
        var shiftX = event.clientX - positionBox.getBoundingClientRect().left;
        var shiftY = event.clientY - positionBox.getBoundingClientRect().top;

        function moveAt(pageX, pageY) {
            positionBox.style.left = pageX - shiftX + 'px';
            positionBox.style.top = pageY - shiftY + 'px';
        }

        function onMouseMove(event) {
            moveAt(event.pageX, event.pageY);
        }

        document.addEventListener('mousemove', onMouseMove);

        document.onmouseup = function() {
            document.removeEventListener('mousemove', onMouseMove);
            positionBox.onmouseup = null;
        };

        positionBox.onmouseup = function() {
            document.removeEventListener('mousemove', onMouseMove);
            positionBox.onmouseup = null;
        };
    };

    positionBox.ondragstart = function() {
        return false;
    };
})();