Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving background-color from all the text in the page, regardless whether we have a selection or not
    primarykey
    data
    text
    <p>We have the following Javascript code:</p> <pre><code>function makeEditableAndHighlight(colour) { sel = window.getSelection(); if (sel.rangeCount &amp;&amp; sel.getRangeAt) { range = sel.getRangeAt(0); document.designMode = "on"; sel.removeAllRanges(); sel.addRange(range); } if (!document.execCommand("HiliteColor", false, colour)) { document.execCommand("BackColor", false, colour); } document.designMode = "off"; } // This is the highlighting function. It takes a color as an argument. function highlight(colour) { var range, sel; if (window.getSelection) { try { if (!document.execCommand("BackColor", false, colour)) { makeEditableAndHighlight(colour); window.getSelection().removeAllRanges(); } } catch (ex) { makeEditableAndHighlight(colour) } } else if (document.selection &amp;&amp; document.selection.createRange) { range = document.selection.createRange(); range.execCommand("BackColor", false, colour); } } // This returns the highlight to transparent (no highlighting) when the user clicks away in the document. function body() { document.getElementsByTagName("body")[0].addEventListener( "click", function(event){ highlight('transparent'); } ); } </code></pre> <p>It works like this:</p> <p>When there's text selected in a page, if you press a button (say an extension popup) then that text gets a yellow highlight and is automatically deselected.</p> <p>That's where the problem comes in.</p> <p>Because the text is automatically deselected, I cannot revert the highlight back to the same text (since there's no selection anymore)</p> <p>So the focus of this question is on the last part, namely:</p> <pre><code>function body() { document.getElementsByTagName("body")[0].addEventListener( "click", function(event){ highlight('transparent'); } ); } </code></pre> <p>This only works if I select the same text and click anywhere in the document.</p> <p>How I would like it to work is to be able to click anywhere in the document, and any highlighted text to be set back to original (or transparent if original not possible).</p> <p>I'm aiming for any text because I have no selection here.</p> <p>So basically this function should work like this:</p> <p>if selection present and button pressed > highlight the selection and automatically remove all selections.</p> <p>when a click is registered anywhere in the page, remove any highlighting on any text, regardless of whether there's a selection or not.</p> <p>Again, this code seems to work very good up until the last part, the 'removing any highlight present' part.</p>
    singulars
    1. This table or related slice is empty.
    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