Skip to content

Commit

Permalink
Merge pull request #85 from asieduernest12/master
Browse files Browse the repository at this point in the history
Update dom parsing and fix some elements not bolded and broken buttons
  • Loading branch information
ansh authored May 29, 2022
2 parents ef078db + e0fb250 commit ff23edf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
40 changes: 18 additions & 22 deletions src/ContentScript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ function makeFixations(/** @type string */ textContent) {
return weakFixation + mildFixation + strongFixation;
}

function parseDocument() {
const tags = ['p', 'font', 'span', 'li'];
const parser = new DOMParser();
tags.forEach((tag) => {
for (const element of document.getElementsByTagName(tag)) {
const n = parser.parseFromString(element.innerHTML, 'text/html');
const textArrTransformed = Array.from(n.body.childNodes).map((node) => {
if (node.nodeType === Node.TEXT_NODE) {
return highlightText(node.nodeValue);
}
return node.outerHTML;
});
element.innerHTML = textArrTransformed.join(' ');
function parseNode(/** @type Element */ node) {
if (node.nodeType === Node.TEXT_NODE && node.nodeValue.length) {
try {
const brSpan = document.createElement('br-span');

brSpan.innerHTML = highlightText(node.nodeValue);

if (brSpan.childElementCount === 0) return;

node.parentElement.replaceChild(brSpan, node);
} catch (error) {

}
});
return;
}

if (node.hasChildNodes()) [...node.childNodes].forEach(parseNode);
}

const ToggleReading = (enableReading) => {
Expand All @@ -57,11 +59,12 @@ const ToggleReading = (enableReading) => {

if (boldedElements.length < 1) {
addStyles();
parseDocument();
[...document.body.children].forEach(parseNode);
}

if (document.body.classList.contains('br-bold') || enableReading === false) {
document.body.classList.remove('br-bold');
console.timeEnd('ToggleReading-Time');
return;
}

Expand All @@ -71,16 +74,10 @@ const ToggleReading = (enableReading) => {

if (enableReading) document.body.classList.add('br-bold');

if (boldedElements.length > 0) {
console.timeEnd('ToggleReading-Time');
return;
}

console.timeEnd('ToggleReading-Time');
};

const onChromeRuntimeMessage = (message, sender, sendResponse) => {
console.log('Got message in content script as =>', message, sender);
switch (message.type) {
case 'getBrMode':
sendResponse({ data: document.body.classList.contains('br-bold') });
Expand Down Expand Up @@ -137,7 +134,6 @@ const onChromeRuntimeMessage = (message, sender, sendResponse) => {
break;
}
default:
console.log('Error: not found', message);
break;
}
};
Expand Down
2 changes: 0 additions & 2 deletions src/Popup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ fixationStrengthSlider.addEventListener('change', (event) => {
});

chrome.runtime.sendMessage(payload, (response) => {
console.log(response);
});
});

/**
* @description Show the word interval between saccades
*/
Expand Down

0 comments on commit ff23edf

Please sign in to comment.