Files
jianli-boss/client/debug_locate_live_check.py
T

90 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""只读实测:验证 _LIST_JS / _LOCATE_ITEM_JS / _CONV_NAME_JS 在真实页面的行为
绝不点击、绝不输入、绝不发送"""
import json
import re
from pathlib import Path
import requests
import websocket
CDP = "http://127.0.0.1:9222"
src = (Path(__file__).resolve().parent / "browser" / "activator.py").read_text(encoding="utf-8")
def extract(name):
return re.search(rf'{name} = """(.*?)"""', src, re.S).group(1)
LIST_JS = extract("_LIST_JS")
LOCATE_JS = extract("_LOCATE_ITEM_JS")
CONV_JS = extract("_CONV_NAME_JS")
_msg_id = 0
def call(ws, method, params=None):
global _msg_id
_msg_id += 1
ws.send(json.dumps({"id": _msg_id, "method": method, "params": params or {}}))
while True:
m = json.loads(ws.recv())
if m.get("id") == _msg_id:
return m.get("result") or {}
def ev(ws, expr):
return call(ws, "Runtime.evaluate",
{"expression": expr, "returnByValue": True}) \
.get("result", {}).get("value")
def main():
tab = next((t for t in requests.get(f"{CDP}/json", timeout=5).json()
if "/web/geek/chat" in (t.get("url") or "")
and t.get("webSocketDebuggerUrl")), None)
if not tab:
print("FAIL 未找到聊天页标签")
return
ws = websocket.create_connection(tab["webSocketDebuggerUrl"], timeout=30)
fails = []
try:
# 1) 列表可读(returnByValue 时可能直接是 list)
raw_list = ev(ws, LIST_JS)
items = raw_list if isinstance(raw_list, list) else json.loads(raw_list or "[]")
visible = [it for it in items if it.get("visible")]
names = [it.get("name") for it in visible if it.get("name")]
print(f"列表可见联系人 {len(visible)} 个:{names[:8]}")
if not names:
fails.append("列表为空")
# 2) 当前会话头部姓名可读
cur = ev(ws, CONV_JS)
print(f"当前会话头部姓名:{cur!r}")
if not cur:
fails.append("会话头部姓名为空(选择器可能失配!)")
# 3) 按真实存在的姓名重定位 → 必须返回坐标
target = cur if cur in names else (names[0] if names else None)
if target:
raw = ev(ws, f"({LOCATE_JS})({json.dumps(target)})")
print(f"重定位[{target}]:{raw}")
if not raw:
# 当前会话的人可能不在可见列表(被过滤等),用列表第一人再试
raw2 = ev(ws, f"({LOCATE_JS})({json.dumps(names[0])})")
print(f"重定位[{names[0]}]:{raw2}")
if not raw2:
fails.append("重定位失败")
else:
p = json.loads(raw)
if not (p["x"] > 0 and p["y"] > 0):
fails.append("重定位坐标非正")
# 4) 不存在的姓名 → 必须返回 null(防点错人的前提)
raw3 = ev(ws, f"({LOCATE_JS})({json.dumps('不存在的人Zz9')})")
print(f"重定位[不存在的人]:{raw3}")
if raw3 not in (None, "", "null"):
fails.append("幽灵姓名竟返回了坐标!")
finally:
ws.close()
print()
print("全部通过" if not fails else f"失败:{fails}")
if __name__ == "__main__":
main()