@@ -36,22 +36,62 @@ async function showNativeNotification(type: "success" | "error" | "warning" | "i
3636 }
3737}
3838
39- /* function initContextMenus() {
39+ function initContextMenus ( ) {
40+ // Create a single context menu for all supported contexts
4041 chrome . contextMenus . create ( {
41- id: "sniff ",
42- title: "Download All Resource" ,
43- contexts: ["page ", "selection ", "link ", "action "]
42+ id : "gopeed-download " ,
43+ title : chrome . i18n . getMessage ( "context_menu_download" ) ,
44+ contexts : [ "link " , "image " , "video " , "audio " ]
4445 } )
4546
46- chrome.contextMenus.onClicked.addListener((info, tab) => {
47- console.log(info, tab)
48- const filePath = sniffer.split("/").pop().split("?")[0]
49- chrome.scripting.executeScript({
50- target: { tabId: tab.id },
51- files: [filePath]
52- })
47+ // Handle context menu clicks
48+ chrome . contextMenus . onClicked . addListener ( async ( info , tab ) => {
49+ // Determine URL: linkUrl for links, srcUrl for images/videos/audio
50+ const url = info . srcUrl || info . linkUrl
51+
52+ if ( ! url ) {
53+ console . error ( "No URL found for context menu download" )
54+ return
55+ }
56+
57+ // Extract filename from URL
58+ let filename : string
59+ try {
60+ const urlObj = new URL ( url )
61+ filename = path . basename ( urlObj . pathname ) || "unknown"
62+ } catch ( e ) {
63+ filename = "unknown"
64+ }
65+
66+ // Get current settings
67+ const settings = await refreshSettings ( )
68+ const isRunning = await refreshIsRunning ( )
69+
70+ // Create download info object
71+ const downloadInfo : DownloadInfo = {
72+ url : url ,
73+ filename : filename ,
74+ filesize : 0 , // Unknown file size for context menu downloads
75+ ua : navigator . userAgent ,
76+ referrer : tab ?. url || url ,
77+ cookieStoreId : ( tab as any ) ?. cookieStoreId
78+ }
79+
80+ // Get download handler and execute
81+ const handler = downloadHandler ( downloadInfo , settings , isRunning )
82+ if ( ! handler ) {
83+ await showNativeNotification (
84+ "error" ,
85+ chrome . i18n . getMessage ( "notification_download_failed" ) ,
86+ chrome . i18n . getMessage ( "notification_no_handler" )
87+ )
88+ return
89+ }
90+
91+ // Execute the download
92+ await handler ( )
5393 } )
54- } */
94+ }
5595
5696let connectNativePort : chrome . runtime . Port | null = null
5797
@@ -129,6 +169,9 @@ chrome.runtime.onStartup &&
129169 } , 3000 )
130170 await refreshSettings ( )
131171 await refreshIsRunning ( )
172+
173+ // Initialize context menus
174+ initContextMenus ( )
132175} ) ( )
133176
134177interface DownloadInfo {
0 commit comments