Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: min file size filter on firefox #57

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function downloadFilter(info: DownloadInfo, settings: Settings): boolean {
settings.minFileSize.value > 0 &&
info.filesize > 0
) {
if (info.filesize < settings.minFileSize.value) {
if (info.filesize < settings.minFileSize.value * 1024 * 1024) {
return false
}
}
Expand All @@ -171,6 +171,9 @@ function downloadHandler(
// chrome.downloads.onDeterminingFilename only available in Chrome
const downloadEvent =
chrome.downloads.onDeterminingFilename || chrome.downloads.onCreated
// In Firefox, the download interception logic will be triggered twice, the order is onHeadersReceived -> onCreated, so a variable is needed to skip the onCreated event to avoid duplicate processing of download tasks.
// PS: Why not use the onCreated event uniformly? Because the onCreated event cannot get the size of the downloaded file in Firefox.
let downloadEventSkip = false

downloadEvent.addListener(async function (item) {
const info: DownloadInfo = {
Expand All @@ -181,6 +184,10 @@ downloadEvent.addListener(async function (item) {
referrer: item.referrer,
cookieStoreId: (item as any).cookieStoreId
}
if (isFirefox && downloadEventSkip) {
downloadEventSkip = false
return
}

if (!downloadFilter(info, settingsCache)) {
return
Expand Down Expand Up @@ -218,14 +225,18 @@ function checkContentDisposition(
if (isFirefox) {
chrome.webRequest.onHeadersReceived.addListener(
function (res) {
if (res.statusCode !== 200) {
if (res.statusCode !== 200 || res.type == "xmlhttprequest") {
return
}

const contentDispositionValue = checkContentDisposition(res)
if (!contentDispositionValue) {
return
}

// Skip the onCreated event to avoid duplicate processing of download tasks.
downloadEventSkip = true

let filename = ""
// Parse filename from content-disposition
if (contentDispositionValue) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"cookies",
"webRequest",
"webRequestBlocking",
"*://*/*",
"nativeMessaging"
],
"browser_specific_settings": {
Expand Down