Skip to content

Commit

Permalink
Add "Substitute folding white spaces (FWS)" option
Browse files Browse the repository at this point in the history
for Thunderbird 128 Beta and later.

The option is enabled by default to ensure compatibility with existing user configurations.

In Thunderbird 128 Beta, headers unfolding behavior was updated to retain folding white spaces (FWS) when encountered, whereas previous Thunderbird versions substituted FWS with spaces during unfolding.
  • Loading branch information
moisseev committed Jul 4, 2024
1 parent 6291fdc commit ac573a1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
40 changes: 25 additions & 15 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ function parseReceivedHeaders(headers, regexp) {
return received;
}

browser.storage.local.get(["headerNumbers", "regexp"]).then(({headerNumbers, regexp}) => {
if (!headerNumbers) browser.storage.local.set({headerNumbers: ""});
if (!regexp) browser.storage.local.set({regexp: "(.*)"});
});

function displayReceivedHeader(windowId, tabIndex, messageId) {
function displayReceivedHeader(windowId, tabIndex, messageId, majorVersion) {
browser.messages.getFull(messageId).then((messagepart) => {
browser.storage.local.get(["headerNumbers", "regexp", "removeDuplicates", "singleLine"])
.then(({headerNumbers, regexp, removeDuplicates, singleLine = false}) => {
browser.storage.local.get(["headerNumbers", "regexp", "removeDuplicates", "singleLine", "substituteFWS"])
.then(({headerNumbers, regexp, removeDuplicates, singleLine = false, substituteFWS = true}) => {
let headers = [];
let numberFound = false;
let separator = "🡄";
Expand Down Expand Up @@ -73,6 +68,13 @@ function displayReceivedHeader(windowId, tabIndex, messageId) {
}

if (headers.length) {
if (majorVersion >= 128 && substituteFWS) {
// Substitute folding white spaces (FWS)
const foldingWhiteSpaceRegex = /[\t ]+/g;
headers.forEach((header, i) => (headers[i] = header.replace(foldingWhiteSpaceRegex, " ")));
}


const parsed = parseReceivedHeaders(headers, regexp);
let filteredParsed = [];

Expand All @@ -97,9 +99,19 @@ function displayReceivedHeader(windowId, tabIndex, messageId) {
});
}

let lastDisplayedMessageId = null;

browser.runtime.getBrowserInfo().then((browserInfo) => {
const [majorVersion] = browserInfo.version.split(".", 1);

// Default options
browser.storage.local.get(["headerNumbers", "regexp", "substituteFWS"])
.then(({headerNumbers, regexp, substituteFWS}) => {
if (!headerNumbers) browser.storage.local.set({headerNumbers: ""});
if (!regexp) browser.storage.local.set({regexp: "(.*)"});
if (majorVersion >= 128 && !substituteFWS) browser.storage.local.set({substituteFWS: true});
});

browser.windows.getAll({populate: true, windowTypes: ["normal", "messageDisplay"]}).then((windows) => {
windows.forEach(function (window) {
window.tabs
Expand All @@ -109,18 +121,16 @@ browser.runtime.getBrowserInfo().then((browserInfo) => {
.forEach((tab) => {
browser.messageDisplay.getDisplayedMessage(tab.id).then((message) => {
if (!message) return;
displayReceivedHeader(tab.windowId, tab.index, message.id);
displayReceivedHeader(tab.windowId, tab.index, message.id, majorVersion);
});
});
});
});
});

let lastDisplayedMessageId = null;

browser.messageDisplay.onMessageDisplayed.addListener((tab, message) => {
lastDisplayedMessageId = message.id;
displayReceivedHeader(tab.windowId, tab.index, message.id);
browser.messageDisplay.onMessageDisplayed.addListener((tab, message) => {
lastDisplayedMessageId = message.id;
displayReceivedHeader(tab.windowId, tab.index, message.id, majorVersion);
});
});

browser.mailTabs.onSelectedMessagesChanged.addListener((tab, selectedMessages) => {
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"gecko": {
"id": "[email protected]",
"strict_min_version": "78.0",
"strict_max_version": "116.*"
"strict_max_version": "128.*"
}
},
"name": "Received",
"description": "Displays the \"Received\" header parsed with a regular expression.",
"version": "2.5.3",
"version": "2.6.0",
"author": "Alexander Moisseev ([email protected])",
"homepage_url": "https://github.com/moisseev/received",
"icons": {
Expand Down
8 changes: 8 additions & 0 deletions options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
.remark {
font-size: smaller;
color: var(--in-content-deemphasized-text);
display: block;
}
.received-examples {
color: var(--in-content-deemphasized-text);
Expand Down Expand Up @@ -67,6 +68,13 @@
</label>
<input type="text" id="regexp" />
</p>
<p id="substitute-fws-option" hidden>
<input type="checkbox" id="substitute-fws" />
<label for="substitute-fws">
Substitute folding white spaces (FWS, RFC 5322)
</label>
<span class="remark">Replaces all sequences of tabs and/or spaces with a single space (" ") in unfolded mail headers.</span>
</p>
<p>
<input type="checkbox" id="remove-duplicates" />
<label for="remove-duplicates">
Expand Down
41 changes: 26 additions & 15 deletions options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@

"use strict";

function saveOptions(e) {
e.preventDefault();

browser.storage.local.set({
headerNumbers: document.querySelector("#header-numbers").value,
regexp: document.querySelector("#regexp").value,
removeDuplicates: document.querySelector("#remove-duplicates").checked,
singleLine: document.querySelector("#single-line").checked
});
}

function restoreOptions() {
browser.storage.local.get(["headerNumbers", "regexp", "removeDuplicates", "singleLine"])
.then(({headerNumbers, regexp, removeDuplicates, singleLine}) => {
browser.storage.local.get(["headerNumbers", "regexp", "removeDuplicates", "singleLine", "substituteFWS"])
.then(({headerNumbers, regexp, removeDuplicates, singleLine, substituteFWS}) => {
document.querySelector("#header-numbers").value = headerNumbers;
document.querySelector("#regexp").value = regexp;
document.querySelector("#remove-duplicates").checked = removeDuplicates;
document.querySelector("#single-line").checked = singleLine;
document.querySelector("#substitute-fws").checked = substituteFWS;
});
}

document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);


browser.runtime.getBrowserInfo().then((browserInfo) => {
const [majorVersion] = browserInfo.version.split(".", 1);

if (majorVersion >= 128) document.querySelector("#substitute-fws-option").removeAttribute("hidden");

function saveOptions(e) {
e.preventDefault();

browser.storage.local.set({
headerNumbers: document.querySelector("#header-numbers").value,
regexp: document.querySelector("#regexp").value,
removeDuplicates: document.querySelector("#remove-duplicates").checked,
singleLine: document.querySelector("#single-line").checked,

});
if (majorVersion >= 128) {
browser.storage.local.set({substituteFWS: document.querySelector("#substitute-fws").checked});
}
}
document.querySelector("form").addEventListener("submit", saveOptions);
});

0 comments on commit ac573a1

Please sign in to comment.