Skip to content

added support for languages with non-Latin letters #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Chrome/content_scripts/isItLetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function isItLetter(char) {

var letter = (char > 64 && char < 91) || (char > 96 && char < 123)
|| (char > 191 && char < 208) || (char > 208 && char < 215)
|| (char > 215 && char < 247) || (char > 247 && char < 256);
|| (char > 215 && char < 247) || (char > 247 && char < 256)
|| (char > 1040 && char < 1103);

return letter;
}
}
101 changes: 28 additions & 73 deletions Chrome/content_scripts/modifyHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,96 +39,51 @@ function modifyHtml(finalElements) { //Process to append <b> 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 <b> 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 <b> 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));

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 = /([\p{L}\p{N}])+|[^\n]/gu
let word_regex = /([\p{L}\p{N}])+/gu

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('');

} else {

countLetter = countLetter + 2;
}
let len = word.length;
if (len > 2) {
//len = Math.ceil(len/2)
len = 2;
bold.appendChild(document.createTextNode(word.substring(0, len)));
not_bold = document.createTextNode(word.substring(len));

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 = '';
} else {
bold.appendChild(document.createTextNode(word));
}

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++;
newText.appendChild(not_bold);


} else {
newText.appendChild(document.createTextNode(word));
}
});

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++;
}
}

finalElements[i].insertBefore(newText, nextElement);

values[j].textContent = '';


/* Our count of exclusionCharacter needs to be lower than the length of our textToConvert

If exclusionCharacter === textToConvert.length, we're not going to insert,
because that means that our text is only composed of "\n" and " ",
because putting these characters in a <span> tag create a display issue

*/

if(exclusionCharacter < textToConvert.length) {

finalElements[i].insertBefore(newText, nextElement);

values[j].textContent = '';
}
*/
}
}
}
}
}
5 changes: 3 additions & 2 deletions Firefox/content_scripts/isItLetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function isItLetter(char) {

var letter = (char > 64 && char < 91) || (char > 96 && char < 123)
|| (char > 191 && char < 208) || (char > 208 && char < 215)
|| (char > 215 && char < 247) || (char > 247 && char < 256);
|| (char > 215 && char < 247) || (char > 247 && char < 256)
|| (char > 1040 && char < 1103);

return letter;
}
}
101 changes: 28 additions & 73 deletions Firefox/content_scripts/modifyHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,96 +39,51 @@ function modifyHtml(finalElements) { //Process to append <b> 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 <b> 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 <b> 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));

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 = /([\p{L}\p{N}])+|[^\n]/gu
let word_regex = /([\p{L}\p{N}])+/gu

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('');

} else {

countLetter = countLetter + 2;
}
let len = word.length;
if (len > 2) {
//len = Math.ceil(len/2)
len = 2;
bold.appendChild(document.createTextNode(word.substring(0, len)));
not_bold = document.createTextNode(word.substring(len));

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 = '';
} else {
bold.appendChild(document.createTextNode(word));
}

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++;
newText.appendChild(not_bold);


} else {
newText.appendChild(document.createTextNode(word));
}
});

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++;
}
}

finalElements[i].insertBefore(newText, nextElement);

values[j].textContent = '';


/* Our count of exclusionCharacter needs to be lower than the length of our textToConvert

If exclusionCharacter === textToConvert.length, we're not going to insert,
because that means that our text is only composed of "\n" and " ",
because putting these characters in a <span> tag create a display issue

*/

if(exclusionCharacter < textToConvert.length) {

finalElements[i].insertBefore(newText, nextElement);

values[j].textContent = '';
}
*/
}
}
}
}
}