Skip to content

Commit

Permalink
add attachment download event handler (baseline)
Browse files Browse the repository at this point in the history
  • Loading branch information
martgil committed Oct 16, 2024
1 parent 2fdeba4 commit 07a96bf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
10 changes: 6 additions & 4 deletions extension/js/common/browser/browser-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export namespace Bm {
export type PgpBlockRetry = { frameId: string; messageSender: Dest };
export type PgpBlockReady = { frameId: string; messageSender: Dest };
export type ThunderbirdOpenPassphraseDialog = { acctEmail: string; longids: string };
export type ThunderbirdAttachmentDownload = { attachment: messenger.messages.MessageAttachment };

export namespace Res {
export type GetActiveTabInfo = {
Expand All @@ -114,7 +115,7 @@ export namespace Bm {
export type ExpirationCacheGet<V> = Promise<V | undefined>;
export type ExpirationCacheSet = Promise<void>;
export type ExpirationCacheDeleteExpired = Promise<void>;
export type ThunderbirdGetActiveTabInfo = number | undefined;
export type ThunderbirdAttachmentDownload = Promise<void>;
export type ThunderbirdGetCurrentUser = string | undefined;
export type ThunderbirdMsgGet = { attachments: messenger.messages.MessageAttachment[]; messagePart: messenger.messages.MessagePart };
export type ThunderbirdOpenPassphraseDialog = Promise<void>;
Expand All @@ -135,7 +136,7 @@ export namespace Bm {
| ExpirationCacheDeleteExpired
| AjaxGmailAttachmentGetChunk
| ConfirmationResult
| ThunderbirdGetActiveTabInfo
| ThunderbirdAttachmentDownload
| ThunderbirdMsgGet;
}

Expand Down Expand Up @@ -177,6 +178,7 @@ export namespace Bm {
| PgpBlockReady
| PgpBlockRetry
| ConfirmationResult
| ThunderbirdAttachmentDownload
| ThunderbirdOpenPassphraseDialog
| Ajax;

Expand Down Expand Up @@ -241,8 +243,8 @@ export class BrowserMsg {
BrowserMsg.sendAwait(undefined, 'expirationCacheSet', bm, true) as Promise<Bm.Res.ExpirationCacheSet>,
expirationCacheDeleteExpired: (bm: Bm.ExpirationCacheDeleteExpired) =>
BrowserMsg.sendAwait(undefined, 'expirationCacheDeleteExpired', bm, true) as Promise<Bm.Res.ExpirationCacheDeleteExpired>,
thunderbirdGetActiveTabInfo: () =>
BrowserMsg.sendAwait(undefined, 'thunderbirdGetActiveTabInfo', undefined, true) as Promise<Bm.Res.ThunderbirdGetActiveTabInfo>,
thunderbirdAttachmentDownload: (bm: Bm.ThunderbirdAttachmentDownload) =>
BrowserMsg.sendAwait(undefined, 'thunderbirdAttachmentDownload', bm, true) as Promise<Bm.Res.ThunderbirdAttachmentDownload>,
thunderbirdGetCurrentUser: () =>
BrowserMsg.sendAwait(undefined, 'thunderbirdGetCurrentUser', undefined, true) as Promise<Bm.Res.ThunderbirdGetCurrentUser>,
thunderbirdMsgGet: () => BrowserMsg.sendAwait(undefined, 'thunderbirdMsgGet', undefined, true) as Promise<Bm.Res.ThunderbirdMsgGet>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
};

private downloadThunderbirdAttachmentHandler = async (attachment: messenger.messages.MessageAttachment) => {
console.log('debug:', attachment);
const messageId = await BrowserMsg.send.bg.await.thunderbirdGetActiveTabInfo();
console.log(messageId);
await BrowserMsg.send.bg.await.thunderbirdAttachmentDownload({ attachment });
};

private isCleartextMsg = (): boolean => {
Expand Down
2 changes: 1 addition & 1 deletion extension/js/service_worker/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ console.info('background.js service worker starting');
if (Catch.isThunderbirdMail()) {
BgHandlers.thunderbirdSecureComposeHandler();
await BgHandlers.thunderbirdContentScriptRegistration();
BrowserMsg.bgAddListener('thunderbirdGetActiveTabInfo', BgHandlers.thunderbirdGetActiveTabInfo);
BrowserMsg.bgAddListener('thunderbirdGetCurrentUser', BgHandlers.thunderbirdGetCurrentUserHandler);
BrowserMsg.bgAddListener('thunderbirdMsgGet', BgHandlers.thunderbirdMsgGetHandler);
BrowserMsg.bgAddListener('thunderbirdAttachmentDownload', BgHandlers.thunderbirdAttachmentDownload);
BrowserMsg.bgAddListener('thunderbirdOpenPassphraseDialog', BgHandlers.thunderbirdOpenPassphraseDialog);
}
})().catch(Catch.reportErr);
15 changes: 12 additions & 3 deletions extension/js/service_worker/bg-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,18 @@ export class BgHandlers {
});
};

public static thunderbirdGetActiveTabInfo = async (): Promise<Bm.Res.ThunderbirdGetActiveTabInfo> => {
const tabs = await messenger.tabs.query({ active: true, currentWindow: true });
return tabs[0].id;
public static thunderbirdAttachmentDownload = async (r: Bm.ThunderbirdAttachmentDownload): Promise<Bm.Res.ThunderbirdAttachmentDownload> => {
const [tab] = await messenger.mailTabs.query({ active: true, currentWindow: true });
if (tab.id) {
const downloadableAttachment = await messenger.messages.getAttachmentFile(tab.id, r.attachment.partName);
const fileUrl = URL.createObjectURL(downloadableAttachment);
await browser.downloads.download({
url: fileUrl,
filename: r.attachment.name,
saveAs: true,
});
URL.revokeObjectURL(fileUrl);
}
};

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

0 comments on commit 07a96bf

Please sign in to comment.