I would like to select all the text on a page, the software with the same result as if I pressed the button the combo the Ctrl + A .
The problem with use document.getSelection().selectAllChildren(body)
is that the selection will also include text nodes that cannot be selected by the user, i.e. <script> </script>
or nodes for which exists user-select:none
, defined in CSS:
<div style="-moz-user-select:none">
Will be selected </div>
There is a method modify
for selection objects, which can be used as follows:
selection.modify("extend", "forward", "documentboundary");
to extend the selection from the beginning of the document to its end, which will ignore any script element or style element and elements with -moz-user-select:none
- unfortunately, Firefox does not allow documentboundary
as 3. an argument and word
does not help much.
Is there a quick way to do this? Only need to work in Firefox.
EDIT (not a very good solution): Select the first node text, then use it selection.modify('extend', 'forward', 'line')
several times, and selection.focusNode
not match the last node text - but depending on the length of the document, it takes a few seconds!
EDIT: selection.selectAllChildren
Will work in Chrome, where text elements with user-select:none
will not be selected - unfortunately, there is a different behavior in FF.
EDIT: , contenteditable
, ;)