I'm using a script from Tim Down to create a highlight. Now, I'd like the user to click the browser action again to remove it.
I thought I could add another if
statement to this snippet:
function makeEditableAndHighlight(colour) {
var range, sel = window.getSelection();
if (sel.rangeCount && sel.getRangeAt) {
range = sel.getRangeAt(0);
}
document.designMode = "on";
if (range) {
sel.removeAllRanges();
sel.addRange(range);
}
// Use HiliteColor since some browsers apply BackColor to the whole block
if (!document.execCommand("HiliteColor", false, '#FFFF00')) {
document.execCommand("BackColor", false, '#FFFF00');
}
if (!document.execCommand("HiliteColor", true, '#FFFF00')) { // Added this logic
document.execCommand("removeFormat", false, null);
}
document.designMode = "off";
}
My thinking was that if "HiliteColor"
returned true
, it would remove the formatting, but it isn't working. Any thoughts?
Edit
After doing more reading, I learned that the boolean in the execCommand
doesn't have anything to do with returning a value. How can I improve my logic to reverse the background color? Is it even doable?