forked from reisxd/TizenTube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebuggerController.js
60 lines (54 loc) · 2.4 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
55
56
57
58
59
60
import WebSocket from 'ws';
import nodeFetch from 'node-fetch';
import { readFileSync } from 'node:fs';
import Config from './config.json' assert { type: 'json' };
import { log, log_error } from './utils.js';
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function startDebugging(port, adb_conn, tv_ip) {
try {
const debuggerJsonReq = await nodeFetch(`http://${tv_ip}:${port}/json`);
const debuggerJson = await debuggerJsonReq.json();
return attachDebugger(debuggerJson[0].webSocketDebuggerUrl, adb_conn);
} catch (error) {
log_error('Could not load scripts to TizenTube:', error.message);
adb_conn._stream.end();
}
}
async function attachDebugger(wsUrl, adb_conn) {
const client = await new WebSocket(wsUrl);
//We don't need the adb connection at this point
adb_conn._stream.end();
let id = 12;
let modFile;
try {
modFile = readFileSync('mods/dist/userScript.js', 'utf-8');
} catch {
log_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' && msg.params.context.origin=="https://www.youtube.com") {
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++;
log("Injected scripts to TizenTube successfully!");
}
if (Config.debug) {
if (msg.method == 'Console.messageAdded') {
log(msg.params.message.text, msg.params.message.parameters);
} else if (msg?.result?.result?.wasThrown) {
log_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;