94 lines
3.5 KiB
Python
94 lines
3.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""只读实测:找出左侧联系人列表的真正滚动容器
|
||
原则:不修改任何 scrollTop,只读取结构信息"""
|
||
import json
|
||
from pathlib import Path
|
||
|
||
import requests
|
||
import websocket
|
||
|
||
CDP = "http://127.0.0.1:9222"
|
||
OUT = Path(__file__).resolve().parent / "data" / "debug" / "chat_scroll_probe.txt"
|
||
|
||
PROBE_JS = """(() => {
|
||
const fs = [];
|
||
// 1) 从首个联系人向上找所有"可滚动"祖先(overflow 允许滚动且可滚高度>0)
|
||
const li = document.querySelector('.friend-content-warp');
|
||
if (!li) { fs.push('无列表项'); return fs.join('\\n'); }
|
||
let cur = li.parentElement;
|
||
fs.push('== 自列表项向上的祖先链(含滚动能力) ==');
|
||
let depth = 0;
|
||
while (cur && depth < 10) {
|
||
const cs = getComputedStyle(cur);
|
||
const canScroll = /(auto|scroll|overlay)/.test(cs.overflowY)
|
||
&& cur.scrollHeight > cur.clientHeight;
|
||
fs.push(' d' + depth + ' <' + cur.tagName + ' class=' + String(cur.className).slice(0, 70)
|
||
+ '> overflowY=' + cs.overflowY
|
||
+ ' scrollH=' + cur.scrollHeight + ' clientH=' + cur.clientHeight
|
||
+ ' scrollTop=' + Math.round(cur.scrollTop)
|
||
+ (canScroll ? ' ← 可滚动' : ''));
|
||
cur = cur.parentElement;
|
||
depth++;
|
||
}
|
||
// 2) 现有 _LIST_SCROLL_JS 三个候选元素的状态对比
|
||
fs.push('== 现有滚动候选元素现状 ==');
|
||
['.user-list-content', '.user-list', '.chat-content'].forEach(sel => {
|
||
const el = document.querySelector(sel);
|
||
if (!el) { fs.push(' ' + sel + ' 不存在'); return; }
|
||
const cs = getComputedStyle(el);
|
||
fs.push(' ' + sel + ' overflowY=' + cs.overflowY
|
||
+ ' scrollH=' + el.scrollHeight + ' clientH=' + el.clientHeight
|
||
+ ' children=' + el.children.length);
|
||
});
|
||
// 3) li 总数与 ul 容器
|
||
const lis = [...document.querySelectorAll('.friend-content-warp')]
|
||
.map(w => w.closest('li')).filter(Boolean);
|
||
fs.push('== li 总数:' + new Set(lis).size + ' ==');
|
||
const ul = lis[0] ? lis[0].parentElement : null;
|
||
fs.push('ul 容器:' + (ul ? '<' + ul.tagName + ' class=' + String(ul.className).slice(0, 60) + '>' : 'null'));
|
||
// 4) 最后一个 li 的视口位置(判断是否在可视区外)
|
||
const last = lis[lis.length - 1];
|
||
if (last) {
|
||
const r = last.getBoundingClientRect();
|
||
fs.push('最后li rect: y=' + Math.round(r.y) + ' h=' + Math.round(r.height)
|
||
+ ' viewportH=' + window.innerHeight);
|
||
}
|
||
return fs.join('\\n');
|
||
})()"""
|
||
|
||
_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 main():
|
||
OUT.parent.mkdir(parents=True, exist_ok=True)
|
||
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)
|
||
out = []
|
||
if not tab:
|
||
out.append("== 没有聊天页标签 ==")
|
||
else:
|
||
ws = websocket.create_connection(tab["webSocketDebuggerUrl"], timeout=30)
|
||
try:
|
||
v = call(ws, "Runtime.evaluate",
|
||
{"expression": PROBE_JS, "returnByValue": True}) \
|
||
.get("result", {}).get("value")
|
||
out.append(v or "探测为空")
|
||
finally:
|
||
ws.close()
|
||
out.append("== DONE ==")
|
||
text = "\n".join(out)
|
||
OUT.write_text(text, encoding="utf-8")
|
||
print(text)
|
||
|
||
if __name__ == "__main__":
|
||
main()
|