Skip to content

Commit 36526b4

Browse files
authored
fix: forward all request headers (#104)
1 parent 07f65b3 commit 36526b4

2 files changed

Lines changed: 47 additions & 29 deletions

File tree

background/index.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ function initContextMenus() {
7171
const downloadInfo: DownloadInfo = {
7272
url: url,
7373
filename: filename,
74-
filesize: 0, // Unknown file size for context menu downloads
75-
ua: navigator.userAgent,
76-
referrer: tab?.url || url,
77-
cookieStoreId: (tab as any)?.cookieStoreId
74+
filesize: 0 // Unknown file size for context menu downloads
7875
}
7976

8077
// Get download handler and execute
@@ -182,19 +179,49 @@ chrome.runtime.onStartup &&
182179
// If map grows too large, clear it entirely as a safety measure
183180
downloadEventSkipMap.clear()
184181
}
182+
if (requestHeaderMap.size > 100) {
183+
requestHeaderMap.clear()
184+
}
185185
}, 30000)
186186
})()
187187

188188
interface DownloadInfo {
189189
url: string
190190
filename: string
191191
filesize: number
192-
ua?: string
193-
referrer?: string
194-
cookieStoreId?: string
195192
tabId?: number
196193
}
197194

195+
const requestHeaderMap = new Map<string, Record<string, string>>()
196+
197+
function storeRequestHeaders(details: chrome.webRequest.WebRequestHeadersDetails) {
198+
if (!details.requestHeaders || details.requestHeaders.length === 0) {
199+
return
200+
}
201+
202+
const requestHeaders = details.requestHeaders.reduce<Record<string, string>>(
203+
(acc, header) => {
204+
if (header.name && header.value) {
205+
acc[header.name] = header.value
206+
}
207+
return acc
208+
},
209+
{}
210+
)
211+
212+
if (Object.keys(requestHeaders).length > 0) {
213+
requestHeaderMap.set(details.url, requestHeaders)
214+
}
215+
}
216+
217+
function consumeRequestHeaders(url: string): Record<string, string> {
218+
const headers = requestHeaderMap.get(url) || {}
219+
if (requestHeaderMap.has(url)) {
220+
requestHeaderMap.delete(url)
221+
}
222+
return headers
223+
}
224+
198225
function downloadFilter(info: DownloadInfo, settings: Settings): boolean {
199226
if (info.url.startsWith("blob:") || info.url.startsWith("data:")) {
200227
return false
@@ -262,14 +289,17 @@ const downloadEvent =
262289
// Use a Map to track skip status per-download to avoid race conditions with multiple tabs
263290
const downloadEventSkipMap = new Map<string, boolean>()
264291

292+
chrome.webRequest.onBeforeSendHeaders.addListener(
293+
storeRequestHeaders,
294+
{ urls: ["<all_urls>"], types: ["main_frame", "sub_frame"] },
295+
isFirefox ? ["requestHeaders"] : ["requestHeaders", "extraHeaders"]
296+
)
297+
265298
downloadEvent.addListener(async function (item) {
266299
const info: DownloadInfo = {
267300
url: item.finalUrl || item.url,
268301
filename: item.filename,
269-
filesize: item.fileSize,
270-
ua: navigator.userAgent,
271-
referrer: item.referrer,
272-
cookieStoreId: (item as any).cookieStoreId
302+
filesize: item.fileSize
273303
}
274304
const downloadUrl = item.finalUrl || item.url
275305
if (isFirefox && downloadEventSkipMap.get(downloadUrl)) {
@@ -346,9 +376,6 @@ if (isFirefox) {
346376
url: res.url,
347377
filename,
348378
filesize,
349-
ua: navigator.userAgent,
350-
referrer: (res as any).originUrl,
351-
cookieStoreId: (res as any).cookieStoreId,
352379
tabId: res.tabId
353380
}
354381
if (!downloadFilter(info, settingsCache)) {
@@ -623,25 +650,15 @@ function handleNativeDownload(
623650
}
624651
}
625652

626-
function getCookie(url: string, storeId?: string) {
627-
return new Promise<string>((resolve) => {
628-
chrome.cookies.getAll({ url, storeId }, (cookies) => {
629-
resolve(
630-
cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join("; ")
631-
)
632-
})
633-
})
634-
}
635-
636653
async function toCreateRequest(info: DownloadInfo): Promise<Request> {
637-
const cookie = await getCookie(info.url, (info as any).cookieStoreId)
654+
const capturedHeaders = consumeRequestHeaders(info.url)
655+
// Force an identity response body to avoid encoded payloads that break multi-part downloading.
638656
return {
639657
url: info.url,
640658
extra: {
641659
header: {
642-
"User-Agent": navigator.userAgent,
643-
Cookie: cookie ? cookie : undefined,
644-
Referer: info.referrer ? info.referrer : undefined
660+
...capturedHeaders,
661+
"Accept-Encoding": "identity"
645662
}
646663
}
647664
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"permissions": [
7171
"downloads",
7272
"cookies",
73+
"webRequest",
7374
"nativeMessaging",
7475
"notifications",
7576
"contextMenus"
@@ -94,4 +95,4 @@
9495
}
9596
}
9697
}
97-
}
98+
}

0 commit comments

Comments
 (0)