correctly preservig formatting. see #70

This commit is contained in:
mic 2024-06-26 22:22:57 +02:00
parent d71301f9e1
commit 7f0a50be42

View file

@ -350,58 +350,62 @@ async function doProceed(message, customText = ''){
operation_done(); operation_done();
} }
function removeTagsAndReturnHTML(rootElement, removeTags, preserveTags) { function removeTagsAndReturnHTML(rootElement, removeTags, preserveTags) {
removeTags.forEach(tag => { const fragment = document.createDocumentFragment();
const elements = rootElement.getElementsByTagName(tag);
while (elements.length > 0) {
const element = elements[0];
const fragment = document.createDocumentFragment();
// Preserve specified tags and their children in the correct order function handleElement(element) {
let child = element.firstChild; let child = element.firstChild;
while (child) { while (child) {
console.log(">>>>>>>>>>>> ciclo child: " + child.tagName.toLowerCase()); const nextSibling = child.nextSibling;
const nextSibling = child.nextSibling; if (preserveTags.includes(child.nodeName.toLowerCase())) {
if (preserveTags.includes(child.tagName.toLowerCase())) { //console.log(">>>>>>>>>>>> preserve child: " + child.tagName.toLowerCase());
console.log(">>>>>>>>>>>> preserve: " + child.tagName.toLowerCase()); fragment.appendChild(child);
fragment.appendChild(child); } else if (child.nodeType === Node.ELEMENT_NODE) {
} //console.log(">>>>>>>>>>>> handleElement(child): " + child.tagName.toLowerCase());
child = nextSibling; handleElement(child);
} }
child = nextSibling;
// Append preserved elements back to the parent of the removed element
element.parentNode.insertBefore(fragment, element);
// Remove the element and all its remaining children
element.parentNode.removeChild(element);
} }
}
removeTags.forEach(tag => {
const elements = Array.from(rootElement.getElementsByTagName(tag));
elements.forEach(element => {
handleElement(element);
element.parentNode.insertBefore(fragment.cloneNode(true), element);
element.parentNode.removeChild(element);
//console.log(">>>>>>>>>>>> removeChild: " + element.tagName.toLowerCase());
});
}); });
replaceNewlinesWithBr(rootElement); replaceNewlinesWithBr(rootElement);
//console.log(">>>>>>>>>>>> rootElement.innerHTML: " + rootElement.innerHTML);
// Return the updated HTML as a string // Return the updated HTML as a string
return rootElement.innerHTML; return rootElement.innerHTML;
} }
// Replace newline characters with <br> tags // Replace newline characters with <br> tags
function replaceNewlinesWithBr(node) { function replaceNewlinesWithBr(node) {
for (let child of Array.from(node.childNodes)) { for (let child of Array.from(node.childNodes)) {
if (child.nodeType === Node.TEXT_NODE) { if (child.nodeType === Node.TEXT_NODE) {
const parts = child.textContent.split('\\n'); const parts = child.textContent.split('\\n');
if (parts.length > 1) { if (parts.length > 1) {
const fragment = document.createDocumentFragment(); const fragment = document.createDocumentFragment();
parts.forEach((part, index) => { parts.forEach((part, index) => {
fragment.appendChild(document.createTextNode(part)); fragment.appendChild(document.createTextNode(part));
if (index < parts.length - 1) { if (index < parts.length - 1) {
fragment.appendChild(document.createElement('br')); fragment.appendChild(document.createElement('br'));
}
});
child.parentNode.replaceChild(fragment, child);
} }
}); } else if (child.nodeType === Node.ELEMENT_NODE) {
child.parentNode.replaceChild(fragment, child); replaceNewlinesWithBr(child);
} }
} else if (child.nodeType === Node.ELEMENT_NODE) {
replaceNewlinesWithBr(child);
}
} }
} }
// In the content script // In the content script
browser.runtime.onMessage.addListener((message, sender, sendResponse) => { browser.runtime.onMessage.addListener((message, sender, sendResponse) => {