Skip to content

Commit

Permalink
Merge pull request #149 from jeevatkm/rwh-v3-beta2
Browse files Browse the repository at this point in the history
RWH v3 beta2 updates
  • Loading branch information
jeevatkm authored May 16, 2024
2 parents d38863e + 448b839 commit f70e382
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 60 deletions.
11 changes: 10 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "ReplyWithHeader",
"description": "Outlook style headers and few goodies for Thunderbird",
"version": "3.0.0.beta1",
"version": "3.0.0.beta2",
"author": "Jeevanandam M.",
"homepage_url": "https://myjeeva.com/replywithheader-mozilla",
"browser_specific_settings": {
Expand All @@ -26,6 +26,15 @@
"messagesRead",
"accountsRead"
],
"message_display_action": {
"default_title": "RWH",
"type": "menu",
"default_icon": {
"16": "images/rwh.svg",
"24": "images/rwh.svg",
"32": "images/rwh.svg"
}
},
"background": {
"page": "background.html"
},
Expand Down
106 changes: 81 additions & 25 deletions modules/compose.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// RWH Compose Module

import { rwhLogger} from './logger.mjs';
import { rwhLogger } from './logger.mjs';
import * as rwhSettings from './settings.mjs';
import * as rwhI18n from './headers-i18n.mjs';
import * as rwhAccounts from './accounts.mjs';
Expand All @@ -25,33 +25,47 @@ export async function process(tab) {

let composeDetails = await messenger.compose.getComposeDetails(tab.id);
rwhLogger.debug(composeDetails);
if (composeDetails.type === 'new') {
rwhLogger.debug('New message getting composed');
return;
}

// Check account level disable exists
let accountId = await rwhAccounts.findIdByIdentityId(composeDetails.identityId);
let isAccountEnabled = await rwhSettings.isAccountEnabled(accountId);
rwhLogger.debug('AccountId', accountId, 'isAccountEnabled', isAccountEnabled);
if (!isAccountEnabled) {
return;
return; // RWH stops here
}

// Check 10s disable exists on message level
let isMessageLevelDisabled = await rwhSettings.get(`disable.${accountId}.message_${composeDetails.relatedMessageId}`);
rwhLogger.debug('isMessageLevelDisabled', isMessageLevelDisabled);
if (isMessageLevelDisabled) {
return; // RWH stops here
}

let fullMsg = await messenger.messages.getFull(composeDetails.relatedMessageId);
rwhLogger.debug(fullMsg);

let rwh = new ReplyWithHeader(composeDetails, fullMsg).init();
let rwh = new ReplyWithHeader(accountId, composeDetails, fullMsg).init();
if (!(rwh.isReply || rwh.isForward)) {
rwhLogger.warn(`Unsupported compose type ${rwh.composeType}`);
return;
return; // RWH stops here
}

await rwh.process(tab);
}

class ReplyWithHeader {
#accountId
#composeDetails
#fullMessage
#document
#text

constructor(composeDetails, fullMessage) {
constructor(accountId, composeDetails, fullMessage) {
this.#accountId = accountId;
this.#composeDetails = composeDetails;
this.#fullMessage = fullMessage;
}
Expand Down Expand Up @@ -103,16 +117,10 @@ class ReplyWithHeader {
}

async process(tab) {
let result = {isModified: false};
let result = { isModified: false };

if (rwhSettings.isTransSubjectPrefix()) {
let subject = this.#composeDetails.subject;
if (this.isReply && subject.startsWith(rwhSettings.replySubjectPrefix)) {
result.subject = subject.replace(rwhSettings.replySubjectPrefix, 'RE:')
}
if (this.isForward && subject.startsWith(rwhSettings.forwardSubjectPrefix)) {
result.subject = subject.replace(rwhSettings.forwardSubjectPrefix, 'FW:');
}
result.subject = this._transformSubjectPrefix(this.#composeDetails.subject);
}

if (this.isPlainText) {
Expand All @@ -121,7 +129,7 @@ class ReplyWithHeader {
result = Object.assign({}, result, await this._processPlainText());
} else {
rwhLogger.debug('HTML Content', this.htmlBody);
this.#document = this._createDocumentFromString(this.htmlBody);
this.#document = rwhUtils.createDocumentFromString(this.htmlBody);
result = Object.assign({}, result, await this._processHtml());
}

Expand Down Expand Up @@ -154,7 +162,7 @@ class ReplyWithHeader {
'cc': await this._extractHeader('cc', true, true),
'date': await this._extractHeader('date', false, true),
'reply-to': await this._extractHeader('reply-to', true, true),
'subject': await this._extractHeader('subject', false, true),
'subject': this._transformSubjectPrefix(await this._extractHeader('subject', false, true)),
}
rwhLogger.debug(headers);

Expand Down Expand Up @@ -193,7 +201,7 @@ class ReplyWithHeader {
'cc': await this._extractHeader('cc', true, false),
'date': await this._extractHeader('date', false, false),
'reply-to': await this._extractHeader('reply-to', true, false),
'subject': await this._extractHeader('subject', false, false),
'subject': this._transformSubjectPrefix(await this._extractHeader('subject', false, false)),
}
rwhLogger.debug(headers);

Expand All @@ -216,7 +224,7 @@ class ReplyWithHeader {
}
} else if (this.isForward) {
linesToDelete = rwhHeaders.length;
for(let l of textLines) {
for (let l of textLines) {
if (l.trim().startsWith(fwdHdrLookupString)) {
break;
}
Expand Down Expand Up @@ -272,6 +280,7 @@ class ReplyWithHeader {
}
}, this);

rwhHeaders += await this._handleAllHeadersFlow(false, true);
rwhHeaders += '</div><br>';

return rwhHeaders;
Expand All @@ -281,7 +290,6 @@ class ReplyWithHeader {
let locale = await rwhSettings.getHeaderLocale();
let headerLabelSeq = await rwhSettings.getHeaderLabelSeqStyle();
let headerLabelSeqValues = rwhSettings.headerLabelSeqStyleSettings[headerLabelSeq];
let lineBreak = '\r\n';

let rwhHeaders = [];
if (await rwhSettings.isHeaderPlainPrefixText()) {
Expand Down Expand Up @@ -309,6 +317,10 @@ class ReplyWithHeader {
}
}, this);

let remainingHeaders = await this._handleAllHeadersFlow(false, false);
if (remainingHeaders.length > 0) {
rwhHeaders = [...rwhHeaders, ...remainingHeaders];
}
rwhHeaders.push('');

return rwhHeaders;
Expand All @@ -322,8 +334,8 @@ class ReplyWithHeader {
let includeTimezone = await rwhSettings.isHeaderTimeZone();

rwhLogger.debug('Date format: ' + (dateFormat == 1 ? 'UTC' : 'Locale (' + locale + ')')
+ ', Time format: ' + (timeFormat == 1 ? '24-hour' : '12-hour')
+ (includeTimezone ? ', Include short timezone info' : ''))
+ ', Time format: ' + (timeFormat == 1 ? '24-hour' : '12-hour')
+ (includeTimezone ? ', Include short timezone info' : ''))

let epoch = null;
try {
Expand Down Expand Up @@ -373,9 +385,18 @@ class ReplyWithHeader {
return escape ? this._escapeHtml(pv.join(', ')) : pv.join(', ');
}

async _extractRemainingHeaders(clean, escape) {
let remainingHeaders = {};
for (let key of Object.keys(this.#fullMessage.headers)) {
if (rwhSettings.headerLabelSeqStyleSettings[0].includes(key)) { continue; }
remainingHeaders[key] = await this._extractHeader(key, clean, escape);
}
return remainingHeaders;
}

_findPlainTextReplyInsertMarker(textLines, lookupWord) {
let startPos = 0;
for(let l of textLines) {
for (let l of textLines) {
if (l.trim().includes(lookupWord)) {
return { found: true, startPos: startPos }
}
Expand Down Expand Up @@ -408,10 +429,6 @@ class ReplyWithHeader {
return (v || '').replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
}

_createDocumentFromString(s) {
return new DOMParser().parseFromString(s, 'text/html')
}

_cleanNodesUpToClassName(node, cssClassName) {
while (node.firstChild) {
if (node.firstChild?.className?.includes(cssClassName)) {
Expand All @@ -420,4 +437,43 @@ class ReplyWithHeader {
node.removeChild(node.firstChild);
}
}

_transformSubjectPrefix(subject) {
if (subject.startsWith(rwhSettings.replySubjectPrefix)) {
return subject.replace(rwhSettings.replySubjectPrefix, 'RE:')
}
if (subject.startsWith(rwhSettings.forwardSubjectPrefix)) {
return subject.replace(rwhSettings.forwardSubjectPrefix, 'FW:');
}
return subject;
}

async _handleAllHeadersFlow(clean, escape) {
if (this.isReply) { return ''; }

let prefName = `header.fwd.all.${this.#accountId}.message_${this.#composeDetails.relatedMessageId}`;
let isForwardAllHeaders = await rwhSettings.get(prefName);
rwhLogger.debug('isForwardAllHeaders', isForwardAllHeaders);
if (isForwardAllHeaders) {
let remainingHeaders = await this._extractRemainingHeaders(clean, escape);
rwhLogger.debug(remainingHeaders);

if (this.isPlainText) {
let rwhHeaders = [];
for (let [key, value] of Object.entries(remainingHeaders)) {
rwhHeaders.push(rwhUtils.toPartialCanonicalFormat(key) + ': ' + value);
}
return rwhHeaders;
} else {
let rwhHeaders = '';
for (let [key, value] of Object.entries(remainingHeaders)) {
rwhHeaders += '<p style="margin:0cm;font-size:11.5pt"><span><b>'
+ rwhUtils.toPartialCanonicalFormat(key) + ':</b> ' + value + '</span></p>';
}
return rwhHeaders;
}
}

return '';
}
}
Loading

0 comments on commit f70e382

Please sign in to comment.