-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnative.ts
117 lines (99 loc) · 3.62 KB
/
native.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { BrowserWindow, dialog, WebContents } from "electron";
import { createServer, Server as HttpServer } from "http";
import { Server, Socket } from "./dependencies.dist";
let io: Server;
let httpServer: HttpServer;
let hasInit = false;
let webFrame: WebContents;
export function init() {
if (hasInit) return;
const windows = BrowserWindow.getAllWindows();
const discordUrls = ["https://discord.com", "https://ptb.discord.com", "https://canary.discord.com"];
for (const win of windows) {
const url = win.webContents.getURL();
if (discordUrls.some(prefix => url.startsWith(prefix))) {
webFrame = win.webContents;
}
}
httpServer = createServer();
io = new Server(httpServer, {
serveClient: false,
allowEIO3: true,
cors: { origin: "*" }
});
httpServer.listen(3020, () => {
console.log("[vc-premid] SocketIO starting on 3020");
logRenderer("SocketIO starting on 3020");
});
httpServer.on("error", onIOError);
io.on("connection", onConnect);
hasInit = true;
}
export function disconnect() {
if (!hasInit) return;
io.close();
httpServer.close();
hasInit = false;
}
async function onConnect(sio: Socket) {
try {
logRenderer("[vc-premid] PreMiD socket connected!");
// Get current user from plugin & send to extension
const {
username,
globalName,
id,
avatar,
discriminator,
flags,
premiumType
} = JSON.parse(await webFrame.executeJavaScript("JSON.stringify(window.Vencord.Webpack.Common.UserStore.getCurrentUser());"));
sio.emit("discordUser", { username, global_name: globalName, discriminator, id, avatar, bot: false, flags, premium_type: premiumType });
// Extension requests Premid version
sio.on("getVersion", () => {
logRenderer("Extension requested version");
sio.emit("receiveVersion", "221");
});
sio.on("setActivity", setActivity);
sio.on("clearActivity", clearActivity);
sio.on("selectLocalPresence", () => {
logRenderer("Selecting local presence is not supported");
dialog.showMessageBox({ message: "Selecting local presence is not supported right now!", title: "vc-premid: oops!" });
});
sio.once("disconnect", () => onIoDisconnect());
} catch (e) {
logError("Error in onConnect: ", e);
}
}
function logRenderer(message: string) {
if (webFrame) {
webFrame.executeJavaScript(`window.Vencord.Plugins.plugins.PreMiD.logger.info('${message}')`);
} else {
// just in case, dont worry about it pls
console.log(`[vc-premid (fallback)] ${message}`);
}
}
function logError(message: string, ...args: any[]) {
console.error(`${message}`, args);
}
function setActivity(activity: any) {
// hopefully this works
webFrame.executeJavaScript(`window.Vencord.Plugins.plugins.PreMiD.receiveActivity(${JSON.stringify(activity)})`).catch(console.error);
}
function clearActivity() {
webFrame.executeJavaScript("window.Vencord.Plugins.plugins.PreMiD.clearActivity()");
}
function onIOError(e: { message: string; code: string; }) {
if (e.message.includes("EADDRINUSE")) return; // dont care, probably 2+ clients open
logError("SocketIO error", e);
}
async function onIoDisconnect() {
console.log("[vc-premid] SocketIO disconnected");
logRenderer("SocketIO disconnected");
clearActivity();
}