@@ -3,19 +3,39 @@ import { type Request } from "@gopeed/types"
33import contentDisposition from "content-disposition"
44import path from "path"
55
6- import { getPort } from "@plasmohq/messaging/background"
76import { Storage } from "@plasmohq/storage"
87
98import { requestServerSelection } from "~background/messages/api/select-server"
109import { skip as pressToSkip } from "~background/messages/api/skip"
1110import { STORAGE_SETTINGS } from "~constants"
1211import { getFullUrl } from "~options/components/RemoteSettings"
1312import { defaultSettings , type Settings } from "~options/types"
14- import type { Server } from "~types"
1513import { getMergedSettings } from "~util/settings"
1614
1715export { }
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 }
0 commit comments