1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
(function() { const keys = ['ArrowUp', 'ArrowRight', 'ArrowDown', 'ArrowLeft']; const keyCodes = { 'ArrowUp': 38, 'ArrowDown': 40, 'ArrowLeft': 37, 'ArrowRight': 39 }; let intervalId = null; let isPaused = false; let isStopped = false; let pressCount = 0; function simulateKey(direction) { const code = keyCodes[direction]; const downEvent = new KeyboardEvent('keydown', { key: direction, code: direction, keyCode: code, which: code, bubbles: true, cancelable: true, composed: true }); document.dispatchEvent(downEvent); const upEvent = new KeyboardEvent('keyup', { key: direction, code: direction, keyCode: code, which: code, bubbles: true, cancelable: true, composed: true }); document.dispatchEvent(upEvent); pressCount++; if (pressCount % 10 === 0) { console.log(`[方向键模拟] 已发送 ${pressCount} 次按键,最近一次: ${direction}`); } } function getRandomDirection() { return keys[Math.floor(Math.random() * keys.length)]; } function fireRandomKey() { if (isStopped) return; if (document.hidden) { return; } const direction = getRandomDirection(); simulateKey(direction); } function startTimer() { if (intervalId !== null) { clearInterval(intervalId); intervalId = null; } if (isStopped) return; intervalId = setInterval(fireRandomKey, 1); } function pause() { if (intervalId !== null) { clearInterval(intervalId); intervalId = null; isPaused = true; console.log('⏸️ 页面进入后台,按键模拟已暂停'); } } function resume() { if (isStopped) return; if (intervalId === null && isPaused) { startTimer(); isPaused = false; console.log('▶️ 页面回到前台,按键模拟已恢复'); } } function stop() { if (intervalId !== null) { clearInterval(intervalId); intervalId = null; } isStopped = true; isPaused = false; console.log(`🛑 模拟器已完全停止,总计模拟按键 ${pressCount} 次`); } document.addEventListener('visibilitychange', function() { if (document.hidden) { pause(); } else { resume(); } }); window.stopRandomKeys = stop; function start() { if (intervalId !== null) { console.warn('⚠️ 定时器已在运行,请勿重复启动'); return; } isStopped = false; isPaused = false; pressCount = 0; startTimer(); console.log('✅ 模拟器已启动:每 50ms 随机按上或右方向键'); console.log('📌 页面切换到后台时自动暂停,回到前台自动恢复'); console.log('🛑 完全停止请执行: stopRandomKeys()'); } if (window.stopRandomKeys) { window.stopRandomKeys(); } start(); })();
|