Created
June 19, 2022 15:16
-
-
Save MikaelCarpenter/8f9c62d382830633407ab4d453a9be9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference: https://github.com/tobyxdd/bread/blob/master/bread.js | |
function processWord(word, node) { | |
const len = word.length; | |
const boldLen = Math.ceil(len * 0.45); // might need an exception for 1 letter words | |
const boldText = word.substring(0, boldLen); | |
const normText = word.substring(boldLen); | |
const span = document.createElement("span"); | |
span.style.fontWeight = "bolder"; | |
span.appendChild(document.createTextNode(boldText)); | |
node.parentNode.insertBefore(span, node); | |
node.parentNode.insertBefore(document.createTextNode(`${normText} `), node); | |
} | |
function processNode(node) { | |
const text = node.textContent; | |
const words = text.split(" "); | |
words.forEach((word) => processWord(word, node)); | |
node.remove(); | |
} | |
// customize for your content | |
const textNodes = document.querySelectorAll( | |
"#contentPanel > div > div.EOs2MW > div > div.bWSNNT > div._14BzLL span" | |
); | |
textNodes.forEach(processNode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment