forked from reisxd/TizenTube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debuggerController.js
54 lines (48 loc) · 2.12 KB
/
debuggerController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import WebSocket from 'ws';
import nodeFetch from 'node-fetch';
import { readFileSync } from 'node:fs';
import Config from './config.json' assert { type: 'json' };
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function startDebugging(port) {
// Sleep to get the app to load.
// For some reason, without it, using the launcher gives an error
await sleep(5000)
const debuggerJsonReq = await nodeFetch(`http://${Config.tvIP}:${port}/json`);
const debuggerJson = await debuggerJsonReq.json();
return attachDebugger(debuggerJson[0].webSocketDebuggerUrl);
}
async function attachDebugger(wsUrl) {
const client = await new WebSocket(wsUrl);
let id = 12;
let modFile;
try {
modFile = readFileSync('mods/dist/userScript.js', 'utf-8');
} catch {
console.error('Could not find the built mod file. Did you build it?');
client.close();
return;
}
client.onmessage = (message) => {
const msg = JSON.parse(message.data);
// Future-proof it just incase the page reloads/something happens.
if (msg.method && msg.method == 'Runtime.executionContextCreated') {
client.send(JSON.stringify({ "id": id, "method": "Runtime.evaluate", "params": { "expression": modFile, "objectGroup": "console", "includeCommandLineAPI": true, "doNotPauseOnExceptionsAndMuteConsole": false, "contextId": msg.params.context.id, "returnByValue": false, "generatePreview": true } }))
id++;
}
if (Config.debug) {
if (msg.method == 'Console.messageAdded') {
console.log(msg.params.message.text, msg.params.message.parameters);
} else if (msg?.result?.result?.wasThrown) {
console.error(msg.result.result.description);
}
}
}
client.onopen = () => {
if (Config.debug) {
client.send(JSON.stringify({ "id": 2, "method": "Console.enable" }));
}
client.send(JSON.stringify({ "id": 7, "method": "Debugger.enable" }));
client.send(JSON.stringify({ "id": 11, "method": "Runtime.enable" }));
}
}
export default startDebugging;