Skip to content

Commit 84076d9

Browse files
committed
[chrome] add back download interception to isolatedframe
1 parent c517492 commit 84076d9

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

packages/chrome/src/Browser.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
animateDownloadFly,
1313
showDownloadsPopup,
1414
} from "./components/Omnibar/Omnibar";
15+
import type { RawDownload } from "./IsolatedFrame";
1516
export const pushTab = createDelegate<Tab>();
1617
export const popTab = createDelegate<Tab>();
1718
export const forceScreenshot = createDelegate<Tab>();
@@ -116,7 +117,7 @@ export class Browser extends StatefulClass {
116117
// });
117118
}
118119

119-
async startDownload(download: ScramjetDownload) {
120+
async startDownload(download: RawDownload) {
120121
this.downloadProgress = 0.1;
121122
let downloaded = 0;
122123
animateDownloadFly();

packages/chrome/src/IsolatedFrame.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import scramjetWASM from "../../scramjet/packages/core/dist/scramjet.wasm.wasm?url";
1717
import scramjetAll from "../../scramjet/packages/core/dist/scramjet.js?url";
1818
import injectScript from "../../inject/dist/inject.js?url";
19-
import { BareClient } from "@mercuryworkshop/bare-mux-custom";
19+
import { BareClient, type BareHeaders } from "@mercuryworkshop/bare-mux-custom";
2020
import { ElementType, type Handler, Parser } from "htmlparser2";
2121
import { type ChildNode, DomHandler, Element, Comment, Node } from "domhandler";
2222
import * as tldts from "tldts";
@@ -360,9 +360,12 @@ export class IsolatedFrame {
360360
}
361361
}
362362

363-
function isDownload(responseHeaders: any, destination: string): boolean {
363+
function isDownload(
364+
responseHeaders: BareHeaders,
365+
destination: string
366+
): boolean {
364367
if (["document", "iframe"].includes(destination)) {
365-
const header = responseHeaders["content-disposition"];
368+
const header = responseHeaders["content-disposition"]?.[0];
366369
if (header) {
367370
if (header === "inline") {
368371
return false; // force it to show in browser
@@ -383,7 +386,7 @@ function isDownload(responseHeaders: any, destination: string): boolean {
383386
"application/xml",
384387
"application/pdf",
385388
];
386-
const contentType = responseHeaders["content-type"]
389+
const contentType = responseHeaders["content-type"]?.[0]
387390
?.split(";")[0]
388391
.trim()
389392
.toLowerCase();
@@ -403,6 +406,14 @@ function isDownload(responseHeaders: any, destination: string): boolean {
403406
return false;
404407
}
405408

409+
export type RawDownload = {
410+
filename: string | null;
411+
url: string;
412+
type: string;
413+
body: BodyType;
414+
length: number;
415+
};
416+
406417
const methods = {
407418
async fetch(
408419
data: ScramjetFetchContext,
@@ -476,19 +487,30 @@ const methods = {
476487

477488
const fetchresponse = await controller.fetchHandler.handleFetch(data);
478489

490+
console.log(fetchresponse.headers);
479491
if (
480492
isDownload(fetchresponse.headers, data.destination) &&
481493
fetchresponse.status === 200
482494
) {
483495
let filename: string | null = null;
484-
const disp = fetchresponse.headers["content-disposition"];
496+
const disp = fetchresponse.headers["content-disposition"]?.[0];
485497
if (typeof disp === "string") {
486498
const filenameMatch = disp.match(/filename=["']?([^"';\n]*)["']?/i);
487499
if (filenameMatch && filenameMatch[1]) {
488500
filename = filenameMatch[1];
489501
}
490502
}
491-
const length = fetchresponse.headers["content-length"];
503+
const length = fetchresponse.headers["content-length"][0];
504+
505+
browser.startDownload({
506+
filename,
507+
url: unrewriteUrl(data.rawUrl, { prefix: controller.prefix } as any),
508+
type:
509+
fetchresponse.headers["content-type"][0] ||
510+
"application/octet-stream",
511+
length: parseInt(length),
512+
body: fetchresponse.body,
513+
});
492514

493515
// endless vortex reference
494516
await new Promise(() => {});

0 commit comments

Comments
 (0)