-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
29 lines (27 loc) · 858 Bytes
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Listen for messages from content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "downloadImage") {
const { url, filename } = request;
// Initiate the download
chrome.downloads.download(
{
url: url,
filename: filename,
saveAs: true, // Prompt the user with the Save As dialog
},
(downloadId) => {
if (chrome.runtime.lastError) {
console.error(`Download failed: ${chrome.runtime.lastError.message}`);
sendResponse({
success: false,
message: chrome.runtime.lastError.message,
});
} else {
console.log(`Download started with ID: ${downloadId}`);
sendResponse({ success: true, downloadId: downloadId });
}
}
);
return true;
}
});