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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| import asyncio import json import random import websockets
class CDPInputSimulator: """CDP 输入模拟器""" KEY_MAP = { "Enter": "Enter", "Tab": "Tab", "Backspace": "Backspace", "Delete": "Delete", "Escape": "Escape", "ArrowUp": "ArrowUp", "ArrowDown": "ArrowDown", "ArrowLeft": "ArrowLeft", "ArrowRight": "ArrowRight", "Home": "Home", "End": "End", "PageUp": "PageUp", "PageDown": "PageDown", "Shift": "Shift", "Control": "Control", "Alt": "Alt", "Meta": "Meta", "Space": " ", } def __init__(self, ws, session_id): self.ws = ws self.session_id = session_id self._cmd_id = 0 async def _cdp(self, method, params=None): self._cmd_id += 1 msg = {"id": self._cmd_id, "method": method, "params": params or {}} if self.session_id: msg["sessionId"] = self.session_id await self.ws.send(json.dumps(msg)) async for resp in self.ws: data = json.loads(resp) if data.get("id") == self._cmd_id: return data.get("result", {}) def _modifiers(self, alt=False, ctrl=False, meta=False, shift=False): mask = 0 if alt: mask |= 1 if ctrl: mask |= 2 if meta: mask |= 4 if shift: mask |= 8 return mask async def click(self, x: int, y: int, button: str = "left", click_count: int = 1, modifiers: int = 0): """在指定坐标点击""" btn = {"left": 0, "middle": 1, "right": 2}.get(button, 0) await self._cdp("Input.dispatchMouseEvent", { "type": "mouseMoved", "x": x, "y": y, "button": btn, "buttons": 0, "clickCount": click_count, "modifiers": modifiers }) await self._cdp("Input.dispatchMouseEvent", { "type": "mousePressed", "x": x, "y": y, "button": btn, "buttons": 1, "clickCount": click_count, "modifiers": modifiers }) await asyncio.sleep(0.05) await self._cdp("Input.dispatchMouseEvent", { "type": "mouseReleased", "x": x, "y": y, "button": btn, "buttons": 0, "clickCount": click_count, "modifiers": modifiers }) async def double_click(self, x: int, y: int): """双击""" await self.click(x, y, click_count=1) await asyncio.sleep(0.1) await self.click(x, y, click_count=2) async def right_click(self, x: int, y: int): """右键点击""" await self.click(x, y, button="right") async def move_mouse(self, x: int, y: int, steps: int = 10): """平滑移动鼠标""" start_x, start_y = 0, 0 for i in range(1, steps + 1): cx = start_x + (x - start_x) * i // steps cy = start_y + (y - start_y) * i // steps if i < steps: cx += random.randint(-2, 2) cy += random.randint(-2, 2) await self._cdp("Input.dispatchMouseEvent", { "type": "mouseMoved", "x": cx, "y": cy, "button": 0, "buttons": 0, "clickCount": 1 }) await asyncio.sleep(0.01) async def drag(self, start_x: int, start_y: int, end_x: int, end_y: int, steps: int = 20): """拖拽操作""" await self._cdp("Input.dispatchMouseEvent", { "type": "mousePressed", "x": start_x, "y": start_y, "button": 0, "buttons": 1, "clickCount": 1 }) for i in range(1, steps + 1): cx = start_x + (end_x - start_x) * i // steps cy = start_y + (end_y - start_y) * i // steps await self._cdp("Input.dispatchMouseEvent", { "type": "mouseMoved", "x": cx, "y": cy, "button": 0, "buttons": 1, "clickCount": 1 }) await asyncio.sleep(0.015) await self._cdp("Input.dispatchMouseEvent", { "type": "mouseReleased", "x": end_x, "y": end_y, "button": 0, "buttons": 0, "clickCount": 1 }) async def scroll(self, delta_x: int = 0, delta_y: int = 300, x: int = 400, y: int = 400): """鼠标滚轮""" await self._cdp("Input.dispatchMouseEvent", { "type": "mouseWheel", "x": x, "y": y, "deltaX": delta_x, "deltaY": delta_y }) async def press_key(self, key: str): """按下一个键并释放""" await self._cdp("Input.dispatchKeyEvent", { "type": "keyDown", "key": key }) await asyncio.sleep(0.05) await self._cdp("Input.dispatchKeyEvent", { "type": "keyUp", "key": key }) async def type_text(self, text: str, delay: float = 0.05): """逐字输入文本""" for char in text: await self._cdp("Input.dispatchKeyEvent", { "type": "keyDown", "key": char, "text": char }) await self._cdp("Input.dispatchKeyEvent", { "type": "char", "key": char, "text": char }) await self._cdp("Input.dispatchKeyEvent", { "type": "keyUp", "key": char, "text": char }) await asyncio.sleep(delay) async def insert_text(self, text: str): """直接插入文本(绕过键盘事件)""" await self._cdp("Input.insertText", {"text": text}) async def hotkey(self, *keys): """组合键:hotkey('Control', 'a') → Ctrl+A""" modifiers = 0 mod_map = {"Control": 2, "Alt": 1, "Shift": 8, "Meta": 4} for k in keys[:-1]: if k in mod_map: modifiers |= mod_map[k] await self._cdp("Input.dispatchKeyEvent", { "type": "keyDown", "key": k }) main_key = keys[-1] await self._cdp("Input.dispatchKeyEvent", { "type": "rawKeyDown", "key": main_key, "modifiers": modifiers }) await asyncio.sleep(0.05) await self._cdp("Input.dispatchKeyEvent", { "type": "keyUp", "key": main_key, "modifiers": modifiers }) for k in reversed(keys[:-1]): await self._cdp("Input.dispatchKeyEvent", { "type": "keyUp", "key": k }) async def touch_tap(self, x: int, y: int): """触摸点击""" await self._cdp("Input.dispatchTouchEvent", { "type": "touchStart", "touchPoints": [{"x": x, "y": y}], "modifiers": 0 }) await asyncio.sleep(0.05) await self._cdp("Input.dispatchTouchEvent", { "type": "touchEnd", "touchPoints": [], "modifiers": 0 }) async def touch_swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, steps: int = 15): """触摸滑动""" await self._cdp("Input.dispatchTouchEvent", { "type": "touchStart", "touchPoints": [{"x": start_x, "y": start_y}], "modifiers": 0 }) for i in range(1, steps + 1): cx = start_x + (end_x - start_x) * i // steps cy = start_y + (end_y - start_y) * i // steps await self._cdp("Input.dispatchTouchEvent", { "type": "touchMove", "touchPoints": [{"x": cx, "y": cy}], "modifiers": 0 }) await asyncio.sleep(0.01) await self._cdp("Input.dispatchTouchEvent", { "type": "touchEnd", "touchPoints": [], "modifiers": 0 }) async def focus_element(self, css_selector: str): """聚焦元素""" await self._cdp("Runtime.evaluate", { "expression": f"document.querySelector('{css_selector}').focus()" }) async def click_element(self, css_selector: str): """通过 JS 直接点击元素(不模拟鼠标)""" await self._cdp("Runtime.evaluate", { "expression": f"document.querySelector('{css_selector}').click()" })
async def human_like_typing(self, text: str, target_wpm: int = 120): """模拟真人打字(带随机延迟变化)""" base_delay = 60.0 / (target_wpm * 5) for char in text: delay = base_delay * random.uniform(0.5, 1.8) await self.type_text(char, 0) await asyncio.sleep(delay)
|