@acedatacloud/skills 2026.714.3 → 2026.715.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/package.json +1 -1
  2. package/skills/telegram/SKILL.md +20 -6
  3. package/skills/telegram/scripts/tg.py +34 -3
  4. package/skills/xiaohongshu/SKILL.md +76 -136
  5. package/skills/xiaohongshu/tests/test_browser_contract.py +79 -0
  6. package/skills/xiaohongshu/README.md +0 -36
  7. package/skills/xiaohongshu/requirements.txt +0 -1
  8. package/skills/xiaohongshu/scripts/vendor/LICENSE.autoclaw-xiaohongshu-skills +0 -21
  9. package/skills/xiaohongshu/scripts/vendor/title_utils.py +0 -57
  10. package/skills/xiaohongshu/scripts/vendor/xhs/__init__.py +0 -1
  11. package/skills/xiaohongshu/scripts/vendor/xhs/cdp.py +0 -940
  12. package/skills/xiaohongshu/scripts/vendor/xhs/comment.py +0 -283
  13. package/skills/xiaohongshu/scripts/vendor/xhs/errors.py +0 -92
  14. package/skills/xiaohongshu/scripts/vendor/xhs/feed_detail.py +0 -550
  15. package/skills/xiaohongshu/scripts/vendor/xhs/feeds.py +0 -49
  16. package/skills/xiaohongshu/scripts/vendor/xhs/human.py +0 -79
  17. package/skills/xiaohongshu/scripts/vendor/xhs/like_favorite.py +0 -188
  18. package/skills/xiaohongshu/scripts/vendor/xhs/login.py +0 -377
  19. package/skills/xiaohongshu/scripts/vendor/xhs/publish.py +0 -1208
  20. package/skills/xiaohongshu/scripts/vendor/xhs/publish_long_article.py +0 -293
  21. package/skills/xiaohongshu/scripts/vendor/xhs/publish_video.py +0 -183
  22. package/skills/xiaohongshu/scripts/vendor/xhs/search.py +0 -226
  23. package/skills/xiaohongshu/scripts/vendor/xhs/selectors.py +0 -97
  24. package/skills/xiaohongshu/scripts/vendor/xhs/stealth.py +0 -316
  25. package/skills/xiaohongshu/scripts/vendor/xhs/types.py +0 -473
  26. package/skills/xiaohongshu/scripts/vendor/xhs/urls.py +0 -30
  27. package/skills/xiaohongshu/scripts/vendor/xhs/user_profile.py +0 -111
  28. package/skills/xiaohongshu/scripts/xiaohongshu.py +0 -1182
  29. package/skills/xiaohongshu/tests/test_xiaohongshu.py +0 -967
@@ -1,940 +0,0 @@
1
- """CDP WebSocket 客户端(Browser, Page, Element),对应 Go browser/browser.go + go-rod API。
2
-
3
- 通过原生 WebSocket 与 Chrome DevTools Protocol 通信,实现浏览器自动化控制。
4
- """
5
-
6
- from __future__ import annotations
7
-
8
- import json
9
- import logging
10
- import os
11
- import random
12
- import select
13
- import time
14
- from collections import deque
15
- from typing import Any
16
-
17
- from .errors import CDPError, ElementNotFoundError
18
- from .stealth import STEALTH_JS, build_ua_override
19
-
20
- logger = logging.getLogger(__name__)
21
-
22
-
23
- class PipeTransport:
24
- """Null-delimited CDP transport used by Chrome remote-debugging-pipe."""
25
-
26
- def __init__(self, write_fd: int, read_fd: int) -> None:
27
- self.write_fd = write_fd
28
- self.read_fd = read_fd
29
- self.buffer = bytearray()
30
-
31
- def send(self, message: str) -> None:
32
- payload = message.encode("utf-8") + b"\0"
33
- while payload:
34
- written = os.write(self.write_fd, payload)
35
- payload = payload[written:]
36
-
37
- def recv(self, timeout: float) -> str:
38
- deadline = time.monotonic() + timeout
39
- while True:
40
- separator = self.buffer.find(b"\0")
41
- if separator >= 0:
42
- message = bytes(self.buffer[:separator])
43
- del self.buffer[: separator + 1]
44
- return message.decode("utf-8")
45
- remaining = deadline - time.monotonic()
46
- if remaining <= 0 or not select.select([self.read_fd], [], [], remaining)[0]:
47
- raise TimeoutError
48
- chunk = os.read(self.read_fd, 64 * 1024)
49
- if not chunk:
50
- raise CDPError("Chrome closed the CDP pipe")
51
- self.buffer.extend(chunk)
52
-
53
- def close(self) -> None:
54
- for fd in (self.write_fd, self.read_fd):
55
- try:
56
- os.close(fd)
57
- except OSError:
58
- pass
59
-
60
-
61
- class CDPClient:
62
- """底层 CDP WebSocket 通信客户端。"""
63
-
64
- def __init__(self, transport: PipeTransport) -> None:
65
- self._transport = transport
66
- self._id = 0
67
- self._callbacks: dict[int, Any] = {}
68
-
69
- def send(self, method: str, params: dict | None = None) -> dict:
70
- """发送 CDP 命令并等待结果。"""
71
- self._id += 1
72
- msg: dict[str, Any] = {"id": self._id, "method": method}
73
- if params:
74
- msg["params"] = params
75
- self._transport.send(json.dumps(msg))
76
- return self._wait_for(self._id)
77
-
78
- def _wait_for(self, msg_id: int, timeout: float = 30.0) -> dict:
79
- """等待指定 id 的响应。"""
80
- deadline = time.monotonic() + timeout
81
- while time.monotonic() < deadline:
82
- try:
83
- raw = self._transport.recv(max(0.1, deadline - time.monotonic()))
84
- except TimeoutError:
85
- break
86
- data = json.loads(raw)
87
- if data.get("id") == msg_id:
88
- if "error" in data:
89
- raise CDPError(f"CDP 错误: {data['error']}")
90
- return data.get("result", {})
91
- raise CDPError(f"等待 CDP 响应超时 (id={msg_id})")
92
-
93
- def close(self) -> None:
94
- import contextlib
95
-
96
- with contextlib.suppress(Exception):
97
- self._transport.close()
98
-
99
-
100
- class Page:
101
- """CDP 页面对象,封装常用操作。"""
102
-
103
- def __init__(self, cdp: CDPClient, target_id: str, session_id: str) -> None:
104
- self._cdp = cdp
105
- self.target_id = target_id
106
- self.session_id = session_id
107
- self._transport = cdp._transport
108
- self._id_counter = 1000
109
- self._events: deque[dict[str, Any]] = deque(maxlen=256)
110
-
111
- def _send_session(self, method: str, params: dict | None = None) -> dict:
112
- """向 session 发送命令。"""
113
- self._id_counter += 1
114
- msg: dict[str, Any] = {
115
- "id": self._id_counter,
116
- "method": method,
117
- "sessionId": self.session_id,
118
- }
119
- if params:
120
- msg["params"] = params
121
- self._transport.send(json.dumps(msg))
122
- return self._wait_session(self._id_counter)
123
-
124
- def _wait_session(self, msg_id: int, timeout: float = 60.0) -> dict:
125
- """等待 session 响应。"""
126
- deadline = time.monotonic() + timeout
127
- while time.monotonic() < deadline:
128
- try:
129
- raw = self._transport.recv(max(0.1, deadline - time.monotonic()))
130
- except TimeoutError:
131
- break
132
- data = json.loads(raw)
133
- if data.get("id") == msg_id:
134
- if "error" in data:
135
- raise CDPError(f"CDP 错误: {data['error']}")
136
- return data.get("result", {})
137
- if "method" in data and data.get("sessionId") == self.session_id:
138
- self._events.append(data)
139
- raise CDPError(f"等待 session 响应超时 (id={msg_id})")
140
-
141
- def clear_events(self) -> None:
142
- self._events.clear()
143
-
144
- def wait_for_event(self, timeout: float) -> dict[str, Any] | None:
145
- if self._events:
146
- return self._events.popleft()
147
- try:
148
- raw = self._transport.recv(timeout)
149
- except TimeoutError:
150
- return None
151
- data = json.loads(raw)
152
- if "method" in data and data.get("sessionId") == self.session_id:
153
- return data
154
- return None
155
-
156
- def navigate(self, url: str) -> None:
157
- """导航到指定 URL。"""
158
- logger.info("导航到: %s", url)
159
- self._send_session("Page.navigate", {"url": url})
160
-
161
- def set_cookies(self, cookies: list[dict[str, Any]]) -> None:
162
- """Install request-scoped cookies in this page's browser context."""
163
- self._send_session("Network.enable")
164
- self._send_session("Network.setCookies", {"cookies": cookies})
165
-
166
- def wait_for_load(self, timeout: float = 60.0) -> None:
167
- """等待页面加载完成(通过轮询 document.readyState)。"""
168
- deadline = time.monotonic() + timeout
169
- while time.monotonic() < deadline:
170
- try:
171
- state = self.evaluate("document.readyState")
172
- if state == "complete":
173
- return
174
- except CDPError:
175
- pass
176
- time.sleep(0.5)
177
- logger.warning("等待页面加载超时")
178
-
179
- def wait_dom_stable(self, timeout: float = 10.0, interval: float = 0.5) -> None:
180
- """等待 DOM 稳定(连续两次 DOM 快照一致)。"""
181
- last_html = ""
182
- deadline = time.monotonic() + timeout
183
- while time.monotonic() < deadline:
184
- try:
185
- html = self.evaluate("document.body ? document.body.innerHTML.length : 0")
186
- if html == last_html and html != "":
187
- return
188
- last_html = html
189
- except CDPError:
190
- pass
191
- time.sleep(interval)
192
-
193
- def evaluate(self, expression: str, timeout: float = 30.0) -> Any:
194
- """执行 JavaScript 表达式并返回结果。"""
195
- result = self._send_session(
196
- "Runtime.evaluate",
197
- {
198
- "expression": expression,
199
- "returnByValue": True,
200
- "awaitPromise": True,
201
- },
202
- )
203
- if "exceptionDetails" in result:
204
- raise CDPError(f"JS 执行异常: {result['exceptionDetails']}")
205
- remote_obj = result.get("result", {})
206
- return remote_obj.get("value")
207
-
208
- def evaluate_function(self, function_body: str, *args: Any) -> Any:
209
- """执行 JavaScript 函数并返回结果。
210
-
211
- function_body 是一个完整的函数体,如 `() => { return 1; }`
212
- """
213
- result = self._send_session(
214
- "Runtime.evaluate",
215
- {
216
- "expression": f"({function_body})()",
217
- "returnByValue": True,
218
- "awaitPromise": True,
219
- },
220
- )
221
- if "exceptionDetails" in result:
222
- raise CDPError(f"JS 函数执行异常: {result['exceptionDetails']}")
223
- remote_obj = result.get("result", {})
224
- return remote_obj.get("value")
225
-
226
- def query_selector(self, selector: str) -> str | None:
227
- """查找单个元素,返回 objectId 或 None。"""
228
- result = self._send_session(
229
- "Runtime.evaluate",
230
- {
231
- "expression": f"document.querySelector({json.dumps(selector)})",
232
- "returnByValue": False,
233
- },
234
- )
235
- remote_obj = result.get("result", {})
236
- if remote_obj.get("subtype") == "null" or remote_obj.get("type") == "undefined":
237
- return None
238
- return remote_obj.get("objectId")
239
-
240
- def query_selector_all(self, selector: str) -> list[str]:
241
- """查找多个元素,返回 objectId 列表。"""
242
- # 通过 JS 返回元素数量,然后逐个获取
243
- count = self.evaluate(f"document.querySelectorAll({json.dumps(selector)}).length")
244
- if not count:
245
- return []
246
- object_ids = []
247
- for i in range(count):
248
- result = self._send_session(
249
- "Runtime.evaluate",
250
- {
251
- "expression": (f"document.querySelectorAll({json.dumps(selector)})[{i}]"),
252
- "returnByValue": False,
253
- },
254
- )
255
- obj = result.get("result", {})
256
- oid = obj.get("objectId")
257
- if oid:
258
- object_ids.append(oid)
259
- return object_ids
260
-
261
- def has_element(self, selector: str) -> bool:
262
- """检查元素是否存在。"""
263
- return self.evaluate(f"document.querySelector({json.dumps(selector)}) !== null") is True
264
-
265
- def wait_for_element(self, selector: str, timeout: float = 30.0) -> str:
266
- """等待元素出现,返回 objectId。"""
267
- deadline = time.monotonic() + timeout
268
- while time.monotonic() < deadline:
269
- oid = self.query_selector(selector)
270
- if oid:
271
- return oid
272
- time.sleep(0.5)
273
- raise ElementNotFoundError(selector)
274
-
275
- def click_element(self, selector: str) -> None:
276
- """点击指定选择器的元素(通过 CDP Input 事件,isTrusted=true)。"""
277
- box = self.evaluate(
278
- f"""
279
- (() => {{
280
- const el = document.querySelector({json.dumps(selector)});
281
- if (!el) return null;
282
- el.scrollIntoView({{block: 'center'}});
283
- const rect = el.getBoundingClientRect();
284
- return {{x: rect.left + rect.width / 2, y: rect.top + rect.height / 2}};
285
- }})()
286
- """
287
- )
288
- if not box:
289
- return
290
- x = box["x"] + random.uniform(-3, 3)
291
- y = box["y"] + random.uniform(-3, 3)
292
- self.mouse_move(x, y)
293
- time.sleep(random.uniform(0.03, 0.08))
294
- self.mouse_click(x, y)
295
-
296
- def click_creator_tab(self, text: str) -> bool:
297
- """Click the real creator tab while excluding XHS honeypot copies."""
298
- document = self._send_session("DOM.getDocument", {"depth": -1, "pierce": True})
299
- target: dict[str, Any] | None = None
300
-
301
- def attrs(node: dict[str, Any]) -> dict[str, str]:
302
- values = node.get("attributes") or []
303
- return dict(zip(values[0::2], values[1::2]))
304
-
305
- def content(node: dict[str, Any]) -> str:
306
- chunks = [node.get("nodeValue") or ""]
307
- for child in node.get("children") or []:
308
- chunks.append(content(child))
309
- for shadow_root in node.get("shadowRoots") or []:
310
- chunks.append(content(shadow_root))
311
- return "".join(chunks).strip()
312
-
313
- def find(node: dict[str, Any]) -> None:
314
- nonlocal target
315
- if target is not None:
316
- return
317
- properties = attrs(node)
318
- classes = properties.get("class", "").split()
319
- if (
320
- node.get("nodeName") == "DIV"
321
- and "creator-tab" in classes
322
- and properties.get("data-hp-bound") == "1"
323
- and "data-hp-kind" not in properties
324
- and "button-hp-installed" not in properties
325
- and content(node) == text
326
- ):
327
- target = node
328
- return
329
- for child in node.get("children") or []:
330
- find(child)
331
- for shadow_root in node.get("shadowRoots") or []:
332
- find(shadow_root)
333
-
334
- find(document["root"])
335
- return self._click_dom_node(target) if target else False
336
-
337
- def _click_dom_node(self, target: dict[str, Any]) -> bool:
338
- node_ref = (
339
- {"backendNodeId": target["backendNodeId"]}
340
- if target.get("backendNodeId")
341
- else {"nodeId": target["nodeId"]}
342
- )
343
- self._send_session("DOM.scrollIntoViewIfNeeded", node_ref)
344
- model = self._send_session("DOM.getBoxModel", node_ref).get("model")
345
- if not model:
346
- return False
347
- quad = model.get("border") or model.get("content")
348
- if not quad or len(quad) < 8:
349
- return False
350
- x = sum(quad[0::2]) / 4
351
- y = sum(quad[1::2]) / 4
352
- self.mouse_move(x, y)
353
- time.sleep(random.uniform(0.03, 0.08))
354
- self.mouse_click(x, y)
355
- return True
356
-
357
- def click_pierced_button(self, host_name: str, button_text: str) -> bool:
358
- document = self._send_session("DOM.getDocument", {"depth": -1, "pierce": True})
359
- targets: list[dict[str, Any]] = []
360
-
361
- def attrs(node: dict[str, Any]) -> dict[str, str]:
362
- values = node.get("attributes") or []
363
- return dict(zip(values[0::2], values[1::2]))
364
-
365
- def content(node: dict[str, Any]) -> str:
366
- chunks = [node.get("nodeValue") or ""]
367
- for child in node.get("children") or []:
368
- chunks.append(content(child))
369
- for shadow_root in node.get("shadowRoots") or []:
370
- chunks.append(content(shadow_root))
371
- return "".join(chunks).strip()
372
-
373
- def find_button(node: dict[str, Any]) -> None:
374
- properties = attrs(node)
375
- if (
376
- node.get("nodeName") == "BUTTON"
377
- and content(node) == button_text
378
- and "disabled" not in properties
379
- and properties.get("aria-disabled") != "true"
380
- ):
381
- targets.append(node)
382
- return
383
- for child in node.get("children") or []:
384
- find_button(child)
385
- for shadow_root in node.get("shadowRoots") or []:
386
- find_button(shadow_root)
387
-
388
- def find_host(node: dict[str, Any]) -> None:
389
- properties = attrs(node)
390
- if node.get("nodeName") == host_name.upper() and properties.get("is-publish") == "true":
391
- for shadow_root in node.get("shadowRoots") or []:
392
- find_button(shadow_root)
393
- return
394
- for child in node.get("children") or []:
395
- find_host(child)
396
-
397
- find_host(document["root"])
398
- for target in targets:
399
- node_ref = {"backendNodeId": target["backendNodeId"]}
400
- try:
401
- self._send_session("DOM.scrollIntoViewIfNeeded", node_ref)
402
- model = self._send_session("DOM.getBoxModel", node_ref).get("model")
403
- except CDPError:
404
- continue
405
- if not model or not model.get("width") or not model.get("height"):
406
- continue
407
- quad = model.get("border") or model.get("content")
408
- if not quad or len(quad) < 8:
409
- continue
410
- x = sum(quad[0::2]) / 4
411
- y = sum(quad[1::2]) / 4
412
- hit = self._send_session(
413
- "DOM.getNodeForLocation",
414
- {"x": int(x), "y": int(y), "includeUserAgentShadowDOM": True},
415
- )
416
- hit_id = hit.get("backendNodeId")
417
- if hit_id != target.get("backendNodeId"):
418
- if not hit_id:
419
- continue
420
- try:
421
- hit_object_id = self._send_session(
422
- "DOM.resolveNode", {"backendNodeId": hit_id}
423
- ).get("object", {}).get("objectId")
424
- button_object_id = self._send_session(
425
- "DOM.resolveNode", node_ref
426
- ).get("object", {}).get("objectId")
427
- if not hit_object_id or not button_object_id:
428
- continue
429
- contains = self._send_session(
430
- "Runtime.callFunctionOn",
431
- {
432
- "objectId": hit_object_id,
433
- "functionDeclaration": "function(button) { return button.contains(this); }",
434
- "arguments": [{"objectId": button_object_id}],
435
- "returnByValue": True,
436
- },
437
- ).get("result", {}).get("value")
438
- except CDPError:
439
- contains = False
440
- if contains is not True:
441
- continue
442
- self.mouse_move(x, y)
443
- time.sleep(random.uniform(0.03, 0.08))
444
- self.mouse_click(x, y)
445
- return True
446
- return False
447
-
448
- def input_text(self, selector: str, text: str) -> None:
449
- """向指定选择器的元素输入文本。"""
450
- self.evaluate(
451
- f"""
452
- (() => {{
453
- const el = document.querySelector({json.dumps(selector)});
454
- if (!el) return;
455
- el.focus();
456
- el.value = {json.dumps(text)};
457
- el.dispatchEvent(new Event('input', {{bubbles: true}}));
458
- el.dispatchEvent(new Event('change', {{bubbles: true}}));
459
- }})()
460
- """
461
- )
462
-
463
- def input_content_editable(self, selector: str, text: str) -> None:
464
- """向 contentEditable 元素输入文本(CDP 逐字输入,模拟真实打字)。"""
465
- # 1. focus 元素
466
- self.evaluate(
467
- f"""
468
- (() => {{
469
- const el = document.querySelector({json.dumps(selector)});
470
- if (el) el.focus();
471
- }})()
472
- """
473
- )
474
- time.sleep(0.1)
475
- # 2. 全选清空(Ctrl+A + Backspace)
476
- self._send_session(
477
- "Input.dispatchKeyEvent",
478
- {"type": "keyDown", "key": "a", "code": "KeyA", "modifiers": 2},
479
- )
480
- self._send_session(
481
- "Input.dispatchKeyEvent",
482
- {"type": "keyUp", "key": "a", "code": "KeyA", "modifiers": 2},
483
- )
484
- self._send_session(
485
- "Input.dispatchKeyEvent",
486
- {
487
- "type": "keyDown",
488
- "key": "Backspace",
489
- "code": "Backspace",
490
- "windowsVirtualKeyCode": 8,
491
- },
492
- )
493
- self._send_session(
494
- "Input.dispatchKeyEvent",
495
- {
496
- "type": "keyUp",
497
- "key": "Backspace",
498
- "code": "Backspace",
499
- "windowsVirtualKeyCode": 8,
500
- },
501
- )
502
- time.sleep(0.1)
503
- # 3. 逐字输入(随机 30-80ms 间隔,换行符转为 Enter 键)
504
- for char in text:
505
- if char == "\n":
506
- self.press_key("Enter")
507
- else:
508
- self._send_session(
509
- "Input.dispatchKeyEvent",
510
- {"type": "keyDown", "text": char},
511
- )
512
- self._send_session(
513
- "Input.dispatchKeyEvent",
514
- {"type": "keyUp", "text": char},
515
- )
516
- time.sleep(random.uniform(0.03, 0.08))
517
-
518
- def get_element_text(self, selector: str) -> str | None:
519
- """获取元素文本内容。"""
520
- return self.evaluate(
521
- f"""
522
- (() => {{
523
- const el = document.querySelector({json.dumps(selector)});
524
- return el ? el.textContent : null;
525
- }})()
526
- """
527
- )
528
-
529
- def get_element_attribute(self, selector: str, attr: str) -> str | None:
530
- """获取元素属性值。"""
531
- return self.evaluate(
532
- f"""
533
- (() => {{
534
- const el = document.querySelector({json.dumps(selector)});
535
- return el ? el.getAttribute({json.dumps(attr)}) : null;
536
- }})()
537
- """
538
- )
539
-
540
- def get_elements_count(self, selector: str) -> int:
541
- """获取匹配元素数量。"""
542
- result = self.evaluate(f"document.querySelectorAll({json.dumps(selector)}).length")
543
- return result if isinstance(result, int) else 0
544
-
545
- def scroll_by(self, x: int, y: int) -> None:
546
- """滚动页面。"""
547
- self.evaluate(f"window.scrollBy({x}, {y})")
548
-
549
- def scroll_to(self, x: int, y: int) -> None:
550
- """滚动到指定位置。"""
551
- self.evaluate(f"window.scrollTo({x}, {y})")
552
-
553
- def scroll_to_bottom(self) -> None:
554
- """滚动到页面底部。"""
555
- self.evaluate("window.scrollTo(0, document.body.scrollHeight)")
556
-
557
- def scroll_element_into_view(self, selector: str) -> None:
558
- """将元素滚动到可视区域。"""
559
- self.evaluate(
560
- f"""
561
- (() => {{
562
- const el = document.querySelector({json.dumps(selector)});
563
- if (el) el.scrollIntoView({{behavior: 'smooth', block: 'center'}});
564
- }})()
565
- """
566
- )
567
-
568
- def scroll_nth_element_into_view(self, selector: str, index: int) -> None:
569
- """将第 N 个匹配元素滚动到可视区域。"""
570
- self.evaluate(
571
- f"""
572
- (() => {{
573
- const els = document.querySelectorAll({json.dumps(selector)});
574
- if (els[{index}]) els[{index}].scrollIntoView(
575
- {{behavior: 'smooth', block: 'center'}}
576
- );
577
- }})()
578
- """
579
- )
580
-
581
- def get_scroll_top(self) -> int:
582
- """获取当前滚动位置。"""
583
- result = self.evaluate(
584
- "window.pageYOffset || document.documentElement.scrollTop"
585
- " || document.body.scrollTop || 0"
586
- )
587
- return int(result) if result else 0
588
-
589
- def get_viewport_height(self) -> int:
590
- """获取视口高度。"""
591
- result = self.evaluate("window.innerHeight")
592
- return int(result) if result else 768
593
-
594
- def set_file_input(self, selector: str, files: list[str]) -> None:
595
- """设置文件输入框的文件(通过 CDP DOM.setFileInputFiles)。"""
596
- # 先获取 nodeId
597
- doc = self._send_session("DOM.getDocument", {"depth": 0})
598
- root_node_id = doc["root"]["nodeId"]
599
- result = self._send_session(
600
- "DOM.querySelector",
601
- {"nodeId": root_node_id, "selector": selector},
602
- )
603
- node_id = result.get("nodeId", 0)
604
- if node_id == 0:
605
- raise ElementNotFoundError(selector)
606
- self._send_session(
607
- "DOM.setFileInputFiles",
608
- {"nodeId": node_id, "files": files},
609
- )
610
-
611
- def dispatch_wheel_event(self, delta_y: float) -> None:
612
- """触发滚轮事件以激活懒加载。"""
613
- self.evaluate(
614
- f"""
615
- (() => {{
616
- let target = document.querySelector('.note-scroller')
617
- || document.querySelector('.interaction-container')
618
- || document.documentElement;
619
- const event = new WheelEvent('wheel', {{
620
- deltaY: {delta_y},
621
- deltaMode: 0,
622
- bubbles: true,
623
- cancelable: true,
624
- view: window,
625
- }});
626
- target.dispatchEvent(event);
627
- }})()
628
- """
629
- )
630
-
631
- def mouse_move(self, x: float, y: float) -> None:
632
- """移动鼠标。"""
633
- self._send_session(
634
- "Input.dispatchMouseEvent",
635
- {"type": "mouseMoved", "x": x, "y": y},
636
- )
637
-
638
- def mouse_click(self, x: float, y: float, button: str = "left") -> None:
639
- """在指定坐标点击。"""
640
- self._send_session(
641
- "Input.dispatchMouseEvent",
642
- {"type": "mousePressed", "x": x, "y": y, "button": button, "clickCount": 1},
643
- )
644
- self._send_session(
645
- "Input.dispatchMouseEvent",
646
- {"type": "mouseReleased", "x": x, "y": y, "button": button, "clickCount": 1},
647
- )
648
-
649
- def type_text(self, text: str, delay_ms: int = 50) -> None:
650
- """逐字符输入文本。"""
651
- for char in text:
652
- self._send_session(
653
- "Input.dispatchKeyEvent",
654
- {"type": "keyDown", "text": char},
655
- )
656
- self._send_session(
657
- "Input.dispatchKeyEvent",
658
- {"type": "keyUp", "text": char},
659
- )
660
- if delay_ms > 0:
661
- time.sleep(delay_ms / 1000.0)
662
-
663
- def press_key(self, key: str) -> None:
664
- """按下并释放指定键。"""
665
- key_map = {
666
- "Enter": {"key": "Enter", "code": "Enter", "windowsVirtualKeyCode": 13},
667
- "ArrowDown": {
668
- "key": "ArrowDown",
669
- "code": "ArrowDown",
670
- "windowsVirtualKeyCode": 40,
671
- },
672
- "Tab": {"key": "Tab", "code": "Tab", "windowsVirtualKeyCode": 9},
673
- }
674
- info = key_map.get(key, {"key": key, "code": key})
675
- self._send_session(
676
- "Input.dispatchKeyEvent",
677
- {"type": "keyDown", **info},
678
- )
679
- self._send_session(
680
- "Input.dispatchKeyEvent",
681
- {"type": "keyUp", **info},
682
- )
683
-
684
- def inject_stealth(self) -> None:
685
- """注入反检测脚本。"""
686
- self._send_session(
687
- "Page.addScriptToEvaluateOnNewDocument",
688
- {"source": STEALTH_JS},
689
- )
690
-
691
- def remove_element(self, selector: str) -> None:
692
- """移除 DOM 元素。"""
693
- self.evaluate(
694
- f"""
695
- (() => {{
696
- const el = document.querySelector({json.dumps(selector)});
697
- if (el) el.remove();
698
- }})()
699
- """
700
- )
701
-
702
- def hover_element(self, selector: str) -> None:
703
- """悬停到元素中心。"""
704
- box = self.evaluate(
705
- f"""
706
- (() => {{
707
- const el = document.querySelector({json.dumps(selector)});
708
- if (!el) return null;
709
- const rect = el.getBoundingClientRect();
710
- return {{x: rect.left + rect.width / 2, y: rect.top + rect.height / 2}};
711
- }})()
712
- """
713
- )
714
- if box:
715
- self.mouse_move(box["x"], box["y"])
716
-
717
- def select_all_text(self, selector: str) -> None:
718
- """选中输入框内所有文本。"""
719
- self.evaluate(
720
- f"""
721
- (() => {{
722
- const el = document.querySelector({json.dumps(selector)});
723
- if (!el) return;
724
- el.focus();
725
- el.select ? el.select() : document.execCommand('selectAll');
726
- }})()
727
- """
728
- )
729
-
730
- def screenshot_element(self, selector: str, padding: int = 0) -> bytes:
731
- """对指定 CSS 选择器的元素截图,返回 PNG 字节。
732
-
733
- 通过 CDP Page.captureScreenshot 截取元素所在区域,比 Python 层 PNG
734
- 解码/重编码快很多,且图片直接来自浏览器渲染结果。
735
-
736
- Args:
737
- selector: CSS 选择器。
738
- padding: 在元素四周额外保留的像素数(背景色填充,相当于白边)。
739
-
740
- Returns:
741
- PNG 字节;元素不存在时返回 b""。
742
- """
743
- import base64 as _b64
744
-
745
- # 用 DOM.getBoxModel 获取元素坐标,返回的是 page 坐标系(CSS px,相对于文档左上角)。
746
- # getBoundingClientRect 返回的是 viewport 坐标系,对 position:fixed 遮罩层内的元素
747
- # 加 pageXOffset 后依然会截到遮罩背后的内容。DOM.getBoxModel 则始终正确。
748
- try:
749
- doc = self._send_session("DOM.getDocument", {"depth": 0})
750
- root_id = doc["root"]["nodeId"]
751
- query = self._send_session("DOM.querySelector", {"nodeId": root_id, "selector": selector})
752
- node_id = query.get("nodeId", 0)
753
- if not node_id:
754
- return b""
755
- box_model = self._send_session("DOM.getBoxModel", {"nodeId": node_id})
756
- model = box_model["model"]
757
- content = model["content"] # [x1,y1, x2,y2, x3,y3, x4,y4] 顺时针四角
758
- x, y = content[0], content[1]
759
- width, height = float(model["width"]), float(model["height"])
760
- except Exception:
761
- return b""
762
-
763
- result = self._send_session(
764
- "Page.captureScreenshot",
765
- {
766
- "format": "png",
767
- "clip": {
768
- "x": max(0.0, x - padding),
769
- "y": max(0.0, y - padding),
770
- "width": width + padding * 2,
771
- "height": height + padding * 2,
772
- "scale": 1.0,
773
- },
774
- },
775
- )
776
- return _b64.b64decode(result.get("data", ""))
777
-
778
-
779
- class Browser:
780
- """Chrome 浏览器 CDP 控制器。"""
781
-
782
- def __init__(self, transport: PipeTransport) -> None:
783
- self._cdp: CDPClient | None = CDPClient(transport)
784
- self._chrome_version: str | None = None
785
-
786
- def connect(self) -> None:
787
- """连接到 Chrome DevTools。"""
788
- if not self._cdp:
789
- raise CDPError("Chrome CDP pipe is closed")
790
- info = self._cdp.send("Browser.getVersion")
791
-
792
- # 从 "Chrome/134.0.6998.88" 提取真实版本号,用于动态构建 UA
793
- browser_str = info.get("Browser", "")
794
- if "/" in browser_str:
795
- self._chrome_version = browser_str.split("/", 1)[1]
796
-
797
- logger.info("连接到 Chrome CDP pipe (version=%s)", self._chrome_version)
798
-
799
- def _targets(self) -> list[dict[str, Any]]:
800
- if not self._cdp:
801
- raise CDPError("Chrome CDP pipe is closed")
802
- return self._cdp.send("Target.getTargets").get("targetInfos", [])
803
-
804
- def _setup_page(self, page: Page) -> Page:
805
- """为 Page 对象注入 stealth、UA、viewport,并启用必要的 CDP domain。"""
806
- import contextlib
807
-
808
- page.inject_stealth()
809
- page._send_session(
810
- "Emulation.setUserAgentOverride",
811
- build_ua_override(self._chrome_version),
812
- )
813
- page._send_session(
814
- "Emulation.setDeviceMetricsOverride",
815
- {
816
- "width": random.randint(1366, 1920),
817
- "height": random.randint(768, 1080),
818
- "deviceScaleFactor": 1,
819
- "mobile": False,
820
- },
821
- )
822
- for perm in ("geolocation", "notifications", "midi", "camera", "microphone"):
823
- with contextlib.suppress(CDPError):
824
- assert self._cdp is not None
825
- self._cdp.send(
826
- "Browser.setPermission",
827
- {"permission": {"name": perm}, "setting": "denied"},
828
- )
829
- page._send_session("Page.enable")
830
- page._send_session("DOM.enable")
831
- page._send_session("Runtime.enable")
832
- return page
833
-
834
- def new_page(self, url: str = "about:blank") -> Page:
835
- """创建新页面(强制开新 tab)。"""
836
- if not self._cdp:
837
- self.connect()
838
- assert self._cdp is not None
839
-
840
- result = self._cdp.send("Target.createTarget", {"url": url})
841
- target_id = result["targetId"]
842
- result = self._cdp.send(
843
- "Target.attachToTarget",
844
- {"targetId": target_id, "flatten": True},
845
- )
846
- session_id = result["sessionId"]
847
- return self._setup_page(Page(self._cdp, target_id, session_id))
848
-
849
- def get_or_create_page(self) -> Page:
850
- """复用现有空白 tab,找不到时才新建。
851
-
852
- 避免每次命令都创建新 tab 导致 Chrome 中 tab 无限堆积。
853
- 空白 tab 判定:url 为 about:blank 或 chrome://newtab/。
854
- """
855
- if not self._cdp:
856
- self.connect()
857
- assert self._cdp is not None
858
-
859
- import contextlib
860
-
861
- targets = self._targets()
862
-
863
- for target in targets:
864
- if target.get("type") == "page" and target.get("url") in (
865
- "about:blank",
866
- "chrome://newtab/",
867
- ):
868
- target_id = target["targetId"]
869
- with contextlib.suppress(Exception):
870
- result = self._cdp.send(
871
- "Target.attachToTarget",
872
- {"targetId": target_id, "flatten": True},
873
- )
874
- session_id = result.get("sessionId")
875
- if session_id:
876
- logger.debug("复用空白 tab: %s", target_id)
877
- return self._setup_page(Page(self._cdp, target_id, session_id))
878
-
879
- # 没有空白 tab,新建一个
880
- return self.new_page()
881
-
882
- def get_page_by_target_id(self, target_id: str) -> Page | None:
883
- """通过 target_id 精确连接到指定 tab。"""
884
- if not self._cdp:
885
- self.connect()
886
- assert self._cdp is not None
887
- try:
888
- result = self._cdp.send(
889
- "Target.attachToTarget",
890
- {"targetId": target_id, "flatten": True},
891
- )
892
- except Exception:
893
- return None
894
- session_id = result.get("sessionId")
895
- if not session_id:
896
- return None
897
- page = Page(self._cdp, target_id, session_id)
898
- page._send_session("Page.enable")
899
- page._send_session("DOM.enable")
900
- page._send_session("Runtime.enable")
901
- page.inject_stealth()
902
- return page
903
-
904
- def get_existing_page(self) -> Page | None:
905
- """获取已有页面(取第一个非 about:blank 的 page target)。"""
906
- if not self._cdp:
907
- self.connect()
908
- assert self._cdp is not None
909
-
910
- targets = self._targets()
911
-
912
- for target in targets:
913
- if target.get("type") == "page" and target.get("url") != "about:blank":
914
- target_id = target["targetId"]
915
- result = self._cdp.send(
916
- "Target.attachToTarget",
917
- {"targetId": target_id, "flatten": True},
918
- )
919
- session_id = result["sessionId"]
920
- page = Page(self._cdp, target_id, session_id)
921
- page._send_session("Page.enable")
922
- page._send_session("DOM.enable")
923
- page._send_session("Runtime.enable")
924
- page.inject_stealth()
925
- return page
926
- return None
927
-
928
- def close_page(self, page: Page) -> None:
929
- """关闭页面。"""
930
- import contextlib
931
-
932
- if self._cdp:
933
- with contextlib.suppress(CDPError):
934
- self._cdp.send("Target.closeTarget", {"targetId": page.target_id})
935
-
936
- def close(self) -> None:
937
- """关闭连接。"""
938
- if self._cdp:
939
- self._cdp.close()
940
- self._cdp = None