// ==UserScript== // @name 元梦之星农场辅助Q群121234447 // @namespace https://your-namespace.com // @match *://gamer.qq.com/v2/game/* // @grant none // @version 1.6 // @author QQ1277745546 // @description 完美实现两套流程的最终版 // @license 允许使用代码, // @require https://code.jquery.com/jquery-3.6.0.min.js // ==/UserScript== (function() { 'use strict'; // ===== 配置 ===== const config = { loopInterval: 30000, uiMinimized: false, buttonsToClick: [ { selector: ".cancel-btn", desc: "取消按钮", customPos: true, x: 879, y: 650 }, { selector: ".reset-btn", desc: "重置位置", customPos: true, x: 1839, y: 905 } ], keySequenceWASD: [ // WASD模式流程 { key: " ", times: 10, interval: 800, delayAfter: 1500 }, { key: "a", duration: 800, delayAfter: 0 }, { key: "q" } ], keySequenceNormal: [ // 普通模式流程 { key: "r", delayAfter: 1500 }, { key: "Shift", times: 10, interval: 800, delayAfter: 1500 }, { key: "a", duration: 800, delayAfter: 0 }, { key: "q" } ], isCapturingPos: false, currentCaptureFor: null, shouldStop: false, qqGroupLink: "https://qm.qq.com/q/esXtDSP2WA", qqGroupNumber: "121234447", useWASD: false, adLink: "https://h5.lot-ml.com/ProductEn/Shop/a6f01a0fe5698a60" }; // ===== 状态变量 ===== let isRunning = false; let timer = null; let cycleCount = 0; let iframe = null; let adShown = false; // ===== 核心流程执行 ===== async function executeSequence() { if (config.shouldStop) return; cycleCount++; log(`\n🔄 开始第 ${cycleCount} 次循环`); updateStatus(); // 第一步:总是点击取消按钮 const cancelBtn = config.buttonsToClick[0]; if (cancelBtn.customPos) { if (!await clickAtPosition(cancelBtn.x, cancelBtn.y)) { log(`⚠️ 取消按钮点击失败: [${cancelBtn.x}, ${cancelBtn.y}]`); } else { log(`🖱️ 点击取消按钮坐标: [${cancelBtn.x}, ${cancelBtn.y}]`); } } else { const element = document.querySelector(cancelBtn.selector); if (element) { simulateRealClick(element); log(`🖱️ 点击取消按钮`); } else { log(`⚠️ 未找到取消按钮`); } } await delay(500); if (cancelBtn.customPos) { if (!await clickAtPosition(cancelBtn.x, cancelBtn.y)) { log(`⚠️ 取消按钮点击失败: [${cancelBtn.x}, ${cancelBtn.y}]`); } else { log(`🖱️ 点击取消按钮坐标: [${cancelBtn.x}, ${cancelBtn.y}]`); } } else { const element = document.querySelector(cancelBtn.selector); if (element) { simulateRealClick(element); log(`🖱️ 点击取消按钮`); } else { log(`⚠️ 未找到取消按钮`); } } await delay(500); // 第二步:根据模式执行不同流程 const resetBtn = config.buttonsToClick[1]; if (config.useWASD) { // WASD模式流程 if (resetBtn.customPos) { // 先移动鼠标到取消按钮位置 await moveMouseToPosition(cancelBtn.x, cancelBtn.y); if (!await clickAtPosition(cancelBtn.x, cancelBtn.y)) { log(`⚠️ 取消按钮点击失败: [${cancelBtn.x}, ${cancelBtn.y}]`); } else { log(`🖱️ 点击取消按钮坐标: [${cancelBtn.x}, ${cancelBtn.y}]`); } await delay(500); // 再次点击取消按钮 await moveMouseToPosition(cancelBtn.x, cancelBtn.y); if (!await clickAtPosition(cancelBtn.x, cancelBtn.y)) { log(`⚠️ 取消按钮点击失败: [${cancelBtn.x}, ${cancelBtn.y}]`); } else { log(`🖱️ 点击取消按钮坐标: [${cancelBtn.x}, ${cancelBtn.y}]`); } await delay(500); // 点击重置位置按钮 await moveMouseToPosition(resetBtn.x, resetBtn.y); if (!await clickAtPosition(resetBtn.x, resetBtn.y)) { log(`⚠️ 重置位置点击失败: [${resetBtn.x}, ${resetBtn.y}]`); } else { log(`🖱️ 点击重置位置坐标: [${resetBtn.x}, ${resetBtn.y}]`); } await delay(1000); // 再次点击重置位置按钮 await moveMouseToPosition(resetBtn.x, resetBtn.y); if (!await clickAtPosition(resetBtn.x, resetBtn.y)) { log(`⚠️ 重置位置点击失败: [${resetBtn.x}, ${resetBtn.y}]`); } else { log(`🖱️ 点击重置位置坐标: [${resetBtn.x}, ${resetBtn.y}]`); } await delay(1000); } else { const element = document.querySelector(resetBtn.selector); if (element) { simulateRealClick(element); log(`🖱️ 点击重置位置`); } else { log(`⚠️ 未找到重置位置按钮`); } } await delay(1000); // 等待1秒 // 执行WASD模式按键序列 for (const step of config.keySequenceWASD) { if (config.shouldStop) break; if (step.times) { for (let i = 1; i <= step.times; i++) { if (config.shouldStop) break; await pressKey(step.key); log(`⇧ ${step.key} 第 ${i} 次`); if (i < step.times) await delay(step.interval); } } else if (step.duration) { await holdKey(step.key, step.duration); } else { await pressKey(step.key); } if (step.delayAfter && !config.shouldStop) await delay(step.delayAfter); } } else { // 普通模式流程 await pressKey("r"); log(`⌨ 按下R键`); await delay(1500); // 等待1.5秒 // 执行普通模式按键序列 for (const step of config.keySequenceNormal.slice(1)) { // 跳过第一个R键 if (config.shouldStop) break; if (step.times) { for (let i = 1; i <= step.times; i++) { if (config.shouldStop) break; await pressKey(step.key); log(`⇧ ${step.key} 第 ${i} 次`); if (i < step.times) await delay(step.interval); } } else if (step.duration) { await holdKey(step.key, step.duration); } else { await pressKey(step.key); } if (step.delayAfter && !config.shouldStop) await delay(step.delayAfter); } } if (!config.shouldStop) { log(`⏱ 下次执行在 ${config.loopInterval/1000} 秒后`); timer = setTimeout(executeSequence, config.loopInterval); } } // ===== 控制函数 ===== function startAuto() { if (isRunning) return; config.shouldStop = false; isRunning = true; updateStatus(); log("🚀 启动自动化任务"); log(`当前模式: ${config.useWASD ? "无摇杆模式" : "有摇杆普通模式"}`); executeSequence(); } async function moveMouseToPosition(x, y) { return new Promise(resolve => { const mouseMove = new MouseEvent('mousemove', { bubbles: true, cancelable: true, view: window, clientX: x, clientY: y, screenX: x + window.screenX, screenY: y + window.screenY }); document.dispatchEvent(mouseMove); setTimeout(resolve, 50); // 小延迟确保鼠标移动到位 }); } async function stopAuto() { if (!isRunning) return; config.shouldStop = true; isRunning = false; if (timer) clearTimeout(timer); updateStatus(); log("🛑 正在停止任务..."); // 停止后1秒按R键 await delay(1000); if (!isRunning) { await pressKey("r"); // WASD模式流程 if (config.useWASD) { if (resetBtn.customPos) { // 移动鼠标到重置位置并点击 await moveMouseToPosition(resetBtn.x, resetBtn.y); if (!await clickAtPosition(resetBtn.x, resetBtn.y)) { log(`⚠️ 重置位置点击失败: [${resetBtn.x}, ${resetBtn.y}]`); } else { log(`🖱️ 点击重置位置坐标: [${resetBtn.x}, ${resetBtn.y}]`); } } else { const element = document.querySelector(resetBtn.selector); if (element) { simulateRealClick(element); log(`🖱️ 点击重置位置`); } else { log(`⚠️ 未找到重置位置按钮`); } } } log("⌨ 已发送停止信号(R键)"); } } // ===== UI界面 ===== function createUI() { if (document.getElementById("auto-ui-container")) return; // 创建iframe容器 const iframeContainer = document.createElement('div'); iframeContainer.id = 'auto-ui-container'; iframeContainer.style.position = 'fixed'; iframeContainer.style.top = '20px'; iframeContainer.style.right = '20px'; iframeContainer.style.width = '320px'; iframeContainer.style.height = '800px'; iframeContainer.style.zIndex = '2147483647'; iframeContainer.style.pointerEvents = 'none'; // 创建iframe iframe = document.createElement('iframe'); iframe.id = 'auto-ui-iframe'; iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.border = 'none'; iframe.style.borderRadius = '8px'; iframe.style.boxShadow = '0 4px 12px rgba(0,0,0,0.3)'; iframe.style.pointerEvents = 'auto'; iframe.sandbox = 'allow-scripts allow-same-origin'; iframeContainer.appendChild(iframe); document.body.appendChild(iframeContainer); // 写入iframe内容 const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; iframeDoc.open(); iframeDoc.write(`
大流量卡首月赠送话费,点击下方按钮立即领取
`;
document.body.appendChild(popup);
// 关闭按钮事件
const closeBtn = popup.querySelector('#ad-close-btn');
closeBtn.addEventListener('click', () => {
document.body.removeChild(popup);
adShown = true;
});
// 5秒后自动关闭
setTimeout(() => {
if (document.body.contains(popup)) {
document.body.removeChild(popup);
adShown = true;
}
}, 50000);
}, 3000); // 3秒后显示广告
}
// ===== 辅助函数 =====
function updateStatus() {
const statusText = isRunning ? "🟢 运行中" : "🔴 已停止";
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage({
type: 'updateStatus',
status: statusText
}, '*');
}
}
function log(message) {
const time = new Date().toLocaleTimeString();
const logText = `[${time}] ${message}`;
console.log(logText);
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage({
type: 'log',
message: logText
}, '*');
}
}
function toggleMinimize() {
const container = iframe.parentElement;
const content = iframe.contentDocument.getElementById('ui-content');
const btn = iframe.contentDocument.getElementById('minimize-btn');
config.uiMinimized = !config.uiMinimized;
if (config.uiMinimized) {
container.style.width = '150px';
container.style.height = '80px';
content.style.display = 'none';
btn.textContent = '↗';
} else {
container.style.width = '420px';
container.style.height = '500px';
content.style.display = 'block';
btn.textContent = '−';
}
}
// ===== 工具函数 =====
function simulateRealClick(element) {
const rect = element.getBoundingClientRect();
const x = rect.left + rect.width/2;
const y = rect.top + rect.height/2;
const mouseOver = new MouseEvent('mouseover', { bubbles: true, clientX: x, clientY: y });
const mouseDown = new MouseEvent('mousedown', { bubbles: true, clientX: x, clientY: y });
const mouseUp = new MouseEvent('mouseup', { bubbles: true, clientX: x, clientY: y });
const click = new MouseEvent('click', { bubbles: true, clientX: x, clientY: y });
element.dispatchEvent(mouseOver);
element.dispatchEvent(mouseDown);
element.dispatchEvent(mouseUp);
element.dispatchEvent(click);
}
async function clickAtPosition(x, y) {
return new Promise(resolve => {
// 先移动鼠标到指定位置
const mouseMove = new MouseEvent('mousemove', {
bubbles: true,
cancelable: true,
view: window,
clientX: x,
clientY: y,
screenX: x + window.screenX,
screenY: y + window.screenY
});
document.dispatchEvent(mouseMove);
// 等待一小段时间确保鼠标移动到位
setTimeout(() => {
const element = document.elementFromPoint(x, y);
if (element) {
const mouseDown = new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
view: window,
clientX: x,
clientY: y,
screenX: x + window.screenX,
screenY: y + window.screenY,
button: 0 // 左键
});
const mouseUp = new MouseEvent('mouseup', {
bubbles: true,
cancelable: true,
view: window,
clientX: x,
clientY: y,
screenX: x + window.screenX,
screenY: y + window.screenY,
button: 0 // 左键
});
const click = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
clientX: x,
clientY: y,
button: 0 // 左键
});
element.dispatchEvent(mouseDown);
setTimeout(() => {
element.dispatchEvent(mouseUp);
element.dispatchEvent(click);
resolve(true);
}, 50);
} else {
resolve(false);
}
}, 100); // 增加100ms延迟确保鼠标移动到位
});
}
function pressKey(key) {
return new Promise(resolve => {
if (config.shouldStop) return resolve();
const code = key === "Shift" ? "ShiftLeft" : `Key${key.toUpperCase()}`;
const keyCode = key === "Shift" ? 16 : key.toUpperCase().charCodeAt(0);
const downEvent = new KeyboardEvent("keydown", {
key, code, keyCode,
bubbles: true,
cancelable: true,
composed: true
});
document.dispatchEvent(downEvent);
setTimeout(() => {
const upEvent = new KeyboardEvent("keyup", {
key, code, keyCode,
bubbles: true,
cancelable: true,
composed: true
});
document.dispatchEvent(upEvent);
log(`⌨ 按键: ${key}`);
resolve();
}, 50);
});
}
function holdKey(key, duration) {
return new Promise(resolve => {
if (config.shouldStop) return resolve();
const code = key === "Shift" ? "ShiftLeft" : `Key${key.toUpperCase()}`;
const keyCode = key === "Shift" ? 16 : key.toUpperCase().charCodeAt(0);
const downEvent = new KeyboardEvent("keydown", {
key, code, keyCode,
bubbles: true,
cancelable: true,
composed: true
});
document.dispatchEvent(downEvent);
log(`🔼 长按: ${key}`);
setTimeout(() => {
document.dispatchEvent(new KeyboardEvent("keyup", {
key, code, keyCode,
bubbles: true,
cancelable: true,
composed: true
}));
log(`🔽 松开: ${key}`);
resolve();
}, duration);
});
}
function delay(ms) {
return new Promise(resolve => {
if (config.shouldStop) return resolve();
setTimeout(resolve, ms);
});
}
function startPositionCapture(buttonIndex) {
config.isCapturingPos = true;
config.currentCaptureFor = buttonIndex;
log(`🎯 开始捕捉坐标 (${config.buttonsToClick[buttonIndex].desc})`);
log("👉 请点击目标位置...");
document.body.style.cursor = "crosshair";
iframe.style.pointerEvents = "none";
}
function handlePositionCapture(e) {
if (!config.isCapturingPos) return;
e.preventDefault();
e.stopPropagation();
const x = e.clientX;
const y = e.clientY;
const buttonIndex = config.currentCaptureFor;
config.buttonsToClick[buttonIndex].x = x;
config.buttonsToClick[buttonIndex].y = y;
config.buttonsToClick[buttonIndex].customPos = true;
if (iframe.contentDocument) {
iframe.contentDocument.getElementById(`pos-x-${buttonIndex}`).value = x;
iframe.contentDocument.getElementById(`pos-y-${buttonIndex}`).value = y;
iframe.contentDocument.getElementById(`use-custom-${buttonIndex}`).checked = true;
}
log(`✔ 已记录坐标: [${x}, ${y}] (${config.buttonsToClick[buttonIndex].desc})`);
endPositionCapture();
}
function endPositionCapture() {
config.isCapturingPos = false;
config.currentCaptureFor = null;
document.body.style.cursor = "";
iframe.style.pointerEvents = "auto";
}
// ===== 初始化 =====
window.addEventListener('load', function() {
// 创建水印和群链接
const watermark = document.createElement('div');
watermark.innerHTML = `