// ==UserScript==
// @name 自动抢购点击器
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 自动识别按钮并在指定时间点击,用于抢购场景
// @author shenfangda
// @match *://*/*
// @grant none
// @downloadURL 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 = `
自动抢购点击器
`;
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 = '请输入按钮ID或选择器';
return;
}
if (!clickTime) {
status.innerHTML = '请选择抢购时间';
return;
}
// 立即尝试查找按钮
findAndWatchButton(selector);
// 设置定时点击
const targetTime = new Date(clickTime).getTime();
const now = new Date().getTime();
const delay = targetTime - now;
if (delay <= 0) {
status.innerHTML = '设置的时间已过期';
return;
}
status.innerHTML = '等待抢购中...';
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 = `倒计时: ${hours}时${minutes}分${seconds}秒`;
}, 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 = '已点击按钮!';
} else {
status.innerHTML = '未找到按钮,请检查选择器';
}
}
// 停止抢购
function stopClicker() {
if (clickTimer) {
clearTimeout(clickTimer);
clickTimer = null;
}
if (observer) {
observer.disconnect();
observer = null;
}
const status = document.getElementById('status');
status.innerHTML = '已停止';
}
// 初始化
window.addEventListener('load', createPanel);
})();