Skip to content

Commit

Permalink
Title reading.
Browse files Browse the repository at this point in the history
  • Loading branch information
boocmp committed Aug 31, 2023
1 parent 12d090b commit d8034df
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 @@ -48,7 +48,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 @@ -68,14 +68,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 @@ -98,10 +116,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 d8034df

Please sign in to comment.