79797377

Date: 2025-10-23 03:56:05
Score: 0.5
Natty:
Report link

When you got a range object from selection(like `myRange=mySelection.getRangeAt(0)`), send the range to this function:

function getSelectedText(range: Range):string {
    const div = document.createElement('div');
    div.appendChild(range.cloneContents());
    // we need get style by window.getComputedStyle(), 
    // so, it must be append on dom tree.
    // here is <body>,or some where that user can't see.
    document.body.appendChild(div);
    const it = document.createNodeIterator(div);
    let i;
    while (i = it.nextNode()) {
        // remove `user-select:none` node
        if (i.nodeType === Node.ELEMENT_NODE && window.getComputedStyle(i).userSelect === 'none') i.remove();
    }
    const cpt: string = div.innerText;
    div.remove();
    return cpt;
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: callmenp