Skip to content

Commit 07a96bf

Browse files
committed
add attachment download event handler (baseline)
1 parent 2fdeba4 commit 07a96bf

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

extension/js/common/browser/browser-msg.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export namespace Bm {
9898
export type PgpBlockRetry = { frameId: string; messageSender: Dest };
9999
export type PgpBlockReady = { frameId: string; messageSender: Dest };
100100
export type ThunderbirdOpenPassphraseDialog = { acctEmail: string; longids: string };
101+
export type ThunderbirdAttachmentDownload = { attachment: messenger.messages.MessageAttachment };
101102

102103
export namespace Res {
103104
export type GetActiveTabInfo = {
@@ -114,7 +115,7 @@ export namespace Bm {
114115
export type ExpirationCacheGet<V> = Promise<V | undefined>;
115116
export type ExpirationCacheSet = Promise<void>;
116117
export type ExpirationCacheDeleteExpired = Promise<void>;
117-
export type ThunderbirdGetActiveTabInfo = number | undefined;
118+
export type ThunderbirdAttachmentDownload = Promise<void>;
118119
export type ThunderbirdGetCurrentUser = string | undefined;
119120
export type ThunderbirdMsgGet = { attachments: messenger.messages.MessageAttachment[]; messagePart: messenger.messages.MessagePart };
120121
export type ThunderbirdOpenPassphraseDialog = Promise<void>;
@@ -135,7 +136,7 @@ export namespace Bm {
135136
| ExpirationCacheDeleteExpired
136137
| AjaxGmailAttachmentGetChunk
137138
| ConfirmationResult
138-
| ThunderbirdGetActiveTabInfo
139+
| ThunderbirdAttachmentDownload
139140
| ThunderbirdMsgGet;
140141
}
141142

@@ -177,6 +178,7 @@ export namespace Bm {
177178
| PgpBlockReady
178179
| PgpBlockRetry
179180
| ConfirmationResult
181+
| ThunderbirdAttachmentDownload
180182
| ThunderbirdOpenPassphraseDialog
181183
| Ajax;
182184

@@ -241,8 +243,8 @@ export class BrowserMsg {
241243
BrowserMsg.sendAwait(undefined, 'expirationCacheSet', bm, true) as Promise<Bm.Res.ExpirationCacheSet>,
242244
expirationCacheDeleteExpired: (bm: Bm.ExpirationCacheDeleteExpired) =>
243245
BrowserMsg.sendAwait(undefined, 'expirationCacheDeleteExpired', bm, true) as Promise<Bm.Res.ExpirationCacheDeleteExpired>,
244-
thunderbirdGetActiveTabInfo: () =>
245-
BrowserMsg.sendAwait(undefined, 'thunderbirdGetActiveTabInfo', undefined, true) as Promise<Bm.Res.ThunderbirdGetActiveTabInfo>,
246+
thunderbirdAttachmentDownload: (bm: Bm.ThunderbirdAttachmentDownload) =>
247+
BrowserMsg.sendAwait(undefined, 'thunderbirdAttachmentDownload', bm, true) as Promise<Bm.Res.ThunderbirdAttachmentDownload>,
246248
thunderbirdGetCurrentUser: () =>
247249
BrowserMsg.sendAwait(undefined, 'thunderbirdGetCurrentUser', undefined, true) as Promise<Bm.Res.ThunderbirdGetCurrentUser>,
248250
thunderbirdMsgGet: () => BrowserMsg.sendAwait(undefined, 'thunderbirdMsgGet', undefined, true) as Promise<Bm.Res.ThunderbirdMsgGet>,

extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
129129
};
130130

131131
private downloadThunderbirdAttachmentHandler = async (attachment: messenger.messages.MessageAttachment) => {
132-
console.log('debug:', attachment);
133-
const messageId = await BrowserMsg.send.bg.await.thunderbirdGetActiveTabInfo();
134-
console.log(messageId);
132+
await BrowserMsg.send.bg.await.thunderbirdAttachmentDownload({ attachment });
135133
};
136134

137135
private isCleartextMsg = (): boolean => {

extension/js/service_worker/background.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ console.info('background.js service worker starting');
7373
if (Catch.isThunderbirdMail()) {
7474
BgHandlers.thunderbirdSecureComposeHandler();
7575
await BgHandlers.thunderbirdContentScriptRegistration();
76-
BrowserMsg.bgAddListener('thunderbirdGetActiveTabInfo', BgHandlers.thunderbirdGetActiveTabInfo);
7776
BrowserMsg.bgAddListener('thunderbirdGetCurrentUser', BgHandlers.thunderbirdGetCurrentUserHandler);
7877
BrowserMsg.bgAddListener('thunderbirdMsgGet', BgHandlers.thunderbirdMsgGetHandler);
78+
BrowserMsg.bgAddListener('thunderbirdAttachmentDownload', BgHandlers.thunderbirdAttachmentDownload);
7979
BrowserMsg.bgAddListener('thunderbirdOpenPassphraseDialog', BgHandlers.thunderbirdOpenPassphraseDialog);
8080
}
8181
})().catch(Catch.reportErr);

extension/js/service_worker/bg-handlers.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,18 @@ export class BgHandlers {
162162
});
163163
};
164164

165-
public static thunderbirdGetActiveTabInfo = async (): Promise<Bm.Res.ThunderbirdGetActiveTabInfo> => {
166-
const tabs = await messenger.tabs.query({ active: true, currentWindow: true });
167-
return tabs[0].id;
165+
public static thunderbirdAttachmentDownload = async (r: Bm.ThunderbirdAttachmentDownload): Promise<Bm.Res.ThunderbirdAttachmentDownload> => {
166+
const [tab] = await messenger.mailTabs.query({ active: true, currentWindow: true });
167+
if (tab.id) {
168+
const downloadableAttachment = await messenger.messages.getAttachmentFile(tab.id, r.attachment.partName);
169+
const fileUrl = URL.createObjectURL(downloadableAttachment);
170+
await browser.downloads.download({
171+
url: fileUrl,
172+
filename: r.attachment.name,
173+
saveAs: true,
174+
});
175+
URL.revokeObjectURL(fileUrl);
176+
}
168177
};
169178

170179
public static thunderbirdGetCurrentUserHandler = async (): Promise<Bm.Res.ThunderbirdGetCurrentUser> => {

0 commit comments

Comments
 (0)