From 30cab5b58db467621f94eedbd4de1a79bda911e7 Mon Sep 17 00:00:00 2001 From: YomoSK Date: Sat, 9 Nov 2024 19:10:25 +0100 Subject: [PATCH] Fixed lots of bugs & QoL --- rust-src/components/home/index.html | 10 +++ rust-src/components/script.js | 34 ---------- .../{topbar.html => topbar/index.html} | 18 ++++-- rust-src/components/topbar/script.js | 64 +++++++++++++++++++ rust-src/components/{ => topbar}/styles.css | 22 ++++++- rust-src/src/lib.rs | 53 +++++++-------- 6 files changed, 126 insertions(+), 75 deletions(-) create mode 100644 rust-src/components/home/index.html delete mode 100644 rust-src/components/script.js rename rust-src/components/{topbar.html => topbar/index.html} (66%) create mode 100644 rust-src/components/topbar/script.js rename rust-src/components/{ => topbar}/styles.css (80%) diff --git a/rust-src/components/home/index.html b/rust-src/components/home/index.html new file mode 100644 index 0000000..9cf75d8 --- /dev/null +++ b/rust-src/components/home/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/rust-src/components/script.js b/rust-src/components/script.js deleted file mode 100644 index 3a08b7b..0000000 --- a/rust-src/components/script.js +++ /dev/null @@ -1,34 +0,0 @@ -const { invoke } = window.__TAURI__.core; -const { listen } = window.__TAURI__.event; - -const title = document.getElementById('tab-title'); -const searchbar = document.getElementById('searchbar'); - -document.addEventListener('DOMContentLoaded', () => { - invoke('get_title').then(t => { - title.innerText = t; - if(t) title.style.display = null; - }); - - searchbar.addEventListener('keydown', ({ target, key }) => { - const url = target.value.trim(); - if(key.toUpperCase() == 'ENTER' && url != 'about:blank') invoke('load_url', { url }); - }); - - document.querySelector('#controls>svg').addEventListener('click', () => invoke('close')); - - window.addEventListener('contextmenu', event => event.preventDefault()); -}); - -listen('title_change', ({ payload }) => { - if(payload) { - title.innerText = payload; - title.style.display = null; - } - else title.style.display = 'none'; -}); - -listen('url_change', ({ payload }) => { - const url = payload.endsWith('/') ? payload.substring(0, payload.length - 1) : payload; - if(payload && url != 'about:blank') searchbar.value = url; -}); \ No newline at end of file diff --git a/rust-src/components/topbar.html b/rust-src/components/topbar/index.html similarity index 66% rename from rust-src/components/topbar.html rename to rust-src/components/topbar/index.html index aeb458a..2baef03 100644 --- a/rust-src/components/topbar.html +++ b/rust-src/components/topbar/index.html @@ -15,13 +15,17 @@ - +
diff --git a/rust-src/components/topbar/script.js b/rust-src/components/topbar/script.js new file mode 100644 index 0000000..e73d563 --- /dev/null +++ b/rust-src/components/topbar/script.js @@ -0,0 +1,64 @@ +const { invoke } = window.__TAURI__.core; +const { listen } = window.__TAURI__.event; + +const title = document.getElementById('tab-title'); +const searchbar = document.querySelector('#searchbar>input'); + +function subpayload(payload, length = 20) { + if(payload.length > length) payload = payload.substring(0, length) + '...'; + return payload; +} + +document.addEventListener('DOMContentLoaded', () => { + // invoke('get_title').then(t => { + // title.innerText = t; + // if(t) title.style.display = null; + // }); + localStorage.clear(); + + searchbar.addEventListener('keydown', ({ target, key }) => { + const url = target.value.trim(); + if(key.toUpperCase() == 'ENTER' && url != 'about:blank') { + target.blur(); + invoke('load_url', { + url: url.includes(' ') ? 'https://google.com/search?q=' + url.split(' ').join('+') : url.includes('://') ? url : 'https://' + url + }); + } + }); + + searchbar.addEventListener('focus', ({ target }) => { + if(target.value) target.value = localStorage.getItem('origin_url') || target.value; + }); + + searchbar.addEventListener('blur', ({ target }) => { + target.value = localStorage.getItem('pretty_url') || target.value; + }); + + document.querySelector('#controls>svg').addEventListener('click', () => { + invoke('close'); + }); + + window.addEventListener('contextmenu', event => event.preventDefault()); +}); + +listen('title_change', ({ payload }) => { + if(payload) { + title.innerText = subpayload(payload); + title.style.display = null; + } + else title.style.display = 'none'; +}); + +listen('url_change', ({ payload }) => { + const url = payload.endsWith('/') ? payload.substring(0, payload.length - 1) : payload; + if(payload && url != 'about:blank') { + const isHTTPS = url.startsWith('https://'); + + document.querySelector('#searchbar>svg').style.display = isHTTPS ? 'block' : null; + searchbar.style.paddingLeft = isHTTPS ? 'calc(2*.4rem + 20px)' : null; + + localStorage.setItem('origin_url', url); + searchbar.value = (isHTTPS ? url.split('://')[1] : url).replace('www.', ''); + localStorage.setItem('pretty_url', searchbar.value); + } +}); \ No newline at end of file diff --git a/rust-src/components/styles.css b/rust-src/components/topbar/styles.css similarity index 80% rename from rust-src/components/styles.css rename to rust-src/components/topbar/styles.css index 2d37e4b..dc41e17 100644 --- a/rust-src/components/styles.css +++ b/rust-src/components/topbar/styles.css @@ -14,7 +14,7 @@ body::before { top: 0; left: 0; width: 100%; - height: 100%; + height: 60px; background: #E3F4FE; opacity: .75; z-index: -1; @@ -45,7 +45,7 @@ nav > #tabs > svg, nav > #controls > svg { cursor: pointer; } -nav > #tabs > div { +nav > #tabs > #tab-title { padding: 0 .5rem; background: #E2E5E8; border-radius: 8px; @@ -54,10 +54,26 @@ nav > #tabs > div { display: flex; justify-content: center; align-items: center; + text-wrap: nowrap; +} + +nav > #searchbar { + position: relative; + height: -webkit-fill-available; } -nav > input#searchbar { +nav > #searchbar > svg { + position: absolute; + top: 50%; + left: .4rem; + transform: translateY(-50%); + width: 20px; + display: none; +} + +nav > #searchbar > input { padding: .4rem 1rem; + width: 100%; height: -webkit-fill-available; background: #E2E5E8; border: none; diff --git a/rust-src/src/lib.rs b/rust-src/src/lib.rs index 40a64d7..093999e 100644 --- a/rust-src/src/lib.rs +++ b/rust-src/src/lib.rs @@ -2,15 +2,7 @@ use isahc::ReadResponseExt; use tauri::{AppHandle, Builder, Emitter, LogicalPosition, LogicalSize, Manager, WebviewBuilder, WebviewUrl, WindowBuilder}; use serde_json::Value; -pub struct _Yomea { - pub url: &'static str -} - -pub static YOMEA: _Yomea = _Yomea { - url: "about:blank" -}; - -pub fn get_title_url(url: String, followredirects: bool) -> Result { +pub fn get_title_url(url: &str, followredirects: bool) -> Result { let mut http = match isahc::get(url) { Ok(res) => res, Err(_) => { @@ -24,7 +16,7 @@ pub fn get_title_url(url: String, followredirects: bool) -> Result if status.is_redirection() && followredirects { if let Some(location) = http.headers().get("Location") { if let Ok(redirect) = location.to_str() { - return Ok(get_title_url(redirect.to_string(), true).unwrap()); + return Ok(get_title_url(redirect, true).unwrap()); } } } @@ -45,28 +37,17 @@ pub fn get_title_url(url: String, followredirects: bool) -> Result } #[tauri::command] -fn get_title() -> String { - let url = YOMEA.url.to_string(); - if url == "about:blank" { - return "".to_string(); - } - - match get_title_url(url, true) { - Ok(title) => title, - Err(code) => code.to_string() - } -} - -#[tauri::command] -fn load_url(app: AppHandle, url: String) { +fn load_url(app: AppHandle, url: &str) { let topbar = app.get_webview("topbar").unwrap(); let mut webview = app.get_webview("webview").unwrap(); // TODO: Basically this requests the site twice: // 1. window.location.href // 2. get_title_url() webview.navigate(url.parse().unwrap()).unwrap(); - + let title = get_title_url(url, true).unwrap_or("".to_string()); + topbar.emit("title_change", title).unwrap(); + // if title.is_err() { // topbar.emit("title_change", title.err()); // } @@ -77,7 +58,6 @@ fn load_url(app: AppHandle, url: String) { // Ok(title) => topbar.emit("title_change", title), // Err(code) => topbar.emit("title_change", "code") // } - topbar.emit("title_change", title).unwrap(); } #[tauri::command] @@ -90,9 +70,10 @@ pub fn run() { let size = serde_json::json!({ "width": 1400, "height": 800 }); Builder::default() - .invoke_handler(tauri::generate_handler![get_title, load_url, close]) + .invoke_handler(tauri::generate_handler![load_url, close]) .setup(move |app| { - let topbarcomponent = "topbar.html".into(); + let topbarcomponent = "topbar/index.html".into(); + let homecomponent = "home/index.html".into(); let width = size.get("width").and_then(Value::as_f64).unwrap(); let height = size.get("height").and_then(Value::as_f64).unwrap(); @@ -112,11 +93,21 @@ pub fn run() { let handle = app.app_handle().clone(); let webview = WebviewBuilder::new( "webview", - WebviewUrl::External(YOMEA.url.parse().unwrap()) + WebviewUrl::App(homecomponent) ).on_navigation(move |url| { let topbar = handle.get_webview("topbar").unwrap(); - topbar.emit("url_change", url.to_string()).unwrap(); - topbar.emit("title_change", get_title_url(url.to_string(), false).unwrap_or("".to_string())).unwrap(); + let strurl = url.to_string(); + + if !strurl.starts_with("http") { + println!("Blocked access for {}", strurl); + return false; + } + + if !strurl.contains("tauri.localhost") && !strurl.contains("127.0.0.1:1430") { + topbar.emit("url_change", strurl).unwrap(); + topbar.emit("title_change", get_title_url(url.as_str(), false).unwrap_or("".to_string())).unwrap(); + } + true });