56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
/*
|
|
* Veskslop development tool — run one expression in the real client over CDP.
|
|
*
|
|
* Use it with the client started on --remote-debugging-port=9222. The main use
|
|
* is to dispatch synthetic events for end-to-end tests.
|
|
*
|
|
* Usage:
|
|
* CDP_EXPRESSION='Vencord.Webpack.Common.FluxDispatcher.dispatch({...})' pnpm cdp:dispatch
|
|
*/
|
|
|
|
import WebSocket from "ws";
|
|
|
|
const expression = process.env.CDP_EXPRESSION;
|
|
if (!expression) {
|
|
console.error("CDP_EXPRESSION is required");
|
|
process.exit(1);
|
|
}
|
|
|
|
const cdpUrl = process.env.CDP_URL ?? "http://127.0.0.1:9222";
|
|
const targets = (await (await fetch(`${cdpUrl}/json`)).json()) as any[];
|
|
const page = targets.find(target => target.type === "page" && target.url.includes("discord.com"));
|
|
|
|
if (!page) {
|
|
console.error("No Discord page target");
|
|
process.exit(1);
|
|
}
|
|
|
|
const ws = new WebSocket(page.webSocketDebuggerUrl);
|
|
await new Promise<void>((resolve, reject) => {
|
|
ws.once("open", () => resolve());
|
|
ws.once("error", reject);
|
|
});
|
|
|
|
const response = await new Promise<any>((resolve, reject) => {
|
|
const timer = setTimeout(() => reject(new Error("CDP evaluate timed out")), 10_000);
|
|
|
|
ws.on("message", raw => {
|
|
const message = JSON.parse(raw.toString());
|
|
if (message.id !== 1) return;
|
|
|
|
clearTimeout(timer);
|
|
resolve(message);
|
|
});
|
|
|
|
ws.send(
|
|
JSON.stringify({
|
|
id: 1,
|
|
method: "Runtime.evaluate",
|
|
params: { expression, returnByValue: true, awaitPromise: true }
|
|
})
|
|
);
|
|
});
|
|
|
|
console.log(JSON.stringify(response.result ?? response.error));
|
|
ws.close();
|