Vencord/browser/VencordNativeStub.ts

67 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-10-22 01:17:06 +02:00
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-10-21 23:58:41 +02:00
import * as DataStore from "../src/api/DataStore";
import IpcEvents from "../src/utils/IpcEvents";
2022-10-04 00:52:42 +02:00
// Discord deletes this so need to store in variable
2022-10-21 23:58:41 +02:00
const { localStorage } = window;
// listeners for ipc.on
const listeners = {} as Record<string, Set<Function>>;
2022-10-04 00:52:42 +02:00
const handlers = {
2022-10-21 23:58:41 +02:00
[IpcEvents.GET_REPO]: () => "https://github.com/Vendicated/Vencord", // shrug
2022-10-04 00:52:42 +02:00
[IpcEvents.GET_SETTINGS_DIR]: () => "LocalStorage",
2022-10-21 23:58:41 +02:00
[IpcEvents.GET_QUICK_CSS]: () => DataStore.get("VencordQuickCss").then(s => s ?? ""),
[IpcEvents.SET_QUICK_CSS]: (css: string) => {
DataStore.set("VencordQuickCss", css);
listeners[IpcEvents.QUICK_CSS_UPDATE]?.forEach(l => l(null, css));
},
2022-10-04 00:52:42 +02:00
[IpcEvents.GET_SETTINGS]: () => localStorage.getItem("VencordSettings") || "{}",
[IpcEvents.SET_SETTINGS]: (s: string) => localStorage.setItem("VencordSettings", s),
[IpcEvents.GET_UPDATES]: () => ({ ok: true, value: [] }),
[IpcEvents.OPEN_EXTERNAL]: (url: string) => open(url, "_blank"),
};
function onEvent(event: string, ...args: any[]) {
const handler = handlers[event];
if (!handler) throw new Error(`Event ${event} not implemented.`);
return handler(...args);
}
2022-10-21 23:58:41 +02:00
// probably should make this less cursed at some point
2022-10-04 00:52:42 +02:00
window.VencordNative = {
getVersions: () => ({}),
ipc: {
send: (event: string, ...args: any[]) => void onEvent(event, ...args),
sendSync: onEvent,
on(event: string, listener: () => {}) {
2022-10-21 23:58:41 +02:00
(listeners[event] ??= new Set()).add(listener);
2022-10-04 00:52:42 +02:00
},
off(event: string, listener: () => {}) {
2022-10-21 23:58:41 +02:00
return listeners[event]?.delete(listener);
2022-10-04 00:52:42 +02:00
},
invoke: (event: string, ...args: any[]) => Promise.resolve(onEvent(event, ...args))
},
};