# -*- coding: utf-8 -*- """实测:视口外联系人 定位→滚入视口→重定位 全流程 唯一副作用:会滚动左侧列表位置(无点击、无输入、无发送)""" 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") BRING_JS = extract("_BRING_INTO_VIEW_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") VIEWPORT_JS = "(() => window.innerHeight)()" SCROLL_INFO_JS = """(() => { const el = document.querySelector('.user-list-content') || document.querySelector('.user-list'); if (!el) return null; return {clientHeight: el.clientHeight, scrollTop: el.scrollTop, scrollHeight: el.scrollHeight}; })()""" 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: viewport = ev(ws, VIEWPORT_JS) print(f"当前视口高度(动态):{viewport}") sc = ev(ws, SCROLL_INFO_JS) print(f"滚动容器:{sc}") # 滚动步长动态性验证:新步长 = max(240, clientHeight*0.8),不再固定 480 if sc: expected = max(240, round(sc["clientHeight"] * 0.8)) print(f"预期翻页步长(动态计算):{expected}") raw = ev(ws, LIST_JS) items = raw if isinstance(raw, list) else json.loads(raw or "[]") names = [it.get("name") for it in items if it.get("visible") and it.get("name")] first, last = names[0], names[-1] print(f"列表 {len(names)} 人,首位={first},末位={last}") # 1) 首位联系人:应在视口内 loc1 = json.loads(ev(ws, f"({LOCATE_JS})({json.dumps(first)})")) print(f"定位[{first}]:{loc1}") if not (loc1 and loc1.get("inView") is True): fails.append("首位应在视口内") # 2) 末位联系人:应判为视口外(探针实测 y=3209 > 视口 825) loc2 = json.loads(ev(ws, f"({LOCATE_JS})({json.dumps(last)})")) print(f"定位[{last}]:{loc2}") if not loc2: fails.append("末位定位失败") elif loc2.get("inView") is True: print("(末位恰在视口内,跳过滚动验证)") else: # 3) 滚入视口 → 重定位应变 inView=true 且 y 在视口内 ok = ev(ws, f"({BRING_JS})({json.dumps(last)})") print(f"滚入视口[{last}]:{ok}") if ok is not True: fails.append("scrollIntoView 返回非 true") import time time.sleep(0.8) loc3 = json.loads(ev(ws, f"({LOCATE_JS})({json.dumps(last)})")) print(f"滚动后重定位[{last}]:{loc3}") if not (loc3 and loc3.get("inView") is True and 0 < loc3["y"] < viewport): fails.append("滚入视口后仍未进入可视区") # 还原:滚回首位,避免影响用户当前浏览位置 ev(ws, f"({BRING_JS})({json.dumps(first)})") # 4) 幽灵姓名 if ev(ws, f"({BRING_JS})({json.dumps('不存在的人Zz9')})") is not False: fails.append("幽灵姓名滚入视口竟返回 true") finally: ws.close() print() print("全部通过" if not fails else f"失败:{fails}") if __name__ == "__main__": main()