muse-chat-api.py upload: ev1() skips CDP chatter on verify

This commit is contained in:
operator-main
2026-10-04 01:28:19 +00:00
parent 716b31a7e6
commit c759ca353f
+20 -2
View File
@@ -284,6 +284,24 @@ def cdp_call(ws, method, params=None):
cdp_call.counter = 100 cdp_call.counter = 100
def ev1(ws, expr, await_p=False):
"""Runtime.evaluate that skips CDP event chatter while awaiting its
response. ev() reads a single message and can catch an event
instead (the known None-result quirk); uploads do several DOM
calls first, so chatter is likely."""
ws.send(json.dumps({
"id": 1, "method": "Runtime.evaluate",
"params": {"expression": expr, "returnByValue": True,
"awaitPromise": await_p}
}))
for _ in range(30):
resp = json.loads(ws.recv())
if resp.get("id") != 1:
continue
return resp.get("result", {}).get("result", {}).get("value")
return None
def cmd_upload(ws, filepath, message=None, dry_run=False): def cmd_upload(ws, filepath, message=None, dry_run=False):
"""Attach a file to the chat composer via CDP DOM.setFileInputFiles. """Attach a file to the chat composer via CDP DOM.setFileInputFiles.
@@ -327,7 +345,7 @@ def cmd_upload(ws, filepath, message=None, dry_run=False):
# element's OWN text: React swaps the file input after change, so # element's OWN text: React swaps the file input after change, so
# input.files is unreliable, and innerText on ancestors over-matches. # input.files is unreliable, and innerText on ancestors over-matches.
base = os.path.basename(filepath) base = os.path.basename(filepath)
preview = ev(ws, """(() => { preview = ev1(ws, """(() => {
const needle = %s; const needle = %s;
const own = e => [...e.childNodes].filter(n => n.nodeType === 3) const own = e => [...e.childNodes].filter(n => n.nodeType === 3)
.map(n => n.nodeValue).join(''); .map(n => n.nodeValue).join('');
@@ -369,7 +387,7 @@ def cmd_upload(ws, filepath, message=None, dry_run=False):
})()""", True) })()""", True)
print("send: %s" % sent) print("send: %s" % sent)
time.sleep(5) time.sleep(5)
recent = ev(ws, """(() => { recent = ev1(ws, """(() => {
const ps = [...document.querySelectorAll('p')].slice(-10) const ps = [...document.querySelectorAll('p')].slice(-10)
.map(p => p.innerText.slice(0,200)); .map(p => p.innerText.slice(0,200));
return ps.join('\\n---\\n'); return ps.join('\\n---\\n');