Skip to content

Commit 4173ef9

Browse files
committed
feat: Add revokeObjectURL functionality and enhance image processing with type handling
1 parent c79f2dc commit 4173ef9

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

scripts/background.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const MESSAGE_ACTIONS = {
4848
SHOW_SUCCESS_TOAST: "showDownloadSuccessToast",
4949
SHOW_ERROR_TOAST: "showDownloadErrorToast",
5050
SHOW_LOADING_TOAST: "showLoadingToast",
51+
DELETE_OBJECT_URL: "deleteObjectURL",
5152
};
5253

5354
// ============================================================================
@@ -216,6 +217,12 @@ async function handleStorageChanges(changes) {
216217
// ============================================================================
217218
// EVENT HANDLERS
218219
// ============================================================================
220+
async function revokeObjectURL(url, tabId) {
221+
browser.tabs.sendMessage(tabId, {
222+
action: MESSAGE_ACTIONS.DELETE_OBJECT_URL,
223+
message: { url },
224+
});
225+
}
219226

220227
async function handleContextMenuClick(info, tab) {
221228
const menuItemId = info.menuItemId;
@@ -232,8 +239,11 @@ async function handleContextMenuClick(info, tab) {
232239
return;
233240
}
234241

235-
const { url, fileExtension } = resolvedImage;
236-
downloadImageToLocal(url, folderSuffix, fileExtension, tab?.id);
242+
const { url, fileExtension, type } = resolvedImage;
243+
await downloadImageToLocal(url, folderSuffix, fileExtension, tab?.id);
244+
if (type == "blob" || type == "data") {
245+
await revokeObjectURL(url, tab?.id);
246+
}
237247
return;
238248
}
239249

@@ -244,7 +254,10 @@ async function handleContextMenuClick(info, tab) {
244254
return;
245255
}
246256

247-
saveImageToDropbox(resolvedImage.url, tab?.id);
257+
await saveImageToDropbox(resolvedImage.url, tab?.id);
258+
if (resolvedImage.type === "blob" || resolvedImage.type === "data") {
259+
await revokeObjectURL(resolvedImage.url, tab?.id);
260+
}
248261
return;
249262
}
250263
}

scripts/content_script.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const MESSAGE_ACTIONS = {
1010
DOWNLOAD_IMAGE_AT_POSITION: "download-image",
1111
CONTEXT_MENU_POSITION: "context-menu-position",
1212
SHOW_LOADING_TOAST: "showLoadingToast",
13+
DELETE_OBJECT_URL: "deleteObjectURL",
1314
};
1415

1516
const TOAST_TYPES = {
@@ -445,6 +446,16 @@ async function handleDownloadImageMessage(message) {
445446
return createImageDownloadResponse(url, "image");
446447
}
447448

449+
async function revokeObjectURL(data) {
450+
const { message } = data;
451+
if (!message || !message.url) {
452+
return null;
453+
}
454+
455+
URL.revokeObjectURL(message.url);
456+
return null;
457+
}
458+
448459
function isToastMessage(action) {
449460
return (
450461
action === MESSAGE_ACTIONS.SHOW_SUCCESS_TOAST ||
@@ -474,6 +485,9 @@ function handleRuntimeMessage(message, sender, sendResponse) {
474485
case MESSAGE_ACTIONS.DOWNLOAD_IMAGE_AT_POSITION:
475486
return handleDownloadImageMessage(message);
476487

488+
case MESSAGE_ACTIONS.DELETE_OBJECT_URL:
489+
return revokeObjectURL(message);
490+
477491
default:
478492
return undefined;
479493
}
@@ -499,8 +513,12 @@ function handleContextMenu(event) {
499513
// EVENT LISTENERS SETUP
500514
// ============================================================================
501515
function setupEventListeners() {
502-
document.addEventListener("dblclick", handleDoubleClickEvent);
503-
document.addEventListener("contextmenu", handleContextMenu);
516+
document.addEventListener("dblclick", handleDoubleClickEvent, {
517+
passive: true,
518+
});
519+
document.addEventListener("contextmenu", handleContextMenu, {
520+
passive: true,
521+
});
504522
browser.runtime.onMessage.addListener(handleRuntimeMessage);
505523
}
506524

scripts/download.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,22 @@ async function processImageUrl(imageUrl, type, tabId, tab) {
8686
return {
8787
url: blobUrl,
8888
fileExtension: blobData.extension,
89+
type: type,
8990
};
9091

9192
case "data":
9293
const response = await browser.tabs.sendMessage(tabId, {
9394
action: "FETCH_DATA_URL",
9495
url: imageUrl,
96+
type: type,
9597
});
9698

9799
if (response.success) {
98100
const dataUrl = response.dataUrl;
99101
return {
100102
url: dataUrl,
101103
fileExtension: await getFileExtension(dataUrl),
104+
type: type,
102105
};
103106
}
104107

@@ -107,13 +110,15 @@ async function processImageUrl(imageUrl, type, tabId, tab) {
107110
return {
108111
url: absoluteUrl,
109112
fileExtension: await getFileExtension(absoluteUrl),
113+
type: type,
110114
};
111115

112116
case "http":
113117
default:
114118
return {
115119
url: imageUrl,
116120
fileExtension: await getFileExtension(imageUrl),
121+
type: type,
117122
};
118123
}
119124
}

0 commit comments

Comments
 (0)