added methods to manipolate the compose message dom. see #30 #34

This commit is contained in:
mic 2024-04-30 22:27:32 +02:00
parent c624908044
commit 87d5b332bb

View file

@ -58,4 +58,50 @@ function extractEmail(text) {
const emailRegex = /[\w.-]+@[\w.-]+\.\w+/;
const match = text.match(emailRegex);
return match ? match[0] : '';
}
}
export async function replaceBody(tabId, text){
let composeDetails = await messenger.compose.getComposeDetails(tabId);
let originalHtmlBody = composeDetails.body;
console.log('originalHtmlBody: ' + originalHtmlBody);
let fullBody = insertText(text, originalHtmlBody);
console.log('fullBody: ' + fullBody);
await messenger.compose.setComposeDetails(tabId, {body: fullBody});
}
// The following methods are a modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js
const insertText = function (text, fullBody_string) {
const parser = new DOMParser();
let fullBody = parser.parseFromString(fullBody_string, "text/html");
const prefix = fullBody.getElementsByClassName("moz-cite-prefix");
if (prefix.length > 0) {
const divider = prefix[0];
let sibling = divider.previousSibling;
while (sibling) {
fullBody.body.removeChild(sibling);
sibling = divider.previousSibling;
}
}
makeParagraphs(text, function (p) {
fullBody.body.insertBefore(p, fullBody.body.firstChild);
});
return fullBody.body.innerHTML;
}
const makeParagraphs = (text, func) => {
const chunks = text.split(/\n{2,}/);
if (chunks.length == 1) {
return func(document.createTextNode(text));
}
const paragraphs = chunks.map((t) => {
const p = document.createElement("p");
p.innerText = t;
return p;
});
for (let i = paragraphs.length - 1; i >= 0; i--) {
func(paragraphs[i]);
}
};