新增boss激活功能

This commit is contained in:
2026-08-26 19:32:21 +08:00
parent 8d57c5dbc3
commit fae7a51cc5
11 changed files with 909 additions and 6 deletions
+130
View File
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
"""
第二轮实测:用浏览器级可信点击(Input.dispatchMouseEvent)打开左侧第一条会话,
探测右侧消息区真实结构(消息条目 / 最后一条消息 / 输入框 / 发送按钮)。
只读验证:点击联系人、读取 DOM,绝不输入与发送。
用法:python -m client.debug_chat_probe2
产物:client/data/debug/chat_conv_probe.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_probe2.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")
# 会话已打开,只定位输入框与发送按钮(不再点击)
POS_JS = """(() => {
const fs = [];
const tas = [...document.querySelectorAll('textarea')];
fs.push('== textarea 总数:' + tas.length + ' ==');
tas.forEach((ta, i) => {
const r = ta.getBoundingClientRect();
fs.push('--- ta' + i + ' class=' + ta.className + ' placeholder=' + (ta.placeholder || '').slice(0, 40));
fs.push(' 可见=' + (ta.offsetHeight > 0) + ' rect=(' + Math.round(r.x) + ',' + Math.round(r.y) + ',' + Math.round(r.width) + 'x' + Math.round(r.height) + ')');
fs.push(' 祖先链:' + (() => {
const chain = []; let cur = ta.parentElement;
for (let j = 0; j < 6 && cur; j++) { chain.push(String(cur.className).slice(0, 60)); cur = cur.parentElement; }
return chain.join(' < '); })());
});
const sends = [...document.querySelectorAll('a,button,div,span')]
.filter(e => (e.innerText || '').trim() === '发送' && e.offsetHeight > 0);
fs.push('== 发送按钮:' + sends.length + ' ==');
sends.forEach(e => {
const r = e.getBoundingClientRect();
fs.push(' <' + e.tagName + ' class=' + String(e.className).slice(0, 100) + '> disabled=' + e.disabled + ' rect=(' + Math.round(r.x + r.width / 2) + ',' + Math.round(r.y + r.height / 2) + ')');
});
return fs.join('\\n');
})()"""
# 会话区结构探测(同上一轮)
CONV_JS = """(() => {
const fs = [];
const conv = document.querySelector('.chat-conversation');
if (!conv) { fs.push('无 .chat-conversation'); return fs.join('\\n'); }
fs.push('== conversation children ==');
[...conv.children].forEach(c => fs.push(' <' + c.tagName + ' class=' + String(c.className).slice(0, 80) + '>'));
const clsCount = {};
for (const el of conv.querySelectorAll('[class]')) {
for (const c of String(el.className).split(/\\s+/)) {
if (/msg|item|message|bubble|text|self|other|my/i.test(c)) clsCount[c] = (clsCount[c] || 0) + 1;
}
}
fs.push('== 会话区高频 class(前30) ==');
Object.entries(clsCount).sort((a, b) => b[1] - a[1]).slice(0, 30)
.forEach(([k, n]) => fs.push(' .' + k + ' -> ' + n));
const items = [...conv.querySelectorAll('.chat-message .item, [class*="msg-item"], [class*="message-item"], .item-myself, .item-other')];
fs.push('== 消息条目候选数:' + items.length + ' ==');
items.slice(-3).forEach((el, i) => {
fs.push('--- msg ' + i + ' class=' + String(el.className).slice(0, 100));
fs.push(' text: ' + (el.innerText || '').replace(/\\s+/g, ' ').trim().slice(0, 150));
fs.push(' html: ' + el.outerHTML.replace(/\\s+/g, ' ').slice(0, 1500));
});
const ta = conv.querySelector('textarea') || document.querySelector('.chat-conversation textarea');
fs.push('== 输入框:' + (ta ? 'class=' + ta.className : '无') + ' ==');
if (ta) {
const wrap = ta.closest('[class*="input"]') || ta.parentElement;
fs.push('输入框外层:' + (wrap ? wrap.outerHTML.replace(/\\s+/g, ' ').slice(0, 1500) : 'null'));
}
const sends = [...document.querySelectorAll('a,button,div,span')]
.filter(e => (e.innerText || '').trim() === '发送' && e.offsetHeight > 0);
fs.push('== 发送按钮候选:' + sends.length + ' ==');
sends.slice(0, 3).forEach(e => fs.push(' <' + e.tagName + ' class=' + String(e.className).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, POS_JS) or "探测为空")
finally:
ws.close()
log("== DONE ==")
if __name__ == "__main__":
main()