158 lines
7.2 KiB
JavaScript
158 lines
7.2 KiB
JavaScript
#!/usr/bin/env node
|
|
// Steuert die Client-Oberfläche über das Debug-Port - ohne Koordinaten.
|
|
//
|
|
// node cdp.mjs --text # sichtbare Texte auflisten
|
|
// node cdp.mjs --click "Join" # Element über seinen Text klicken
|
|
// node cdp.mjs --eval "<js>" # beliebigen Ausdruck auswerten
|
|
//
|
|
// Die Seite wird über ihren Zieldaten ermittelt; geklickt wird der kleinste
|
|
// passende Knoten, damit nicht ein umschliessender Container getroffen wird.
|
|
|
|
const PORT = process.env.CDP_PORT || 9333;
|
|
const args = process.argv.slice(2);
|
|
|
|
function flag(name) {
|
|
const i = args.indexOf(name);
|
|
return i < 0 ? null : (args[i + 1] ?? true);
|
|
}
|
|
|
|
async function seite() {
|
|
const liste = await (await fetch(`http://127.0.0.1:${PORT}/json/list`)).json();
|
|
return liste.find((t) => t.type === "page" && (t.url || "").includes("lobby"))
|
|
|| liste.find((t) => t.type === "page");
|
|
}
|
|
|
|
const SICHTBAR = `n.offsetParent !== null && (n.innerText || "").trim().length > 0`;
|
|
|
|
const KLICK = (text) => `(() => {
|
|
const wollen = ${JSON.stringify(text)}.trim().toLowerCase();
|
|
const alle = [...document.querySelectorAll('button, a, [role="button"], span, div, li')]
|
|
.filter((n) => ${SICHTBAR});
|
|
const genau = alle.filter((n) => (n.innerText || "").trim().toLowerCase() === wollen)
|
|
.sort((a, b) => {
|
|
const ra = a.getBoundingClientRect(), rb = b.getBoundingClientRect();
|
|
return ra.width * ra.height - rb.width * rb.height;
|
|
});
|
|
const teil = alle.filter((n) => (n.innerText || "").trim().toLowerCase().startsWith(wollen))
|
|
.sort((a, b) => {
|
|
const ra = a.getBoundingClientRect(), rb = b.getBoundingClientRect();
|
|
return ra.width * ra.height - rb.width * rb.height;
|
|
});
|
|
const ziel = genau[0] || teil[0];
|
|
if (!ziel) return "nichts gefunden fuer: " + wollen;
|
|
const r = ziel.getBoundingClientRect();
|
|
const opt = { bubbles: true, cancelable: true, composed: true, button: 0,
|
|
clientX: r.x + r.width / 2, clientY: r.y + r.height / 2 };
|
|
for (const art of ["pointerdown", "mousedown", "pointerup", "mouseup", "click"]) {
|
|
const ev = art.startsWith("pointer") ? new PointerEvent(art, opt) : new MouseEvent(art, opt);
|
|
ziel.dispatchEvent(ev);
|
|
}
|
|
return "geklickt: " + (ziel.innerText || "").trim().replace(/\\s+/g, " ").slice(0, 50)
|
|
+ " [" + Math.round(r.width) + "x" + Math.round(r.height) + "]";
|
|
})()`;
|
|
|
|
const TEXTE = `(() => {
|
|
const knoten = [...document.querySelectorAll('button, a, [role="button"], span, div, th, td')]
|
|
.filter((n) => ${SICHTBAR} && n.children.length === 0);
|
|
const gesehen = new Set(); const aus = [];
|
|
for (const n of knoten) {
|
|
const t = (n.innerText || "").trim().replace(/\\s+/g, " ");
|
|
if (!t || t.length > 40 || gesehen.has(t)) continue;
|
|
gesehen.add(t); aus.push(t);
|
|
}
|
|
return aus.join(" | ");
|
|
})()`;
|
|
|
|
// Wie KLICK, aber der n-te Treffer (0-basiert, in Reihenfolge der Seite).
|
|
const KLICK_NTH = (text, n) => `(() => {
|
|
const wollen = ${JSON.stringify(text)}.trim().toLowerCase();
|
|
const treffer = [...document.querySelectorAll('button, a, [role="button"], span, div')]
|
|
.filter((k) => k.offsetParent !== null &&
|
|
(k.innerText || "").trim().toLowerCase() === wollen);
|
|
if (!treffer.length) return "nichts gefunden: " + wollen;
|
|
const ziel = treffer[Math.min(${Number(n) || 0}, treffer.length - 1)];
|
|
const r = ziel.getBoundingClientRect();
|
|
const opt = { bubbles: true, cancelable: true, composed: true, button: 0,
|
|
clientX: r.x + r.width / 2, clientY: r.y + r.height / 2 };
|
|
for (const art of ["pointerdown", "mousedown", "pointerup", "mouseup", "click"]) {
|
|
const ev = art.startsWith("pointer") ? new PointerEvent(art, opt) : new MouseEvent(art, opt);
|
|
ziel.dispatchEvent(ev);
|
|
}
|
|
const zeile = (ziel.closest("tr, li, [class*=row], [class*=Row]") || ziel).innerText || "";
|
|
return "geklickt #" + ${Number(n) || 0} + " von " + treffer.length + ": "
|
|
+ zeile.replace(/\\s+/g, " ").slice(0, 60);
|
|
})()`;
|
|
|
|
// Klickt einen Knopf INNERHALB der Zeile, die einen Text enthält.
|
|
const KLICK_IN = (zeilenText, knopfText) => `(() => {
|
|
const zeile = ${JSON.stringify(zeilenText)}.trim().toLowerCase();
|
|
const knopf = ${JSON.stringify(knopfText)}.trim().toLowerCase();
|
|
const sichtbar = (n) => n.offsetParent !== null;
|
|
const flaeche = (n) => { const r = n.getBoundingClientRect(); return r.width * r.height; };
|
|
const treffer = [...document.querySelectorAll("*")]
|
|
.filter((n) => sichtbar(n) && (n.innerText || "").toLowerCase().includes(zeile))
|
|
.sort((a, b) => flaeche(a) - flaeche(b));
|
|
for (const z of treffer) {
|
|
const knoepfe = [...z.querySelectorAll('button, a, [role="button"], span, div')]
|
|
.filter((n) => sichtbar(n) && (n.innerText || "").trim().toLowerCase() === knopf)
|
|
.sort((a, b) => flaeche(a) - flaeche(b));
|
|
if (!knoepfe.length) continue;
|
|
const ziel = knoepfe[0];
|
|
const r = ziel.getBoundingClientRect();
|
|
const opt = { bubbles: true, cancelable: true, composed: true, button: 0,
|
|
clientX: r.x + r.width / 2, clientY: r.y + r.height / 2 };
|
|
for (const art of ["pointerdown", "mousedown", "pointerup", "mouseup", "click"]) {
|
|
const ev = art.startsWith("pointer") ? new PointerEvent(art, opt) : new MouseEvent(art, opt);
|
|
ziel.dispatchEvent(ev);
|
|
}
|
|
return "geklickt in Zeile '" + ${JSON.stringify(zeilenText)} + "': "
|
|
+ (ziel.innerText || "").trim().slice(0, 30);
|
|
}
|
|
return "kein Knopf '" + knopf + "' in der Zeile gefunden";
|
|
})()`;
|
|
|
|
async function main() {
|
|
const t = await seite();
|
|
if (!t) { console.log("keine Seite am Debug-Port"); process.exit(1); }
|
|
const ws = new WebSocket(t.webSocketDebuggerUrl);
|
|
await new Promise((ok, bad) => { ws.onopen = ok; ws.onerror = bad; });
|
|
let nr = 0; const wartend = new Map();
|
|
ws.onmessage = (e) => {
|
|
const m = JSON.parse(e.data);
|
|
if (m.id && wartend.has(m.id)) { wartend.get(m.id)(m); wartend.delete(m.id); }
|
|
};
|
|
const senden = (method, params = {}) => new Promise((res) => {
|
|
const i = ++nr; wartend.set(i, res); ws.send(JSON.stringify({ id: i, method, params }));
|
|
});
|
|
await senden("Runtime.enable");
|
|
const werten = async (expr) => {
|
|
const r = await senden("Runtime.evaluate",
|
|
{ expression: expr, returnByValue: true, awaitPromise: true });
|
|
if (r.result?.exceptionDetails) return "Fehler: " + JSON.stringify(r.result.exceptionDetails).slice(0, 300);
|
|
return r.result?.result?.value;
|
|
};
|
|
|
|
const klick = flag("--click");
|
|
if (klick) { console.log(await werten(KLICK(klick))); ws.close(); return; }
|
|
if (args.includes("--click-in")) {
|
|
const i = args.indexOf("--click-in");
|
|
console.log(await werten(KLICK_IN(args[i + 1], args[i + 2] || "Join")));
|
|
ws.close();
|
|
return;
|
|
}
|
|
if (args.includes("--click-nth")) {
|
|
const i = args.indexOf("--click-nth");
|
|
console.log(await werten(KLICK_NTH(args[i + 1], args[i + 2])));
|
|
ws.close();
|
|
return;
|
|
}
|
|
const js = args.includes("--eval") ? args[args.indexOf("--eval") + 1] : null;
|
|
if (js) { console.log(await werten(js)); ws.close(); return; }
|
|
console.log("Titel:", await werten("document.title"));
|
|
console.log("Adresse:", await werten("location.href"));
|
|
console.log("Texte:", await werten(TEXTE));
|
|
ws.close();
|
|
}
|
|
|
|
main().catch((e) => { console.error("Fehler:", e.message); process.exit(1); });
|