2022-10-21 23:17:06 +00: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-22 16:18:41 +00:00
|
|
|
import "./updater";
|
2023-06-29 14:06:21 +00:00
|
|
|
import "./ipcPlugins";
|
2024-03-13 20:45:45 +00:00
|
|
|
import "./settings";
|
2022-10-22 16:18:41 +00:00
|
|
|
|
2024-03-13 20:59:09 +00:00
|
|
|
import { debounce } from "@shared/debounce";
|
|
|
|
import { IpcEvents } from "@shared/IpcEvents";
|
2023-09-25 16:53:10 +00:00
|
|
|
import { BrowserWindow, ipcMain, shell, systemPreferences } from "electron";
|
2024-05-26 17:12:18 +00:00
|
|
|
import monacoHtml from "file://monacoWin.html?minify&base64";
|
2024-03-13 20:45:45 +00:00
|
|
|
import { FSWatcher, mkdirSync, watch, writeFileSync } from "fs";
|
|
|
|
import { open, readdir, readFile } from "fs/promises";
|
2023-08-04 17:52:20 +00:00
|
|
|
import { join, normalize } from "path";
|
2022-10-22 16:18:41 +00:00
|
|
|
|
2023-08-04 17:52:20 +00:00
|
|
|
import { getThemeInfo, stripBOM, UserThemeHeader } from "./themes";
|
2024-03-13 20:45:45 +00:00
|
|
|
import { ALLOWED_PROTOCOLS, QUICKCSS_PATH, THEMES_DIR } from "./utils/constants";
|
2023-08-04 18:13:53 +00:00
|
|
|
import { makeLinksOpenExternally } from "./utils/externalLinks";
|
2022-10-22 04:31:47 +00:00
|
|
|
|
2023-08-04 17:52:20 +00:00
|
|
|
mkdirSync(THEMES_DIR, { recursive: true });
|
|
|
|
|
|
|
|
export function ensureSafePath(basePath: string, path: string) {
|
|
|
|
const normalizedBasePath = normalize(basePath);
|
|
|
|
const newPath = join(basePath, path);
|
|
|
|
const normalizedPath = normalize(newPath);
|
|
|
|
return normalizedPath.startsWith(normalizedBasePath) ? normalizedPath : null;
|
|
|
|
}
|
2022-10-02 01:11:30 +00:00
|
|
|
|
|
|
|
function readCss() {
|
|
|
|
return readFile(QUICKCSS_PATH, "utf-8").catch(() => "");
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:52:20 +00:00
|
|
|
async function listThemes(): Promise<UserThemeHeader[]> {
|
|
|
|
const files = await readdir(THEMES_DIR).catch(() => []);
|
|
|
|
|
|
|
|
const themeInfo: UserThemeHeader[] = [];
|
|
|
|
|
|
|
|
for (const fileName of files) {
|
2023-08-18 23:33:39 +00:00
|
|
|
if (!fileName.endsWith(".css")) continue;
|
|
|
|
|
2023-08-04 17:52:20 +00:00
|
|
|
const data = await getThemeData(fileName).then(stripBOM).catch(() => null);
|
2023-08-18 23:33:39 +00:00
|
|
|
if (data == null) continue;
|
|
|
|
|
|
|
|
themeInfo.push(getThemeInfo(data, fileName));
|
2023-08-04 17:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return themeInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getThemeData(fileName: string) {
|
|
|
|
fileName = fileName.replace(/\?v=\d+$/, "");
|
|
|
|
const safePath = ensureSafePath(THEMES_DIR, fileName);
|
|
|
|
if (!safePath) return Promise.reject(`Unsafe path ${fileName}`);
|
|
|
|
return readFile(safePath, "utf-8");
|
|
|
|
}
|
|
|
|
|
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());
|
2022-10-22 02:41:33 +00:00
|
|
|
ipcMain.handle(IpcEvents.SET_QUICK_CSS, (_, css) =>
|
2024-03-13 20:45:45 +00:00
|
|
|
writeFileSync(QUICKCSS_PATH, css)
|
2022-10-22 02:41:33 +00:00
|
|
|
);
|
2022-10-02 01:11:30 +00:00
|
|
|
|
2023-08-04 17:52:20 +00:00
|
|
|
ipcMain.handle(IpcEvents.GET_THEMES_DIR, () => THEMES_DIR);
|
|
|
|
ipcMain.handle(IpcEvents.GET_THEMES_LIST, () => listThemes());
|
|
|
|
ipcMain.handle(IpcEvents.GET_THEME_DATA, (_, fileName) => getThemeData(fileName));
|
2023-09-25 16:53:10 +00:00
|
|
|
ipcMain.handle(IpcEvents.GET_THEME_SYSTEM_VALUES, () => ({
|
|
|
|
// win & mac only
|
|
|
|
"os-accent-color": `#${systemPreferences.getAccentColor?.() || ""}`
|
|
|
|
}));
|
2023-08-04 17:52:20 +00:00
|
|
|
|
2022-10-02 01:11:30 +00:00
|
|
|
|
|
|
|
export function initIpc(mainWindow: BrowserWindow) {
|
2024-03-07 14:33:00 +00:00
|
|
|
let quickCssWatcher: FSWatcher | undefined;
|
|
|
|
|
2022-10-02 01:11:30 +00:00
|
|
|
open(QUICKCSS_PATH, "a+").then(fd => {
|
|
|
|
fd.close();
|
2024-03-07 14:33:00 +00:00
|
|
|
quickCssWatcher = watch(QUICKCSS_PATH, { persistent: false }, debounce(async () => {
|
2022-10-02 01:11:30 +00:00
|
|
|
mainWindow.webContents.postMessage(IpcEvents.QUICK_CSS_UPDATE, await readCss());
|
|
|
|
}, 50));
|
2024-03-07 14:33:00 +00:00
|
|
|
}).catch(() => { });
|
2023-08-04 17:52:20 +00:00
|
|
|
|
2024-03-07 14:33:00 +00:00
|
|
|
const themesWatcher = watch(THEMES_DIR, { persistent: false }, debounce(() => {
|
2023-08-04 17:52:20 +00:00
|
|
|
mainWindow.webContents.postMessage(IpcEvents.THEME_UPDATE, void 0);
|
|
|
|
}));
|
2024-03-07 14:33:00 +00:00
|
|
|
|
|
|
|
mainWindow.once("closed", () => {
|
|
|
|
quickCssWatcher?.close();
|
|
|
|
themesWatcher.close();
|
|
|
|
});
|
2022-10-02 01:11:30 +00:00
|
|
|
}
|
2022-10-22 02:41:33 +00:00
|
|
|
|
|
|
|
ipcMain.handle(IpcEvents.OPEN_MONACO_EDITOR, async () => {
|
2024-01-13 18:27:41 +00:00
|
|
|
const title = "Vencord QuickCSS Editor";
|
|
|
|
const existingWindow = BrowserWindow.getAllWindows().find(w => w.title === title);
|
|
|
|
if (existingWindow && !existingWindow.isDestroyed()) {
|
|
|
|
existingWindow.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-22 02:41:33 +00:00
|
|
|
const win = new BrowserWindow({
|
2024-01-13 18:27:41 +00:00
|
|
|
title,
|
2022-12-02 13:11:20 +00:00
|
|
|
autoHideMenuBar: true,
|
|
|
|
darkTheme: true,
|
2022-10-22 02:41:33 +00:00
|
|
|
webPreferences: {
|
2023-08-10 22:14:50 +00:00
|
|
|
preload: join(__dirname, IS_DISCORD_DESKTOP ? "preload.js" : "vencordDesktopPreload.js"),
|
2022-12-02 13:11:20 +00:00
|
|
|
contextIsolation: true,
|
2023-02-17 14:37:38 +00:00
|
|
|
nodeIntegration: false,
|
|
|
|
sandbox: false
|
2022-10-22 02:41:33 +00:00
|
|
|
}
|
|
|
|
});
|
2023-08-04 18:13:53 +00:00
|
|
|
|
|
|
|
makeLinksOpenExternally(win);
|
|
|
|
|
2022-10-22 02:41:33 +00:00
|
|
|
await win.loadURL(`data:text/html;base64,${monacoHtml}`);
|
|
|
|
});
|