From 34e6b8584914dae9f1ccf1facaa2a9272ec8dffb Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 14 Jul 2023 20:35:02 -0700 Subject: [PATCH] fix: vencordorion --- .github/workflows/build.yml | 4 +- .gitignore | 3 ++ package.json | 3 +- src-tauri/injection/injection.js | 17 -------- src-tauri/injection/preinject.js | 16 +++++++ src-tauri/src/config.rs | 2 + src-tauri/src/injection.rs | 72 ++++++++++++++++++++++++++++++-- src-tauri/tauri.conf.json | 2 + 8 files changed, 95 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 761965f9..a2e7a05b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,7 +34,7 @@ jobs: toolchain: stable - name: Install deps and build - run: yarn && yarn build && yarn tauri build + run: yarn && yarn update && yarn build && yarn tauri build # Move built to its own folder for compression - name: Move build @@ -79,7 +79,7 @@ jobs: run: sudo apt install libwebkit2gtk-4.0-dev build-essential curl wget libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev - name: Install deps and build - run: yarn && yarn build && yarn tauri build + run: yarn && yarn update && yarn build && yarn tauri build # Move built to its own folder for compression - name: Move build diff --git a/.gitignore b/.gitignore index 7d633724..409a8a26 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,7 @@ dist-ssr src-tauri/injection/injection_min.js src-tauri/injection/preinject_min.js +src-tauri/injection/browser.js +src-tauri/injection/browser.css + src-tauri/html diff --git a/package.json b/package.json index 79c8cb66..f171b552 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "format": "cargo fmt --manifest-path ./src-tauri/Cargo.toml --all", "lint": "eslint ./src ./src-tauri/injection/injection.js ./src-tauri/injection/preinject.js", "clippy": "cargo clippy --manifest-path ./src-tauri/Cargo.toml --no-default-features -- -D warnings", - "codefix": "yarn format && yarn clippy && yarn lint" + "codefix": "yarn format && yarn clippy && yarn lint", + "update": "curl -L https://github.com/SpikeHD/Vencordorion/releases/latest/download/browser.css -o src-tauri/injection/browser.css && curl -L https://github.com/SpikeHD/Vencordorion/releases/latest/download/browser.js -o src-tauri/injection/browser.js" }, "devDependencies": { "@tauri-apps/api": "^1.0.2", diff --git a/src-tauri/injection/injection.js b/src-tauri/injection/injection.js index 54660b68..2e2d5dfd 100644 --- a/src-tauri/injection/injection.js +++ b/src-tauri/injection/injection.js @@ -63,9 +63,6 @@ function onClientLoad() { loaded = true observer.disconnect() - // Recreate window.localStorage - createLocalStorage() - // Notifcation watcher notifGetter() @@ -137,20 +134,6 @@ function initTopBarEvents() { document.querySelector('#topmax').onclick = maximize } -/** - * Discord wipes `window.localStorage`, so we have to recreate it in case plugins require it - * - * https://github.com/SpikeHD/Dorion/issues/7#issuecomment-1320861432 - */ -function createLocalStorage() { - const iframe = document.createElement('iframe'); - document.head.append(iframe); - const pd = Object.getOwnPropertyDescriptor(iframe.contentWindow, 'localStorage'); - iframe.remove(); - - Object.defineProperty(window, 'localStorage', pd) -} - function applyNotificationCount() { const { invoke } = window.__TAURI__ const title = document.querySelector('title') diff --git a/src-tauri/injection/preinject.js b/src-tauri/injection/preinject.js index 6700ce60..99790a1a 100644 --- a/src-tauri/injection/preinject.js +++ b/src-tauri/injection/preinject.js @@ -54,6 +54,8 @@ async function createTopBar() { * This will localize imports for JS and CSS, as well as some other things */ (async () => { + createLocalStorage() + await displayLoadingTop() await createTopBar() @@ -270,4 +272,18 @@ function _interceptEventListeners() { } return this._addEventListener(...args); } +} + +/** + * Discord wipes `window.localStorage`, so we have to recreate it in case plugins require it + * + * https://github.com/SpikeHD/Dorion/issues/7#issuecomment-1320861432 + */ +function createLocalStorage() { + const iframe = document.createElement('iframe'); + document.head.append(iframe); + const pd = Object.getOwnPropertyDescriptor(iframe.contentWindow, 'localStorage'); + iframe.remove(); + + Object.defineProperty(window, 'localStorage', pd) } \ No newline at end of file diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 93c93e51..209a0332 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -88,6 +88,8 @@ pub fn get_client_type() -> String { pub fn get_systray() -> bool { let parsed: Config = serde_json::from_str(read_config_file().as_str()).unwrap_or_else(|_| default_config()); + + println!("{:?}", read_config_file().as_str()); parsed.sys_tray.unwrap_or(false) } diff --git a/src-tauri/src/injection.rs b/src-tauri/src/injection.rs index 251eeef0..f77150fc 100644 --- a/src-tauri/src/injection.rs +++ b/src-tauri/src/injection.rs @@ -1,5 +1,5 @@ -use std::{collections::HashMap, env, fs, thread, time::Duration}; -use tauri::regex::Regex; +use std::{collections::HashMap, env, fs, thread, time::Duration, path::PathBuf}; +use tauri::{regex::Regex, Manager}; use crate::{helpers::resource_folder, js_preprocess::eval_js_imports}; @@ -32,7 +32,40 @@ pub fn preinject(window: &tauri::Window) { } }; - window.eval(injection_js.as_str()).unwrap_or(()) + match window.eval(injection_js.as_str()) { + Ok(r) => r, + Err(e) => { + println!("Error evaluating preinject: {}", e) + } + }; + + // Run vencord's preinject script + match window.eval( + &get_vencord_js_content(&window.app_handle()) + ) { + Ok(r) => r, + Err(e) => { + println!("Error evaluating vencord preinject: {}", e) + } + }; + + // Inject vencords css + match window.eval( + format!( + " + const ts = document.createElement('style') + + ts.textContent = ` + {} + ` + + document.head.appendChild(ts)", get_vencord_css_content(&window.app_handle())).as_str() + ) { + Ok(r) => r, + Err(e) => { + println!("Error evaluating vencord css: {}", e) + } + }; } #[tauri::command] @@ -101,7 +134,6 @@ fn periodic_injection_check( break; } - // Check if window.dorion exists window .eval( format!( @@ -116,3 +148,35 @@ fn periodic_injection_check( } }); } + +pub fn get_vencord_js_content(app: &tauri::AppHandle) -> String { + let path = app + .path_resolver() + .resolve_resource(PathBuf::from("injection/browser.js")) + .unwrap(); + + match fs::read_to_string(path) { + Ok(f) => f, + Err(e) => { + println!("Failed to read browser.js in resource dir: {}", e); + + String::new() + } + } +} + +pub fn get_vencord_css_content(app: &tauri::AppHandle) -> String { + let path = app + .path_resolver() + .resolve_resource(PathBuf::from("injection/browser.css")) + .unwrap(); + + match fs::read_to_string(path) { + Ok(f) => f, + Err(e) => { + println!("Failed to read browser.css in resource dir: {}", e); + + String::new() + } + } +} \ No newline at end of file diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 04a99bbe..e2961859 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -42,6 +42,8 @@ "resources": [ "injection/preinject_min.js", "injection/injection_min.js", + "injection/browser.js", + "injection/browser.css", "html/index.html", "html/top.html", "html/notification.html",