From 544a339a5de0b6ad05cd2ba0c4822c4f85a88a05 Mon Sep 17 00:00:00 2001 From: TheRedKeks <40245965+RedCommander735@users.noreply.github.com> Date: Wed, 8 Mar 2023 19:23:49 +0100 Subject: [PATCH 1/2] Changed boldify loop --- Firefox/content_scripts/modifyHtml.js | 101 ++++++++------------------ 1 file changed, 31 insertions(+), 70 deletions(-) diff --git a/Firefox/content_scripts/modifyHtml.js b/Firefox/content_scripts/modifyHtml.js index 83188d9..fa7a62f 100644 --- a/Firefox/content_scripts/modifyHtml.js +++ b/Firefox/content_scripts/modifyHtml.js @@ -39,79 +39,47 @@ function modifyHtml(finalElements) { //Process to append tag var newText = document.createElement("span"); // Serve to incubate our textWorking newText.className = "smartReader-remove"; newText.style = "display:unset"; // Fix Display problems - - var countLetter = 0; // Indicator to only bold the first two characters - var textWorking = ''; // Text without formatting - - var exclusionCharacter = 0; // Count "\n" and " " to avoid text only consisting of them, causing display problems - - for(k = 0 ; k < textToConvert.length ; k++) { - - var textNode; // Used to create a textNode, contening textWorking - var bold; // Used when the condition to append the tag is valid, contening textNode - - var characterToChange = textToConvert.charAt(k); - var secondCharacter = textToConvert.charAt(k + 1); - var isLetter = isItLetter(textToConvert.codePointAt(k)); // Used to separate words from special characters - var isLetterSecondChar = isItLetter(textToConvert.codePointAt(k + 1)); - var isLetterThirdChar = isItLetter(textToConvert.codePointAt(k + 2)); + + let exclusionCharacter = 0; // Count "\n" and " " to avoid text only consisting of them, causing display problems - if(isLetter === true && isLetterSecondChar === true && isLetterThirdChar === true && countLetter < 2 ) { //This condition is valid at the start of a word - - if(secondCharacter === " " || secondCharacter === "\n" || secondCharacter === "\t") { + let node_regex = /([A-zäöüßÄÖÜ])+\w|[^\n]/g + let word_regex = /([A-zäöüßÄÖÜ])+/g - exclusionCharacter++; + let matching_words = textToConvert.match(node_regex) - countLetter = 0; + matching_words.forEach(word => { + if (word_regex.test(word)) { + let bold = document.createElement("B") + let not_bold = document.createTextNode('') + let len = word.length + if (len > 2) { + len = Math.ceil(len/2) + bold.appendChild(document.createTextNode(word.substring(0, len))) + not_bold = document.createTextNode(word.substring(len)) + } else { - - countLetter = countLetter + 2; + bold.appendChild(document.createTextNode(word)) } - - if(textWorking !== '') { // Adding textWorking in a node to keep the order of the words, because we're going to append a new bold element - - textNode = document.createTextNode(textWorking); - newText.appendChild(textNode); - textWorking = ''; + newText.appendChild(bold) + newText.appendChild(not_bold) + + + } else { + newText.appendChild(document.createTextNode(word)) + if(characterToChange === "\n" || characterToChange === " " || characterToChange === "\t") { // Avoid display problems + exclusionCharacter++; } - - bold = document.createElement("B"); - - textNode = document.createTextNode(characterToChange + secondCharacter); - - bold.appendChild(textNode); - - newText.appendChild(bold); - - k++; - - } else if(isLetter === false) { // If it's a special character, the count of letter is reset to 0 and we add the text at our TextWorking - - textWorking = textWorking + characterToChange; - - countLetter = 0; - - } else { // We're going here when we got a letter and when (countLetter > 2) - - textWorking = textWorking + characterToChange; - - countLetter++; } + console.log(newText) + }); - if(k === textToConvert.length - 1) { // If we are at the end of the loop, we add the text to a node - - textNode = document.createTextNode(textWorking); - newText.appendChild(textNode); - textWorking = ''; - } - - if(characterToChange === "\n" || characterToChange === " " || characterToChange === "\t") { // Avoid display problems - - exclusionCharacter++; - } + if(exclusionCharacter < textToConvert.length) { + finalElements[i].insertBefore(newText, nextElement); + + values[j].textContent = ''; } /* Our count of exclusionCharacter needs to be lower than the length of our textToConvert @@ -120,14 +88,7 @@ function modifyHtml(finalElements) { //Process to append tag because that means that our text is only composed of "\n" and " ", because putting these characters in a tag create a display issue - */ - - if(exclusionCharacter < textToConvert.length) { - - finalElements[i].insertBefore(newText, nextElement); - - values[j].textContent = ''; - } + */ } } } From 6ef1038c7aafe5ca30b9ddbf511ac65d3172d710 Mon Sep 17 00:00:00 2001 From: TheRedKeks <40245965+RedCommander735@users.noreply.github.com> Date: Wed, 8 Mar 2023 19:28:30 +0100 Subject: [PATCH 2/2] . --- Firefox/content_scripts/modifyHtml.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Firefox/content_scripts/modifyHtml.js b/Firefox/content_scripts/modifyHtml.js index fa7a62f..bda1279 100644 --- a/Firefox/content_scripts/modifyHtml.js +++ b/Firefox/content_scripts/modifyHtml.js @@ -69,18 +69,15 @@ function modifyHtml(finalElements) { //Process to append tag } else { newText.appendChild(document.createTextNode(word)) - if(characterToChange === "\n" || characterToChange === " " || characterToChange === "\t") { // Avoid display problems - exclusionCharacter++; - } } console.log(newText) }); - if(exclusionCharacter < textToConvert.length) { - finalElements[i].insertBefore(newText, nextElement); + + finalElements[i].insertBefore(newText, nextElement); - values[j].textContent = ''; - } + values[j].textContent = ''; + /* Our count of exclusionCharacter needs to be lower than the length of our textToConvert