2022-10-02 01:11:30 +00:00
|
|
|
import { app, BrowserWindow, desktopCapturer, ipcMain, shell } from "electron";
|
|
|
|
import { mkdirSync, readFileSync, watch } from "fs";
|
|
|
|
import { open, readFile, writeFile } from "fs/promises";
|
2022-10-05 22:42:58 +00:00
|
|
|
import { join } from "path";
|
2022-10-02 01:11:30 +00:00
|
|
|
import { debounce } from "../utils/debounce";
|
2022-10-05 22:42:58 +00:00
|
|
|
import IpcEvents from "../utils/IpcEvents";
|
2022-10-02 01:11:30 +00:00
|
|
|
|
|
|
|
import "./updater";
|
|
|
|
|
|
|
|
const DATA_DIR = join(app.getPath("userData"), "..", "Vencord");
|
|
|
|
const SETTINGS_DIR = join(DATA_DIR, "settings");
|
|
|
|
const QUICKCSS_PATH = join(SETTINGS_DIR, "quickCss.css");
|
|
|
|
const SETTINGS_FILE = join(SETTINGS_DIR, "settings.json");
|
2022-10-09 17:55:13 +00:00
|
|
|
const ALLOWED_PROTOCOLS = [
|
|
|
|
"https:",
|
|
|
|
"http:",
|
|
|
|
"steam:",
|
|
|
|
"spotify:"
|
|
|
|
];
|
2022-10-02 01:11:30 +00:00
|
|
|
|
|
|
|
mkdirSync(SETTINGS_DIR, { recursive: true });
|
|
|
|
|
|
|
|
function readCss() {
|
|
|
|
return readFile(QUICKCSS_PATH, "utf-8").catch(() => "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function readSettings() {
|
|
|
|
try {
|
|
|
|
return readFileSync(SETTINGS_FILE, "utf-8");
|
|
|
|
} catch {
|
|
|
|
return "{}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix for screensharing in Electron >= 17
|
|
|
|
ipcMain.handle(IpcEvents.GET_DESKTOP_CAPTURE_SOURCES, (_, opts) => desktopCapturer.getSources(opts));
|
|
|
|
|
2022-10-03 17:17:54 +00:00
|
|
|
ipcMain.handle(IpcEvents.OPEN_QUICKCSS, () => shell.openPath(QUICKCSS_PATH));
|
|
|
|
|
|
|
|
ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => {
|
|
|
|
try {
|
|
|
|
var { protocol } = new URL(url);
|
|
|
|
} catch {
|
|
|
|
throw "Malformed URL";
|
|
|
|
}
|
2022-10-09 17:55:13 +00:00
|
|
|
if (!ALLOWED_PROTOCOLS.includes(protocol))
|
2022-10-03 17:17:54 +00:00
|
|
|
throw "Disallowed protocol.";
|
|
|
|
|
|
|
|
shell.openExternal(url);
|
|
|
|
});
|
2022-10-02 01:11:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
ipcMain.handle(IpcEvents.GET_QUICK_CSS, () => readCss());
|
|
|
|
|
|
|
|
ipcMain.handle(IpcEvents.GET_SETTINGS_DIR, () => SETTINGS_DIR);
|
2022-10-05 22:42:58 +00:00
|
|
|
ipcMain.on(IpcEvents.GET_SETTINGS, e => e.returnValue = readSettings());
|
2022-10-02 01:11:30 +00:00
|
|
|
|
|
|
|
let settingsWriteQueue = Promise.resolve();
|
|
|
|
ipcMain.handle(IpcEvents.SET_SETTINGS, (_, s) => {
|
|
|
|
settingsWriteQueue = settingsWriteQueue.then(() => writeFile(SETTINGS_FILE, s));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export function initIpc(mainWindow: BrowserWindow) {
|
|
|
|
open(QUICKCSS_PATH, "a+").then(fd => {
|
|
|
|
fd.close();
|
|
|
|
watch(QUICKCSS_PATH, debounce(async () => {
|
|
|
|
mainWindow.webContents.postMessage(IpcEvents.QUICK_CSS_UPDATE, await readCss());
|
|
|
|
}, 50));
|
|
|
|
});
|
|
|
|
}
|