Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
fix: save webview downloads natively + silence unused var warning
Claudee8a6804f2539
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rsindex 6448d6d7..11f94af3 100644--- a/src-tauri/src/app/window.rs+++ b/src-tauri/src/app/window.rs@@ -1,12 +1,14 @@use crate::app::config::PakeConfig;-use crate::util::get_data_dir;+use crate::util::{+ check_file_or_append, get_data_dir, get_download_message_with_lang, show_toast, MessageType,+};use std::{path::PathBuf,str::FromStr,sync::atomic::{AtomicU32, Ordering},};use tauri::{- webview::{NewWindowFeatures, NewWindowResponse},+ webview::{DownloadEvent, NewWindowFeatures, NewWindowResponse},AppHandle, Config, Manager, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder,};@@ -426,6 +428,61 @@ fn build_window(}}+ // Capture webview-initiated downloads (blob:, data:, Content-Disposition,+ // etc.) and write them to the OS Downloads folder. This is essential for+ // sites with a strict Content-Security-Policy (e.g. Gemini): their+ // `connect-src` blocks Tauri's IPC origin, so downloads cannot be routed+ // through the JS bridge, and downloads triggered from a sandboxed iframe+ // can't reach the IPC either. Letting the browser download natively and+ // catching it here is independent of the page CSP and the IPC channel.+ {+ let download_handle = app.clone();+ window_builder = window_builder.on_download(move |_webview, event| match event {+ DownloadEvent::Requested { url, destination } => {+ match download_handle.path().download_dir() {+ Ok(download_dir) => {+ let filename = destination+ .file_name()+ .map(|name| name.to_string_lossy().to_string())+ .filter(|name| !name.is_empty())+ .or_else(|| {+ url.path_segments()+ .and_then(|mut segments| segments.next_back())+ .map(|segment| segment.to_string())+ .filter(|segment| !segment.is_empty())+ })+ .unwrap_or_else(|| "download".to_string());++ let target = download_dir.join(filename);+ if let Some(path_str) = target.to_str() {+ *destination = PathBuf::from(check_file_or_append(path_str));+ }+ }+ Err(error) => {+ eprintln!("[Pake] Failed to resolve download dir: {error}");+ }+ }+ true+ }+ DownloadEvent::Finished {+ url: _,+ path: _,+ success,+ } => {+ if let Some(window) = download_handle.get_webview_window("pake") {+ let message_type = if success {+ MessageType::Success+ } else {+ MessageType::Failure+ };+ show_toast(&window, &get_download_message_with_lang(message_type, None));+ }+ true+ }+ _ => true,+ });+ }+window_builder = window_builder.on_navigation(|_| true);window_builder.build()diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.jsindex f3bb7cf9..d5eb88ce 100644--- a/src-tauri/src/inject/event.js+++ b/src-tauri/src/inject/event.js@@ -423,37 +423,6 @@ document.addEventListener("DOMContentLoaded", () => {});}- // detect blob download by createElement("a")- function detectDownloadByCreateAnchor() {- const createEle = document.createElement;- document.createElement = (el) => {- if (el !== "a") return createEle.call(document, el);- const anchorEle = createEle.call(document, el);-- // use addEventListener to avoid overriding the original click event.- anchorEle.addEventListener(- "click",- (e) => {- const url = anchorEle.href;- const filename = anchorEle.download || getFilenameFromUrl(url);- if (window.blobToUrlCaches.has(url)) {- e.preventDefault();- e.stopImmediatePropagation();- downloadFromBlobUrl(url, filename);- // case: download from dataURL -> convert dataURL ->- } else if (url.startsWith("data:")) {- e.preventDefault();- e.stopImmediatePropagation();- downloadFromDataUri(url, filename);- }- },- true,- );-- return anchorEle;- };- }-// process special download protocol['data:','blob:']const isSpecialDownload = (url) =>["blob", "data"].some((protocol) => url.startsWith(protocol));@@ -587,11 +556,16 @@ document.addEventListener("DOMContentLoaded", () => {return;}- // Process download links for Rust to handle.- if (- isDownloadRequired(absoluteUrl, anchorElement, e) &&- !isSpecialDownload(absoluteUrl)- ) {+ // Process download links.+ if (isDownloadRequired(absoluteUrl, anchorElement, e)) {+ // Let the browser download blob:/data: URLs natively; the Rust+ // on_download handler saves them to the Downloads folder. Routing them+ // through the IPC fails on strict-CSP sites (e.g. Gemini), whose+ // connect-src blocks the IPC origin, and on downloads triggered from a+ // sandboxed iframe where the IPC can't be reached.+ if (isSpecialDownload(absoluteUrl)) {+ return;+ }e.preventDefault();e.stopImmediatePropagation();const userLanguage = getUserLanguage();@@ -626,7 +600,6 @@ document.addEventListener("DOMContentLoaded", () => {document.addEventListener("click", detectAnchorElementClick, true);collectUrlToBlobs();- detectDownloadByCreateAnchor();// Rewrite the window.open function.const originalWindowOpen = window.open;diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 01c1434e..c4c90ad9 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -43,7 +43,7 @@ pub fn run_app() {let start_to_tray = pake_config.windows[0].start_to_tray && show_system_tray; // Only valid when tray is enabledlet multi_instance = pake_config.multi_instance;let multi_window = pake_config.multi_window;- let enable_find = pake_config.windows[0].enable_find;+ let _enable_find = pake_config.windows[0].enable_find;let window_state_plugin = WindowStatePlugin::default().with_state_flags(if init_fullscreen {@@ -99,7 +99,7 @@ pub fn run_app() {// --- Menu Construction Start ---#[cfg(target_os = "macos")]{- app::menu::set_app_menu(app.app_handle(), multi_window, enable_find)?;+ app::menu::set_app_menu(app.app_handle(), multi_window, _enable_find)?;// Event Handling for Custom Menu Itemapp.on_menu_event(move |app_handle, event| {