自动抢购点击器

自动识别按钮并在指定时间点击,用于抢购场景

目前為 2025-09-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name         自动抢购点击器
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动识别按钮并在指定时间点击,用于抢购场景
// @author       shenfangda
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建设置面板
    function createPanel() {
        const panel = document.createElement('div');
        panel.id = 'auto-clicker-panel';
        panel.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            width: 300px;
            background: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 15px;
            box-shadow: 0 0 10px rgba(0,0,0,0.3);
            z-index: 10000;
            font-family: Arial, sans-serif;
        `;
        
        panel.innerHTML = `
            <h3 style="margin-top: 0;">自动抢购点击器</h3>
            <div>
                <label>按钮ID/选择器:</label><br>
                <input type="text" id="button-selector" placeholder="#buy-button 或 .submit-btn" style="width: 100%; padding: 5px; margin: 5px 0;">
            </div>
            <div>
                <label>抢购时间:</label><br>
                <input type="datetime-local" id="click-time" style="width: 100%; padding: 5px; margin: 5px 0;">
            </div>
            <div>
                <button id="start-btn" style="background: #4CAF50; color: white; border: none; padding: 8px 15px; margin-right: 5px; cursor: pointer;">开始</button>
                <button id="stop-btn" style="background: #f44336; color: white; border: none; padding: 8px 15px; cursor: pointer;">停止</button>
            </div>
            <div id="status" style="margin-top: 10px; font-size: 14px;"></div>
        `;
        
        document.body.appendChild(panel);
        
        // 绑定事件
        document.getElementById('start-btn').addEventListener('click', startClicker);
        document.getElementById('stop-btn').addEventListener('click', stopClicker);
    }

    let clickTimer = null;
    let observer = null;

    // 开始抢购
    function startClicker() {
        const selector = document.getElementById('button-selector').value;
        const clickTime = document.getElementById('click-time').value;
        const status = document.getElementById('status');
        
        if (!selector) {
            status.innerHTML = '<span style="color: red;">请输入按钮ID或选择器</span>';
            return;
        }
        
        if (!clickTime) {
            status.innerHTML = '<span style="color: red;">请选择抢购时间</span>';
            return;
        }
        
        // 立即尝试查找按钮
        findAndWatchButton(selector);
        
        // 设置定时点击
        const targetTime = new Date(clickTime).getTime();
        const now = new Date().getTime();
        const delay = targetTime - now;
        
        if (delay <= 0) {
            status.innerHTML = '<span style="color: red;">设置的时间已过期</span>';
            return;
        }
        
        status.innerHTML = '<span style="color: orange;">等待抢购中...</span>';
        
        clickTimer = setTimeout(() => {
            clickButton(selector);
        }, delay);
        
        // 更新倒计时显示
        updateCountdown(targetTime);
    }

    // 更新倒计时
    function updateCountdown(targetTime) {
        const status = document.getElementById('status');
        const interval = setInterval(() => {
            const now = new Date().getTime();
            const distance = targetTime - now;
            
            if (distance <= 0) {
                clearInterval(interval);
                return;
            }
            
            const hours = Math.floor(distance / (1000 * 60 * 60));
            const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = Math.floor((distance % (1000 * 60)) / 1000);
            
            status.innerHTML = `<span style="color: orange;">倒计时: ${hours}时${minutes}分${seconds}秒</span>`;
        }, 1000);
    }

    // 查找并监视按钮
    function findAndWatchButton(selector) {
        // 先尝试立即查找
        const button = document.querySelector(selector);
        if (button) {
            highlightButton(button);
        }
        
        // 使用 MutationObserver 监视 DOM 变化
        observer = new MutationObserver(() => {
            const button = document.querySelector(selector);
            if (button) {
                highlightButton(button);
            }
        });
        
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // 高亮显示按钮
    function highlightButton(button) {
        button.style.outline = '2px solid #4CAF50';
        button.style.boxShadow = '0 0 10px #4CAF50';
    }

    // 点击按钮
    function clickButton(selector) {
        const button = document.querySelector(selector);
        const status = document.getElementById('status');
        
        if (button) {
            button.click();
            status.innerHTML = '<span style="color: green;">已点击按钮!</span>';
        } else {
            status.innerHTML = '<span style="color: red;">未找到按钮,请检查选择器</span>';
        }
    }

    // 停止抢购
    function stopClicker() {
        if (clickTimer) {
            clearTimeout(clickTimer);
            clickTimer = null;
        }
        
        if (observer) {
            observer.disconnect();
            observer = null;
        }
        
        const status = document.getElementById('status');
        status.innerHTML = '<span style="color: blue;">已停止</span>';
    }

    // 初始化
    window.addEventListener('load', createPanel);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址