Skip to content

Commit

Permalink
Title reading.
Browse files Browse the repository at this point in the history
  • Loading branch information
boocmp committed Jul 27, 2023
1 parent ec66e2d commit 5b77d0f
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions components/speedreader/resources/speedreader-desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const defaultSpeedreaderData = {
}

const extractTextToSpeak = () => {
const textTags = ['P', 'DIV', 'MAIN', 'ARTICLE']
const textTags = ['P', 'DIV', 'MAIN', 'ARTICLE', 'H1', 'H2', 'H3', 'H4', 'H5', 'STRONG', 'BLOCKQUOTE' ]

const extractParagraphs = (node) => {
let paragraphs = []
Expand All @@ -66,14 +66,32 @@ const extractTextToSpeak = () => {
return paragraphs
}

const paragraphs = extractParagraphs($(contentDivId))
const getTextContent = (element) => {
if (!element) {
return null
}
const text = element.innerText.replace(/\n|\r +/g, ' ').trim()
if (text.length > 0) {
return text
}
return null
}

const textToSpeak = []
const title = $(metaDataDivId)?.querySelector('.title')
const titleText = getTextContent(title)
if (titleText) {
title.setAttribute('tts-paragraph-index', textToSpeak.length)
textToSpeak.push(titleText)
}

const paragraphs = extractParagraphs($(contentDivId))

for (const p of paragraphs) {
const text = p.innerText.replace(/\n|\r +/g, ' ').trim()
const text = getTextContent(p)
if (text) {
p.setAttribute('tts-paragraph-index', textToSpeak.length)
textToSpeak.push(p.innerText.replace(/\n|\r +/g, ' '))
textToSpeak.push(text)
}
}

Expand All @@ -96,10 +114,6 @@ const highlightText = (ttsParagraphIndex, charIndex, length) => {
'[tts-paragraph-index="' + ttsParagraphIndex + '"]')
if (paragraph) {
paragraph.classList.add('tts-highlighted')
const ttsContent = document.createElement('tts-content')
ttsContent.append(...paragraph.childNodes)
paragraph.append(ttsContent)
document.documentElement.style.setProperty('--tts-highlight-progress', (charIndex / paragraph.textContent.length) * 100 + '%')
}
}

Expand Down

0 comments on commit 5b77d0f

Please sign in to comment.