Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sosnovsky committed Sep 11, 2024
1 parent dc212e0 commit cc34e7f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
14 changes: 6 additions & 8 deletions extension/chrome/elements/passphrase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
};

private generatePgpBlockTemplate = (encryptionStatus: string, verificationStatus: string, messageToRender: string): string => {
const pgpBlockTemplate = `
return `
<div ${encryptionStatus === 'encrypted' ? 'class="pgp_secure"' : 'class="pgp_neutral"'}>
<div>
<div id="pgp_encryption" class="pgp_badge short ${encryptionStatus === 'encrypted' ? 'green_label' : 'red_label'}">${encryptionStatus}</div>
Expand All @@ -99,19 +99,18 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
<pre>${Xss.escape(messageToRender)}</pre>
</div>
</div>`;
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) => {
Expand All @@ -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 => {
Expand Down
6 changes: 3 additions & 3 deletions extension/js/service_worker/bg-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 })),
Expand Down
2 changes: 1 addition & 1 deletion tooling/bundle-content-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (existsSync(OUT_DIR)) {
}
mkdirSync(OUT_DIR);

// webmail;
// webmail
buildContentScript(
([] as string[]).concat(
getFilesInDir(`${sourceDir}/js/common/platform`, /\.js$/, false),
Expand Down

0 comments on commit cc34e7f

Please sign in to comment.