Skip to content

Commit

Permalink
fix: vencordorion
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Jul 15, 2023
1 parent 6deccd6 commit 34e6b85
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 0 additions & 17 deletions src-tauri/injection/injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ function onClientLoad() {
loaded = true
observer.disconnect()

// Recreate window.localStorage
createLocalStorage()

// Notifcation watcher
notifGetter()

Expand Down Expand Up @@ -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')
Expand Down
16 changes: 16 additions & 0 deletions src-tauri/injection/preinject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
72 changes: 68 additions & 4 deletions src-tauri/src/injection.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -101,7 +134,6 @@ fn periodic_injection_check(
break;
}

// Check if window.dorion exists
window
.eval(
format!(
Expand All @@ -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()
}
}
}
2 changes: 2 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 34e6b85

Please sign in to comment.