A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

[Proposal] DOM onhighlighttext Event API

fauzan121002
2021-06-01

I suggest to add a new event in the Javascript DOM, namely onhighlighttext.

Example :

document.addEventListener("onhighlighttext", function(){
  alert("wow you highlighted me sir!");
});

Actually we can do this with getSelection by onmouseup or keyup , but it ends up with a syntax that is not neat and hard to read.

var t = "";
function gText(e) {
    t = (document.all) ? document.selection.createRange().text : document.getSelection();
    document.getElementById('input').value = t;
}

document.onmouseup = gText;
if (!document.all) document.captureEvents(Event.MOUSEUP);

Besides that we can use selectionchange, but selectionchange as the latter fires so many events (up to the chars selected), you have to wait some arbitrary period to get the final selection.

I can’t find any ideas and proposals about this.

fauzan121002
2021-06-01

Move from here

tomayac
2021-06-08

Looks like this is better discussed in the context of a new Issue for the CSS Custom Highlight API Module Level 1.