Skip to content

Commit

Permalink
add new background message for obtaining current tab
Browse files Browse the repository at this point in the history
  • Loading branch information
martgil committed Oct 16, 2024
1 parent aa4feac commit 2fdeba4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions extension/js/common/browser/browser-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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 ThunderbirdGetCurrentUser = string | undefined;
export type ThunderbirdMsgGet = { attachments: messenger.messages.MessageAttachment[]; messagePart: messenger.messages.MessagePart };
export type ThunderbirdOpenPassphraseDialog = Promise<void>;
Expand All @@ -134,6 +135,7 @@ export namespace Bm {
| ExpirationCacheDeleteExpired
| AjaxGmailAttachmentGetChunk
| ConfirmationResult
| ThunderbirdGetActiveTabInfo
| ThunderbirdMsgGet;
}

Expand Down Expand Up @@ -239,6 +241,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>,
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 @@ -120,16 +120,18 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
attachmentDownloadBtn
.addClass('attachment_download')
.text('download')
.on('click', () => {
this.downloadThunderbirdAttachmentHandler(attachment);
.on('click', async () => {
await this.downloadThunderbirdAttachmentHandler(attachment);
});
attachmentHtmlRoot.append(attachmentFilename); // xss-escaped
attachmentHtmlRoot.append(attachmentDownloadBtn); // xss-escaped
return attachmentHtmlRoot;
};

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

private isCleartextMsg = (): boolean => {
Expand Down
1 change: 1 addition & 0 deletions extension/js/service_worker/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ 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('thunderbirdOpenPassphraseDialog', BgHandlers.thunderbirdOpenPassphraseDialog);
Expand Down
5 changes: 5 additions & 0 deletions extension/js/service_worker/bg-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ 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 thunderbirdGetCurrentUserHandler = async (): Promise<Bm.Res.ThunderbirdGetCurrentUser> => {
const [tab] = await messenger.tabs.query({ active: true, currentWindow: true });
if (tab.id) {
Expand Down

0 comments on commit 2fdeba4

Please sign in to comment.