Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: route same-origin Windows downloads through WebView2

WaveYo49a38a6ae675
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index e8e9e08a..3a6e6b6e 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -727,6 +727,17 @@ document.addEventListener("DOMContentLoaded", () => {
};
function downloadFile(url, filename) {
+ // download attributes reliably force same-origin resources to save instead
+ // of navigating. Cross-origin resources retain the HTTP fallback because
+ // browsers can ignore their download attribute (including PDFs and images).
+ if (
+ window.pakeNativeDownloads === true &&
+ (isSpecialDownload(url) ||
+ new URL(url, window.location.href).origin === window.location.origin)
+ ) {
+ triggerNativeDownload(url, filename);
+ return Promise.resolve();
+ }
return invoke("download_file", {
params: {
url,
diff --git a/tests/unit/event-link-guard.test.js b/tests/unit/event-link-guard.test.js
index 9ebf0280..270a9af6 100644
--- a/tests/unit/event-link-guard.test.js
+++ b/tests/unit/event-link-guard.test.js
@@ -8,6 +8,7 @@ function loadEventHelpers({
userAgent = "Mozilla/5.0",
initialZoom = null,
downloadError = null,
+ nativeDownloads = false,
} = {}) {
const source = ["link_policy.js", "event.js"]
.map((file) =>
@@ -108,6 +109,7 @@ function loadEventHelpers({
isAuthLink: () => false,
isAuthPopup: () => false,
pakeConfig: {},
+ pakeNativeDownloads: nativeDownloads,
},
document: {
addEventListener: registerListener,
@@ -172,6 +174,51 @@ function makeClickEvent(anchor) {
}
describe("event link guard", () => {
+ it("uses native Windows downloads once for same-origin files without IPC", () => {
+ const context = loadEventHelpers({
+ withTauri: true,
+ nativeDownloads: true,
+ });
+ runDomReady(context);
+ const anchor = makeAnchor("https://example.com/report.pdf", "_self");
+ anchor.download = "report.pdf";
+ const click = makeClickEvent(anchor);
+ getClickGuard(context)(click);
+ expect(click.preventDefault).toHaveBeenCalledOnce();
+ expect(context.nativeDownloadClicks).toEqual([[anchor.href, "report.pdf"]]);
+ expect(
+ context.invokeCalls.filter(([command]) => command === "download_file"),
+ ).toEqual([]);
+ });
+
+ it("keeps cross-origin forced downloads on HTTP when the browser may preview them", () => {
+ const context = loadEventHelpers({
+ withTauri: true,
+ nativeDownloads: true,
+ });
+ runDomReady(context);
+ const anchor = makeAnchor("https://cdn.example.com/report.pdf", "_self");
+ anchor.download = "report.pdf";
+ getClickGuard(context)(makeClickEvent(anchor));
+ expect(context.nativeDownloadClicks).toEqual([]);
+ expect(
+ context.invokeCalls.filter(([command]) => command === "download_file"),
+ ).toEqual([
+ [
+ "download_file",
+ {
+ params: {
+ url: anchor.href,
+ filename: "report.pdf",
+ language: "en-US",
+ user_agent: "Mozilla/5.0",
+ page_url: "https://example.com/app",
+ },
+ },
+ ],
+ ]);
+ });
+
it.each([
'download_file not allowed on window "pake", webview "pake", URL: https://other.example/',
"Command download_file not allowed by ACL",