From cc34e7f85b51c26709c3ea18a243065b27296f25 Mon Sep 17 00:00:00 2001 From: Roma Sosnovsky Date: Wed, 11 Sep 2024 09:47:53 +0300 Subject: [PATCH] code improvements --- extension/chrome/elements/passphrase.ts | 14 +++--- .../thunderbird-element-replacer.ts | 43 ++++++++----------- extension/js/service_worker/bg-handlers.ts | 6 +-- tooling/bundle-content-scripts.ts | 2 +- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/extension/chrome/elements/passphrase.ts b/extension/chrome/elements/passphrase.ts index 5572ed3849b..ec41fc03aa9 100644 --- a/extension/chrome/elements/passphrase.ts +++ b/extension/chrome/elements/passphrase.ts @@ -201,14 +201,12 @@ View.run( }; private closeDialogPageOpenedExternally = async () => { - if (window.top === window.self) { - if (Catch.isThunderbirdMail()) { - const currentTab = await messenger.tabs.query({ active: true, currentWindow: true }); - if (currentTab.length > 0) { - const tabId = currentTab[0].id; - if (tabId) { - await messenger.tabs.remove(tabId); - } + if (Catch.isThunderbirdMail() && window.top === window.self) { + const currentTab = await messenger.tabs.query({ active: true, currentWindow: true }); + if (currentTab.length > 0) { + const tabId = currentTab[0].id; + if (tabId) { + await messenger.tabs.remove(tabId); } } } diff --git a/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts b/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts index 9159199fe61..039f78d5c62 100644 --- a/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts +++ b/extension/js/content_scripts/webmail/thunderbird/thunderbird-element-replacer.ts @@ -89,7 +89,7 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer { }; private generatePgpBlockTemplate = (encryptionStatus: string, verificationStatus: string, messageToRender: string): string => { - const pgpBlockTemplate = ` + return `
${encryptionStatus}
@@ -99,19 +99,18 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
${Xss.escape(messageToRender)}
`; - return pgpBlockTemplate; }; private isCleartextMsg = (fullMsg: messenger.messages.MessagePart): boolean => { - const isClearTextMsg = + return ( (fullMsg.headers && 'openpgp' in fullMsg.headers && fullMsg.parts && fullMsg.parts[0]?.parts?.length === 1 && fullMsg.parts[0].parts[0].contentType === 'text/plain' && this.resemblesCleartextMsg(fullMsg.parts[0].parts[0].body?.trim() || '')) || - false; - return isClearTextMsg; + false + ); }; private resemblesCleartextMsg = (body: string) => { @@ -124,27 +123,19 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer { }; private isPublicKeyEncryptedMsg = (fullMsg: messenger.messages.MessagePart): boolean => { - const isPublicKeyEncrypted = - (fullMsg.headers && - 'openpgp' in fullMsg.headers && - fullMsg.parts && - fullMsg.parts[0]?.parts?.length === 2 && - fullMsg.parts[0]?.parts[1].contentType === 'application/pgp-encrypted' && - this.resemblesAsciiArmoredMsg(fullMsg.parts[0]?.parts[0].body?.trim() || '')) || - (fullMsg.headers && - 'openpgp' in fullMsg.headers && - fullMsg.parts && - fullMsg.parts[0]?.parts?.length === 1 && - fullMsg.parts[0]?.contentType === 'multipart/mixed' && - this.resemblesAsciiArmoredMsg(fullMsg.parts[0]?.parts[0].body?.trim() || '')) || - (fullMsg.headers && - 'openpgp' in fullMsg.headers && - fullMsg.parts && - fullMsg.parts.length === 1 && - fullMsg.parts[0]?.contentType === 'text/plain' && - this.resemblesAsciiArmoredMsg(fullMsg.parts[0]?.body?.trim() || '')) || - false; - return isPublicKeyEncrypted; + if (fullMsg.headers && 'openpgp' in fullMsg.headers && fullMsg.parts) { + return ( + (fullMsg.parts[0]?.parts?.length === 2 && + fullMsg.parts[0]?.parts[1].contentType === 'application/pgp-encrypted' && + this.resemblesAsciiArmoredMsg(fullMsg.parts[0]?.parts[0].body?.trim() || '')) || + (fullMsg.parts[0]?.parts?.length === 1 && + fullMsg.parts[0]?.contentType === 'multipart/mixed' && + this.resemblesAsciiArmoredMsg(fullMsg.parts[0]?.parts[0].body?.trim() || '')) || + (fullMsg.parts.length === 1 && fullMsg.parts[0]?.contentType === 'text/plain' && this.resemblesAsciiArmoredMsg(fullMsg.parts[0]?.body?.trim() || '')) || + false + ); + } + return false; }; private resemblesAsciiArmoredMsg = (body: string): boolean => { diff --git a/extension/js/service_worker/bg-handlers.ts b/extension/js/service_worker/bg-handlers.ts index b1e1717bd56..d33378ea5a6 100644 --- a/extension/js/service_worker/bg-handlers.ts +++ b/extension/js/service_worker/bg-handlers.ts @@ -132,9 +132,8 @@ export class BgHandlers { const messageDetails = await messenger.compose.getComposeDetails(Number(tab.id)); const composeMethod = messageDetails.type; const msgId = Number(messageDetails.relatedMessageId); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const acctEmail = Str.parseEmail(messageDetails.from as string).email!; - await handleClickEvent(Number(tab.id), acctEmail, msgId, composeMethod); + const acctEmail = Str.parseEmail(messageDetails.from as string).email; + if (acctEmail) await handleClickEvent(Number(tab.id), acctEmail, msgId, composeMethod); }); messenger.messageDisplayAction.onClicked.addListener(async tab => { const tabId = Number(tab.id); @@ -150,6 +149,7 @@ export class BgHandlers { public static thunderbirdContentScriptRegistration = async () => { const contentScriptGroups = chrome.runtime.getManifest().content_scripts ?? []; // we know it's in the manifest + // sweetalert2.js throws error in Thunderbird environment const files = contentScriptGroups[0].js?.filter(url => !url.includes('sweetalert2')).map(url => url.replace(/moz-extension:\/\/[^/]+\//, './')) ?? []; await messenger.messageDisplayScripts.register({ js: files.map(file => ({ file })), diff --git a/tooling/bundle-content-scripts.ts b/tooling/bundle-content-scripts.ts index a28bc5beb5e..e4ffb9f2935 100644 --- a/tooling/bundle-content-scripts.ts +++ b/tooling/bundle-content-scripts.ts @@ -32,7 +32,7 @@ if (existsSync(OUT_DIR)) { } mkdirSync(OUT_DIR); -// webmail; +// webmail buildContentScript( ([] as string[]).concat( getFilesInDir(`${sourceDir}/js/common/platform`, /\.js$/, false),