|
| 1 | +//We probably don't need Unit Test |
| 2 | + |
| 3 | +function ModifyTextBasic(textNodeContent) |
| 4 | +{ |
| 5 | + return textNodeContent.split(' ').map((word) => { |
| 6 | + //TODO if the user wants numbers to be bolded |
| 7 | + //if(/\d/.test(word)) return word; |
| 8 | + |
| 9 | + |
| 10 | + var boldUp2 = Math.floor(Math.random() * Math.floor(word.length/2)); //TODO Add customizable length: 1/4 , 1/2 , 3/4 of a word etc.. |
| 11 | + return word.replace(word, `<b>${word.substring(0, boldUp2+1)}</b>${word.substring(boldUp2+1)}`); //TODO Add customizable fonts & underline the words that are originally bolded |
| 12 | + }); |
| 13 | +} |
| 14 | + |
| 15 | +function ModifyTextSyllable(textNodeContent) |
| 16 | +{ |
| 17 | + return textNodeContent.split(' ').map((word) => { |
| 18 | + //if(/\d/.test(word)) return word; |
| 19 | + |
| 20 | + var vowel = /[aeiouy]/i; |
| 21 | + var match = vowel.exec(word); |
| 22 | + if(match != null) |
| 23 | + var boldUp2 = match.index; |
| 24 | + return word.replace(word, `<b>${word.substring(0, boldUp2+1)}</b>${word.substring(boldUp2+1)}`); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +function ModifyWebPage() |
| 29 | +{ |
| 30 | + const domParser = new DOMParser(); |
| 31 | + var allText = [... document.getElementsByTagName('p')]; //TODO replace this with customizable Tags |
| 32 | + allText.forEach(element => { |
| 33 | + var text = domParser.parseFromString(element.innerHTML, "text/html"); |
| 34 | + var textNodeCollection = Array.from(text.body.childNodes).map((node) => { |
| 35 | + if(node.nodeType === Node.TEXT_NODE) |
| 36 | + return ModifyTextBasic(node.textContent).join(' '); //Change this to ModifyTextSyllable when changing algorithm |
| 37 | + else |
| 38 | + return node.outerHTML;}) |
| 39 | + element.innerHTML = textNodeCollection.join(''); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +ModifyWebPage(); |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
0 commit comments