Skip to content

Commit f3c4cd4

Browse files
authored
feat: enhance context menu for downloads (#90)
1 parent e17b0a6 commit f3c4cd4

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

background/index.ts

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5696
let 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

134177
interface DownloadInfo {

locales/en/messages.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,14 @@
181181
},
182182
"go_to": {
183183
"message": "GO"
184+
},
185+
"context_menu_download": {
186+
"message": "Download with Gopeed"
187+
},
188+
"notification_download_failed": {
189+
"message": "Download Failed"
190+
},
191+
"notification_no_handler": {
192+
"message": "Unable to create download task"
184193
}
185194
}

locales/zh/messages.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,14 @@
181181
},
182182
"go_to": {
183183
"message": "前往"
184+
},
185+
"context_menu_download": {
186+
"message": "使用 Gopeed 下载"
187+
},
188+
"notification_download_failed": {
189+
"message": "下载失败"
190+
},
191+
"notification_no_handler": {
192+
"message": "无法创建下载任务"
184193
}
185194
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"downloads",
7272
"cookies",
7373
"nativeMessaging",
74-
"notifications"
74+
"notifications",
75+
"contextMenus"
7576
],
7677
"overrides": {
7778
"firefox": {
@@ -82,7 +83,8 @@
8283
"webRequestBlocking",
8384
"*://*/*",
8485
"nativeMessaging",
85-
"notifications"
86+
"notifications",
87+
"contextMenus"
8688
],
8789
"browser_specific_settings": {
8890
"gecko": {

0 commit comments

Comments
 (0)