Skip to content

Commit f292658

Browse files
authored
fix: min file size filter on firefox (#57)
1 parent d08f2f9 commit f292658

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

background/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function downloadFilter(info: DownloadInfo, settings: Settings): boolean {
146146
settings.minFileSize.value > 0 &&
147147
info.filesize > 0
148148
) {
149-
if (info.filesize < settings.minFileSize.value) {
149+
if (info.filesize < settings.minFileSize.value * 1024 * 1024) {
150150
return false
151151
}
152152
}
@@ -171,6 +171,9 @@ function downloadHandler(
171171
// chrome.downloads.onDeterminingFilename only available in Chrome
172172
const downloadEvent =
173173
chrome.downloads.onDeterminingFilename || chrome.downloads.onCreated
174+
// 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.
175+
// PS: Why not use the onCreated event uniformly? Because the onCreated event cannot get the size of the downloaded file in Firefox.
176+
let downloadEventSkip = false
174177

175178
downloadEvent.addListener(async function (item) {
176179
const info: DownloadInfo = {
@@ -181,6 +184,10 @@ downloadEvent.addListener(async function (item) {
181184
referrer: item.referrer,
182185
cookieStoreId: (item as any).cookieStoreId
183186
}
187+
if (isFirefox && downloadEventSkip) {
188+
downloadEventSkip = false
189+
return
190+
}
184191

185192
if (!downloadFilter(info, settingsCache)) {
186193
return
@@ -218,14 +225,18 @@ function checkContentDisposition(
218225
if (isFirefox) {
219226
chrome.webRequest.onHeadersReceived.addListener(
220227
function (res) {
221-
if (res.statusCode !== 200) {
228+
if (res.statusCode !== 200 || res.type == "xmlhttprequest") {
222229
return
223230
}
231+
224232
const contentDispositionValue = checkContentDisposition(res)
225233
if (!contentDispositionValue) {
226234
return
227235
}
228236

237+
// Skip the onCreated event to avoid duplicate processing of download tasks.
238+
downloadEventSkip = true
239+
229240
let filename = ""
230241
// Parse filename from content-disposition
231242
if (contentDispositionValue) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"cookies",
7070
"webRequest",
7171
"webRequestBlocking",
72+
"*://*/*",
7273
"nativeMessaging"
7374
],
7475
"browser_specific_settings": {

0 commit comments

Comments
 (0)