Note that there are some explanatory texts on larger screens.

plurals
  1. POgetSelection.toString() - without using toString()?
    primarykey
    data
    text
    <p>I need to extract text from a selection and send it to a TTS service. The TTS service will return a stream URL and a set of indices for each word, indicating where they start and end (in both time and text).</p> <p>When the user plays the stream I want to highlight each word as they are read out. To do that I can't just use the text indices for each word, because they can not get me back to the original HTML nodes - hence why I can't use <code>toString()</code> which is strictly text.</p> <p>What I'm doing so far is creating a TreeWalker using the start and end containers of the range object and using that to extract all the text nodes in the range.</p> <p><strong>Problem:</strong> <code>window.getSelection().toString()</code> inherently ignores nodes that are not displayed. That includes <code>&lt;script&gt;</code> nodes, <code>&lt;style&gt;</code> node, nodes with <code>display: none;</code> and the likes. Using TreeWalker doesn't.</p> <p>I know I can manually skip all of these nodes in the TreeWalker (like suggested in <a href="https://stackoverflow.com/questions/7256608/getselection-without-alt-attribute-and-scripts-in-it">getSelection without alt attribute and scripts in it?</a>), but it can become quite complex really fast (especially checking the visibility of each node).</p> <p>Before going into this I wanted to ask, if there are any new methods or libraries available that have emerged since the question I linked was answered?</p> <p>I don't intend the code to be cross browser and I'm using plain Javascript (i.e. no jQuery).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload