This commit is contained in:
Vendicated 2022-08-29 18:11:44 +02:00
parent af498e7829
commit 876e622f4f
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
17 changed files with 289 additions and 34 deletions

View file

@ -1,21 +1,80 @@
#!/usr/bin/node
import esbuild from "esbuild";
import { readdirSync } from "fs";
import { performance } from "perf_hooks";
/**
* @type {esbuild.WatchMode}
*/
const watch = {
onRebuild: (err) => {
if (err) console.error("Build Error", err.message);
else console.log("Rebuilt!");
}
};
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
const makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup(build) {
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, args => ({ path: args.path, external: true }));
},
};
const globPlugins = {
name: "glob-plugins",
setup: build => {
build.onResolve({ filter: /^plugins$/ }, args => {
return {
namespace: "import-plugins",
path: args.path
};
});
build.onLoad({ filter: /^plugins$/, namespace: "import-plugins" }, () => {
const files = readdirSync("./src/plugins");
let code = "";
let arr = "[";
for (let i = 0; i < files.length; i++) {
if (files[i] === "index.ts") {
continue;
}
const mod = `__pluginMod${i}`;
code += `import ${mod} from "./${files[i].replace(".ts", "")}";\n`;
arr += `${mod},`;
}
code += `export default ${arr}]`;
return {
contents: code,
resolveDir: "./src/plugins"
};
});
}
};
const begin = performance.now();
await Promise.all([
esbuild.build({
entryPoints: ["src/preload.ts"],
outfile: "dist/preload.js",
format: "cjs",
treeShaking: true,
bundle: true,
platform: "node",
target: ["esnext"]
target: ["esnext"],
plugins: [makeAllPackagesExternalPlugin],
watch
}),
esbuild.build({
entryPoints: ["src/patcher.ts"],
outfile: "dist/patcher.js",
bundle: true,
format: "cjs",
target: ["esnext"],
platform: "node"
external: ["electron"],
platform: "node",
plugins: [makeAllPackagesExternalPlugin],
watch
}),
esbuild.build({
entryPoints: ["src/Vencord.ts"],
@ -24,8 +83,19 @@ await Promise.all([
bundle: true,
target: ["esnext"],
footer: { js: "//# sourceURL=VencordRenderer" },
globalName: "Vencord"
globalName: "Vencord",
external: ["plugins"],
plugins: [
globPlugins
],
watch
})
]);
]).then(res => {
const took = performance.now() - begin;
console.log(`Built in ${took.toFixed(2)}ms`);
}).catch(err => {
console.error("Build failed");
console.error(err.message);
});
console.log("Built!");
if (watch) console.log("Watching...");

View file

@ -8,5 +8,9 @@
"electron-devtools-installer": "^3.2.0",
"jsposed": "^1.0.2",
"prettier": "^2.7.1"
},
"scripts": {
"build": "node build.mjs",
"watch": "node build.mjs --watch"
}
}

View file

@ -5,6 +5,7 @@ specifiers:
electron: ^20.1.0
electron-devtools-installer: ^3.2.0
esbuild: ^0.15.5
esbuild-plugin-import-glob: ^0.1.1
jsposed: ^1.0.2
prettier: ^2.7.1
@ -17,6 +18,7 @@ devDependencies:
'@types/node': 18.7.13
electron: 20.1.0
esbuild: 0.15.5
esbuild-plugin-import-glob: 0.1.1
packages:
@ -47,6 +49,27 @@ packages:
dev: true
optional: true
/@nodelib/fs.scandir/2.1.5:
resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==}
engines: {node: '>= 8'}
dependencies:
'@nodelib/fs.stat': 2.0.5
run-parallel: 1.2.0
dev: true
/@nodelib/fs.stat/2.0.5:
resolution: {integrity: sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==}
engines: {node: '>= 8'}
dev: true
/@nodelib/fs.walk/1.2.8:
resolution: {integrity: sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==}
engines: {node: '>= 8'}
dependencies:
'@nodelib/fs.scandir': 2.1.5
fastq: 1.13.0
dev: true
/@sindresorhus/is/0.14.0:
resolution: {integrity: sha512-9NET910DNaIPngYnLLPeg+Ogzqsi9uM4mSboU5y6p8S5DzMTVEsJZrawi+BoDNUVBa2DhJqQYUFvMDfgU062LQ==}
engines: {node: '>=6'}
@ -91,6 +114,13 @@ packages:
concat-map: 0.0.1
dev: false
/braces/3.0.2:
resolution: {integrity: sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==}
engines: {node: '>=8'}
dependencies:
fill-range: 7.0.1
dev: true
/buffer-crc32/0.2.13:
resolution: {integrity: sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==}
dev: true
@ -359,6 +389,12 @@ packages:
dev: true
optional: true
/esbuild-plugin-import-glob/0.1.1:
resolution: {integrity: sha512-yAFH+9AoIcsQkODSx0KUPRv1FeJUN6Tef8vkPQMcuVkc2vXYneYKsHhOiFS/yIsg5bQ70HHtAlXVA1uTjgoJXg==}
dependencies:
fast-glob: 3.2.11
dev: true
/esbuild-sunos-64/0.15.5:
resolution: {integrity: sha512-2sIzhMUfLNoD+rdmV6AacilCHSxZIoGAU2oT7XmJ0lXcZWnCvCtObvO6D4puxX9YRE97GodciRGDLBaiC6x1SA==}
engines: {node: '>=12'}
@ -444,12 +480,36 @@ packages:
- supports-color
dev: true
/fast-glob/3.2.11:
resolution: {integrity: sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==}
engines: {node: '>=8.6.0'}
dependencies:
'@nodelib/fs.stat': 2.0.5
'@nodelib/fs.walk': 1.2.8
glob-parent: 5.1.2
merge2: 1.4.1
micromatch: 4.0.5
dev: true
/fastq/1.13.0:
resolution: {integrity: sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==}
dependencies:
reusify: 1.0.4
dev: true
/fd-slicer/1.1.0:
resolution: {integrity: sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==}
dependencies:
pend: 1.2.0
dev: true
/fill-range/7.0.1:
resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==}
engines: {node: '>=8'}
dependencies:
to-regex-range: 5.0.1
dev: true
/fs-extra/8.1.0:
resolution: {integrity: sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==}
engines: {node: '>=6 <7 || >=8'}
@ -491,6 +551,13 @@ packages:
pump: 3.0.0
dev: true
/glob-parent/5.1.2:
resolution: {integrity: sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==}
engines: {node: '>= 6'}
dependencies:
is-glob: 4.0.3
dev: true
/glob/7.2.3:
resolution: {integrity: sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==}
dependencies:
@ -602,6 +669,23 @@ packages:
dev: true
optional: true
/is-extglob/2.1.1:
resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}
engines: {node: '>=0.10.0'}
dev: true
/is-glob/4.0.3:
resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==}
engines: {node: '>=0.10.0'}
dependencies:
is-extglob: 2.1.1
dev: true
/is-number/7.0.0:
resolution: {integrity: sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==}
engines: {node: '>=0.12.0'}
dev: true
/isarray/1.0.0:
resolution: {integrity: sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==}
dev: false
@ -675,6 +759,19 @@ packages:
dev: true
optional: true
/merge2/1.4.1:
resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==}
engines: {node: '>= 8'}
dev: true
/micromatch/4.0.5:
resolution: {integrity: sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==}
engines: {node: '>=8.6'}
dependencies:
braces: 3.0.2
picomatch: 2.3.1
dev: true
/mimic-response/1.0.1:
resolution: {integrity: sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==}
engines: {node: '>=4'}
@ -744,6 +841,11 @@ packages:
resolution: {integrity: sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==}
dev: true
/picomatch/2.3.1:
resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==}
engines: {node: '>=8.6'}
dev: true
/pify/3.0.0:
resolution: {integrity: sha512-C3FsVNH1udSEX48gGX1xfvwTWfsYWj5U+8/uK15BGzIGrKoUpghX8hWZwa/OFnakBiiVNmBvemTJR5mcy7iPcg==}
engines: {node: '>=4'}
@ -782,6 +884,10 @@ packages:
once: 1.4.0
dev: true
/queue-microtask/1.2.3:
resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
dev: true
/readable-stream/2.3.7:
resolution: {integrity: sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==}
dependencies:
@ -800,6 +906,11 @@ packages:
lowercase-keys: 1.0.1
dev: true
/reusify/1.0.4:
resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==}
engines: {iojs: '>=1.0.0', node: '>=0.10.0'}
dev: true
/rimraf/3.0.2:
resolution: {integrity: sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==}
hasBin: true
@ -820,6 +931,12 @@ packages:
dev: true
optional: true
/run-parallel/1.2.0:
resolution: {integrity: sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==}
dependencies:
queue-microtask: 1.2.3
dev: true
/safe-buffer/5.1.2:
resolution: {integrity: sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==}
dev: false
@ -878,6 +995,13 @@ packages:
engines: {node: '>=6'}
dev: true
/to-regex-range/5.0.1:
resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==}
engines: {node: '>=8.0'}
dependencies:
is-number: 7.0.0
dev: true
/tslib/2.4.0:
resolution: {integrity: sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==}
dev: false

View file

@ -1,3 +1,5 @@
import "./utils/patchWebpack";
import "./utils/quickCss";
export const Webpack = {};
export const Webpack = {};
import "./plugins";

11
src/VencordNative.ts Normal file
View file

@ -0,0 +1,11 @@
import { IPC_QUICK_CSS_UPDATE, IPC_GET_QUICK_CSS } from './utils/ipcEvents';
import { ipcRenderer } from 'electron';
export default {
handleQuickCssUpdate(cb: (s: string) => void) {
ipcRenderer.on(IPC_QUICK_CSS_UPDATE, (_, css) => {
cb(css);
});
},
getQuickCss: () => ipcRenderer.invoke(IPC_GET_QUICK_CSS)
};

13
src/globals.d.ts vendored
View file

@ -1,9 +1,14 @@
declare var appSettings: any;
import TVencordNative from "./VencordNative";
declare global {
export var VencordNative: typeof TVencordNative;
export var appSettings: {
set(setting: string, v: any): void;
};
interface Window {
webpackChunkdiscord_app: { push(chunk): any; };
webpackChunkdiscord_app: {
push(chunk: any): any;
};
}
}
export { };

25
src/ipcMain.ts Normal file
View file

@ -0,0 +1,25 @@
import { app, BrowserWindow, ipcMain } from "electron";
import { fstat, watch } from "fs";
import { open, readFile } from "fs/promises";
import { join } from 'path';
import { IPC_GET_SETTINGS_DIR, IPC_GET_QUICK_CSS, IPC_QUICK_CSS_UPDATE } from './utils/ipcEvents';
const DATA_DIR = join(app.getPath("userData"), "..", "Vencord");
const SETTINGS_DIR = join(DATA_DIR, "settings");
const QUICKCSS_PATH = join(SETTINGS_DIR, "quickCss.css");
function readCss() {
return readFile(QUICKCSS_PATH, "utf-8").catch(() => "");
}
ipcMain.handle(IPC_GET_SETTINGS_DIR, () => SETTINGS_DIR);
ipcMain.handle(IPC_GET_QUICK_CSS, () => readCss());
export function initIpc(mainWindow: BrowserWindow) {
open(QUICKCSS_PATH, "a+").then(fd => {
fd.close();
watch(QUICKCSS_PATH, async () => {
mainWindow.webContents.postMessage(IPC_QUICK_CSS_UPDATE, await readCss());
});
});
}

View file

@ -2,20 +2,21 @@
import electron, { app, BrowserWindowConstructorOptions } from "electron";
import installExt, { REACT_DEVELOPER_TOOLS } from "electron-devtools-installer";
import { join } from "path";
import { initIpc } from './ipcMain';
console.log("[Vencord] Starting up...");
class BrowserWindow extends electron.BrowserWindow {
constructor(options: BrowserWindowConstructorOptions) {
if (options?.webPreferences?.preload && options.title) {
const original = options.webPreferences.preload;
options.webPreferences.preload = join(__dirname, "preload.js");
process.env.APP_PATH = app.getAppPath();
process.env.DISCORD_PRELOAD = original;
}
super(options);
super(options);
initIpc(this);
} else super(options);
}
}
Object.assign(BrowserWindow, electron.BrowserWindow);
@ -28,10 +29,11 @@ require.cache[electronPath]!.exports = {
BrowserWindow
};
// Patch appSettingsa to force enable devtools
// Patch appSettings to force enable devtools
Object.defineProperty(global, "appSettings", {
set: (v) => {
set: (v: typeof global.appSettings) => {
v.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true);
// @ts-ignore
delete global.appSettings;
global.appSettings = v;
},
@ -41,20 +43,17 @@ Object.defineProperty(global, "appSettings", {
process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord");
electron.app.whenReady().then(() => {
/* installExt(REACT_DEVELOPER_TOOLS)
installExt(REACT_DEVELOPER_TOOLS)
.then(() => console.log("Installed React DevTools"))
.catch((err) => console.error("Failed to install React DevTools", err)); */
.catch((err) => console.error("Failed to install React DevTools", err));
// Remove CSP
electron.session.defaultSession.webRequest.onHeadersReceived(({ responseHeaders, url }, cb) => {
if (responseHeaders && url.endsWith(".css")) {
if (responseHeaders) {
delete responseHeaders["content-security-policy-report-only"];
delete responseHeaders["content-security-policy"];
// probably makes github raw work? not tested.
responseHeaders["content-type"] = ["text/css"];
responseHeaders;
}
cb({ cancel: false, responseHeaders: responseHeaders });
cb({ cancel: false, responseHeaders });
});
// Drop science and sentry requests

4
src/plugins.d.ts vendored Normal file
View file

@ -0,0 +1,4 @@
declare module "plugins" {
var plugins: Record<string, any>[];
export default plugins;
}

3
src/plugins/bar.ts Normal file
View file

@ -0,0 +1,3 @@
export default {
name: "bar"
};

3
src/plugins/foo.ts Normal file
View file

@ -0,0 +1,3 @@
export default {
name: "foo"
};

3
src/plugins/index.ts Normal file
View file

@ -0,0 +1,3 @@
import plugins from "plugins";
console.log(plugins);

View file

@ -1,14 +1,10 @@
import { contextBridge, webFrame } from "electron";
import { readFileSync } from "fs";
import { join } from "path";
import Vencord from "./Vencord";
import VencordNative from "./VencordNative";
contextBridge.exposeInMainWorld("VencordNative", {
getSettings: () => "hi"
});
contextBridge.exposeInMainWorld("VencordNative", VencordNative);
webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8"));
require(process.env.DISCORD_PRELOAD!);
window.onload = () => console.log("hi");

View file

@ -1,4 +1 @@
import { join } from 'path';
export const WEBPACK_CHUNK = "webpackChunkdiscord_app";
// export const SETTINGS_DIR = join(process.env.DATA_DIR!, "settings");

3
src/utils/ipcEvents.ts Normal file
View file

@ -0,0 +1,3 @@
export const IPC_QUICK_CSS_UPDATE = "VencordQuickCssUpdate";
export const IPC_GET_QUICK_CSS = "VencordGetQuickCss";
export const IPC_GET_SETTINGS_DIR = "VencordGetSettingsDir";

6
src/utils/quickCss.ts Normal file
View file

@ -0,0 +1,6 @@
document.addEventListener("DOMContentLoaded", async () => {
const style = document.createElement("style");
document.head.appendChild(style);
VencordNative.handleQuickCssUpdate((css: string) => style.innerText = css);
style.innerText = await VencordNative.getQuickCss();
});

View file

@ -9,5 +9,5 @@
"noImplicitAny": false,
"target": "ESNEXT"
},
"include": ["src/*"]
"include": ["src/**/*.ts"]
}