Skip to content
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

feat: keep original element styles while changing to bionic mode #19

Merged
merged 1 commit into from
May 22, 2022
Merged
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
62 changes: 35 additions & 27 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,41 @@ changeColor.addEventListener("click", async () => {

// The body of this function will be executed as a content script inside the current page
function convertToReadbaleText() {
chrome.storage.sync.get("color", async ({ color }) => {

// making half of the letters in a word bold
function highlightText(sentenceText) {
return sentenceText
.split(' ')
.map((word) => {
const length = word.length
const midPoint = Math.round(length / 2)
const firstHalf = word.slice(0, midPoint)
const secondHalf = word.slice(midPoint)
const htmlWord = `<b>${firstHalf}</b>${secondHalf}`
return htmlWord
})
.join(' ')
}

chrome.storage.sync.get('color', async ({ color }) => {
// setting global styles
var style = document.createElement("style");
style.textContent = "b { font-weight: bold !important; }";
document.head.appendChild(style);

let tags = ["p", "font", "span"];

tags.forEach((element) => {
pList = document.getElementsByTagName(element);
// making half of the letters in a word bold
for (let sentence of pList) {
const sentenceText = sentence.innerText;
const textArr = sentenceText.split(" ");
const textArrTransformed = textArr.map((word) => {
const length = word.length;
const midPoint = Math.round(length / 2);
const firstHalf = word.slice(0, midPoint);
const secondHalf = word.slice(midPoint);
const htmlWord = `<b>${firstHalf}</b>${secondHalf}`;
return htmlWord;
});
console.log();
sentence.innerHTML = textArrTransformed.join(" ");
}
});
var style = document.createElement('style')
style.textContent = 'b { font-weight: bold !important; }'
document.head.appendChild(style)

});
let tags = ['p', 'font', 'span', 'li']

const parser = new DOMParser()
tags.forEach((tag) => {
for (let 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(' ')
}
})
})
}