Skip to content

Commit e17b0a6

Browse files
authored
feat: use native notification (#89)
1 parent 8e27137 commit e17b0a6

File tree

5 files changed

+48
-114
lines changed

5 files changed

+48
-114
lines changed

background/index.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,39 @@ import { type Request } from "@gopeed/types"
33
import contentDisposition from "content-disposition"
44
import path from "path"
55

6-
import { getPort } from "@plasmohq/messaging/background"
76
import { Storage } from "@plasmohq/storage"
87

98
import { requestServerSelection } from "~background/messages/api/select-server"
109
import { skip as pressToSkip } from "~background/messages/api/skip"
1110
import { STORAGE_SETTINGS } from "~constants"
1211
import { getFullUrl } from "~options/components/RemoteSettings"
1312
import { defaultSettings, type Settings } from "~options/types"
14-
import type { Server } from "~types"
1513
import { getMergedSettings } from "~util/settings"
1614

1715
export { }
1816

17+
// Native notification utility function
18+
async function showNativeNotification(type: "success" | "error" | "warning" | "info" = "success", title: string, message: string) {
19+
const notificationId = `gopeed-${Date.now()}`
20+
21+
try {
22+
await chrome.notifications.create(notificationId, {
23+
type: "basic",
24+
iconUrl: chrome.runtime.getURL("assets/icon.png"),
25+
title: title,
26+
message: message,
27+
priority: type === "error" ? 2 : 1
28+
})
29+
30+
// Auto-clear notification after 6 seconds
31+
setTimeout(() => {
32+
chrome.notifications.clear(notificationId)
33+
}, 6000)
34+
} catch (error) {
35+
console.error("Failed to create notification:", error)
36+
}
37+
}
38+
1939
/* function initContextMenus() {
2040
chrome.contextMenus.create({
2141
id: "sniff",
@@ -424,12 +444,11 @@ async function executeDownloadTask(
424444

425445
// Show notification based on confirmBeforeDownload setting
426446
if (settings.confirmBeforeDownload || !success) {
427-
const port = getPort("notify")
428-
port.postMessage({
429-
type: notificationType,
430-
title: notificationTitle,
431-
message: notificationMessage
432-
})
447+
await showNativeNotification(
448+
notificationType as "success" | "error",
449+
notificationTitle,
450+
notificationMessage
451+
)
433452
}
434453

435454
return success
@@ -470,12 +489,11 @@ function createDownloadTask(
470489

471490
// Show notification based on confirmBeforeDownload setting or if there's an error
472491
if (settings.confirmBeforeDownload || !success) {
473-
const port = getPort("notify")
474-
port.postMessage({
475-
type: notificationType,
476-
title: notificationTitle,
477-
message: notificationMessage
478-
})
492+
await showNativeNotification(
493+
notificationType as "success" | "error",
494+
notificationTitle,
495+
notificationMessage
496+
)
479497
}
480498
return success
481499
}
@@ -521,22 +539,20 @@ function handleNativeDownload(
521539
})
522540

523541
if (!settings.confirmBeforeDownload) {
524-
const port = getPort("notify")
525-
port.postMessage({
526-
type: "success",
527-
title: chrome.i18n.getMessage("notification_create_success"),
528-
message: chrome.i18n.getMessage("notification_native_success_message")
529-
})
542+
await showNativeNotification(
543+
"success",
544+
chrome.i18n.getMessage("notification_create_success"),
545+
chrome.i18n.getMessage("notification_native_success_message")
546+
)
530547
}
531548
} catch (e) {
532549
console.error(e)
533550
if (!settings.confirmBeforeDownload) {
534-
const port = getPort("notify")
535-
port.postMessage({
536-
type: "error",
537-
title: chrome.i18n.getMessage("notification_create_error"),
538-
message: chrome.i18n.getMessage("notification_native_error_message")
539-
})
551+
await showNativeNotification(
552+
"error",
553+
chrome.i18n.getMessage("notification_create_error"),
554+
chrome.i18n.getMessage("notification_native_error_message")
555+
)
540556
}
541557
}
542558
}

background/ports/notify.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

contents/notify.tsx

Lines changed: 0 additions & 77 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"permissions": [
7171
"downloads",
7272
"cookies",
73-
"nativeMessaging"
73+
"nativeMessaging",
74+
"notifications"
7475
],
7576
"overrides": {
7677
"firefox": {
@@ -80,7 +81,8 @@
8081
"webRequest",
8182
"webRequestBlocking",
8283
"*://*/*",
83-
"nativeMessaging"
84+
"nativeMessaging",
85+
"notifications"
8486
],
8587
"browser_specific_settings": {
8688
"gecko": {
@@ -90,4 +92,4 @@
9092
}
9193
}
9294
}
93-
}
95+
}

types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
export interface Server {
1+
interface Server {
22
protocol: "http" | "https"
33
url: string
44
token?: string
55
}
6-
7-
type PlatformOS = "mac" | "windows" | "linux"

0 commit comments

Comments
 (0)