| 54 | #done; |
| 55 | #exit; |
| 56 | constructor(url) { |
| 57 | this.websocket = new WebSocket(url); |
| 58 | this.#id = 1; |
| 59 | this.#resp = new Map(); |
| 60 | this.#exit = (status) => { }; |
| 61 | this.#done = new Promise((resolve) => { |
| 62 | this.#exit = resolve; |
| 63 | }); |
| 64 | this.#target = ""; |
| 65 | this.#session = ""; |
| 66 | const readyOpen = new Promise((resolve, reject) => { |
| 67 | this.websocket.onopen = async () => { resolve(); }; |
| 68 | }); |
| 69 | const readyPage = (async () => { |
| 70 | await readyOpen; |
| 71 | const target = await this._send("Target.getTargets", {}); |
| 72 | if (target.targetInfos.length) { |
| 73 | this.#target = target.targetInfos[0].targetId; |
| 74 | } |
| 75 | else { |
| 76 | const target = await this._send("Target.createTarget", { url: "" }); |
| 77 | this.#target = target.targetId; |
| 78 | } |
| 79 | const attached = await this._send("Target.attachToTarget", { |
| 80 | targetId: this.#target, |
| 81 | flatten: true |
| 82 | }); |
| 83 | this.#session = attached.sessionId; |
| 84 | })(); |
| 85 | this.#readyOpen = readyOpen; |
| 86 | this.#readyPage = readyPage; |
| 87 | this.websocket.onmessage = (_msg) => { |
| 88 | const msg = JSON.parse(_msg.data); |
| 89 | if (msg.id != null) { |
| 90 | const responder = this.#resp.get(msg.id); |
| 91 | this.#resp.delete(msg.id); |
| 92 | if (responder == null) { |
| 93 | console.log("WARN: unknown request ${ msg.id }"); |
| 94 | return; |
| 95 | } |
| 96 | if (msg.error) { |
| 97 | console.log(`ERROR: ${msg.error}`); |
| 98 | responder.reject(new Error(msg.error)); |
| 99 | } |
| 100 | else { |
| 101 | responder.resolve(msg.result); |
| 102 | } |
| 103 | return; |
| 104 | } |
| 105 | switch (msg.method) { |
| 106 | case "Console.messageAdded": { |
| 107 | const text = msg.params.message.text; |
| 108 | if (text.startsWith("#status")) { |
| 109 | this.#exit(parseInt(text.split("=").pop())); |
| 110 | } |
| 111 | console.log(text); |
| 112 | //console.log(msg.params.message.text, `${ msg.params.message.url }:${ msg.params.message.line }`); |
| 113 | break; |