Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>var r = document.createRange() r.selectNode(document.body) window.getSelection().addRange(r) </code></pre> <p>I tried creating a new Greasemonkey script, typing the above code (which I grabbed and edited from <a href="http://www.sitepoint.com/forums/showthread.php?t=459934" rel="nofollow noreferrer">this page</a>), and loading a page.</p> <p>It did select all the text, but for some pages, it becomes unselected immediately. For example, Google's homepage, because the page focuses the search field.</p> <hr> <h2>Update by BA:</h2> <p>This didn't work on Google because it's fighting native scripts. But, by re-running the code at <code>onload</code> and again after, we can preserve the selection.</p> <p>Also, if the native script sets focus to an <code>input</code> or <code>textarea</code>, we must fight that.</p> <p>So, a Greasemonkey script that incorporates all these ideas, and seems to work is:</p> <pre><code>//--- Save this as "SelectWholePage.user.js" and install with Greasemonkey. // // ==UserScript== // @name Select a whole page // @namespace google.com // @description Selects a whole page (equivalent to 'Ctrl-A'). // @include http://www.google.com/* // ==/UserScript== // /*--- Run the main function 3 times (when DOM ready, at load and just after load) because page javascript will often reset the focus and selection. */ LocalMain (); window.addEventListener ( "load", function(evt) { LocalMain (); window.setTimeout (LocalMain, 222); }, false ); function LocalMain () { var WholePage = document.createRange (); WholePage.selectNode (document.body); window.getSelection ().addRange (WholePage); var aInputs = document.getElementsByTagName ("input"); for (var J = aInputs.length-1; J&gt;0; J--) aInputs[J].blur (); document.body.focus (); } </code></pre>
 

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