Skip to content

Commit 2556e27

Browse files
authored
Merge pull request #31 from cloudoptlab/fix/2.x/fix-downloading-protection-switch
Fix download protection switch issue
2 parents f28ec8a + 490a6ce commit 2556e27

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/background/download.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@ import * as coreConfig from '../core/config'
22
import * as grade from '../core/grade'
33
import * as notification from '../core/notification'
44
import * as i18n from '../core/i18n'
5+
import * as message from './message'
56

6-
let listenerAdded: boolean = false
77
let downloadSafetyEnabled: boolean = false
88

9-
export async function refresh() {
9+
async function refreshConfig(): Promise<void> {
1010
const config = await coreConfig.get()
1111
downloadSafetyEnabled = config.safeDownload
12+
}
13+
14+
export async function initialize() {
15+
refreshConfig()
16+
message.addListener({
17+
type: 'refresh-config',
18+
callback: async (msg, sender, sendResponse) => {
19+
refreshConfig()
20+
sendResponse({})
21+
return true
22+
},
23+
})
1224

13-
if (!listenerAdded && chrome.downloads) {
25+
if (chrome.downloads) {
1426
chrome.downloads.onCreated.addListener(async (downloadItem) => {
1527
if (!downloadSafetyEnabled) {
1628
return
@@ -27,8 +39,7 @@ export async function refresh() {
2739
chrome.downloads.cancel(downloadItem.id)
2840
}
2941
})
30-
listenerAdded = true
3142
}
3243
}
3344

34-
refresh()
45+
initialize()

0 commit comments

Comments
 (0)