Files
jianli-boss/client/debug_chat_probe3.py
T

115 lines
4.1 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 -*-
"""
第三轮实测:以可见的 button.btn-send 为锚点,探测输入区真实结构。
只读 DOM,绝不输入与发送。
前提:浏览器已开聊天页(/web/geek/chat)且左侧已点开任意一个会话。
用法:python -m client.debug_chat_probe3
产物:client/data/debug/chat_conv_probe3.txt
"""
import json
import time
from pathlib import Path
import requests
import websocket
CDP = "http://127.0.0.1:9222"
OUT = Path(__file__).resolve().parent / "data" / "debug" / "chat_conv_probe3.txt"
_msg_id = 0
def send(ws, method, params=None):
global _msg_id
_msg_id += 1
ws.send(json.dumps({"id": _msg_id, "method": method, "params": params or {}}))
while True:
msg = json.loads(ws.recv())
if msg.get("id") == _msg_id:
return msg.get("result") or {}
def evaluate(ws, expression):
return send(ws, "Runtime.evaluate",
{"expression": expression, "returnByValue": True}) \
.get("result", {}).get("value")
PROBE_JS = """(() => {
const fs = [];
// 1) 以发送按钮为锚点
const btn = document.querySelector('button.btn-send');
if (!btn) { fs.push('无 button.btn-send'); }
else {
const r = btn.getBoundingClientRect();
fs.push('== btn-send 可见=' + (btn.offsetHeight > 0)
+ ' rect=(' + Math.round(r.x) + ',' + Math.round(r.y) + ','
+ Math.round(r.width) + 'x' + Math.round(r.height) + ') disabled=' + btn.disabled);
// 向上找容器并 dump 结构
let wrap = btn.parentElement;
for (let i = 0; i < 3 && wrap; i++) wrap = wrap.parentElement;
fs.push('== btn 上溯3层容器 class=' + (wrap ? String(wrap.className).slice(0, 120) : 'null'));
if (wrap) fs.push('HTML: ' + wrap.outerHTML.replace(/\\s+/g, ' ').slice(0, 3000));
}
// 2) 全页可编辑元素
const ces = [...document.querySelectorAll('[contenteditable=""],[contenteditable="true"]')]
.filter(e => e.offsetHeight > 0);
fs.push('== 可见 contenteditable:' + ces.length + ' ==');
ces.forEach((e, i) => {
const r = e.getBoundingClientRect();
fs.push('--- ce' + i + ' <' + e.tagName + ' class=' + String(e.className).slice(0, 80)
+ '> rect=(' + Math.round(r.x) + ',' + Math.round(r.y) + ','
+ Math.round(r.width) + 'x' + Math.round(r.height) + ')'
+ ' text=' + (e.innerText || '').slice(0, 40).replace(/\\n/g, ' '));
});
// 3) 全页 input/textarea(含隐藏,附 display/visibility 样式)
const eds = [...document.querySelectorAll('textarea,input')];
fs.push('== input/textarea 总数:' + eds.length + ' ==');
eds.forEach((e, i) => {
const cs = getComputedStyle(e);
const r = e.getBoundingClientRect();
fs.push('--- ed' + i + ' <' + e.tagName + ' class=' + String(e.className).slice(0, 60)
+ ' ph=' + (e.placeholder || '').slice(0, 30)
+ '> display=' + cs.display + ' visibility=' + cs.visibility
+ ' rect=(' + Math.round(r.x) + ',' + Math.round(r.y) + ','
+ Math.round(r.width) + 'x' + Math.round(r.height) + ')');
});
// 4) iframe 里是否藏着输入框
const ifs = [...document.querySelectorAll('iframe')];
fs.push('== iframe:' + ifs.length + ' ==');
ifs.forEach((f, i) => fs.push('--- if' + i + ' src=' + (f.src || '').slice(0, 80)));
return fs.join('\\n');
})()"""
def main():
OUT.parent.mkdir(parents=True, exist_ok=True)
lines = []
def log(*a):
s = " ".join(str(x) for x in a)
lines.append(s)
OUT.write_text("\n".join(lines), encoding="utf-8")
tab = None
for t in requests.get(f"{CDP}/json", timeout=5).json():
if "/web/geek/chat" in (t.get("url") or "") and t.get("webSocketDebuggerUrl"):
tab = t
break
if not tab:
log("== 没有聊天页标签,请先开聊天页 ==")
return
requests.get(f"{CDP}/json/activate/{tab['id']}", timeout=5)
time.sleep(1)
ws = websocket.create_connection(tab["webSocketDebuggerUrl"], timeout=30)
try:
log(evaluate(ws, PROBE_JS) or "探测为空")
finally:
ws.close()
log("== DONE ==")
if __name__ == "__main__":
main()