得到点击的单词

有的时候需要知道当前点击的单词是什么(比如说需要实时翻译的时候)

export function get_cur_word():string{
    let sel = window.getSelection()
    if(sel?.anchorNode){
        let text = sel.anchorNode.textContent || ""
        if (text == "") return "";

        let pos = sel.anchorOffset
        let len = text?.length || 0
        let end = pos
        while(end <len && text[end] !=' '){
            end++;
        }
        while(pos >=0 && text[pos] !=' '){
            pos--;
        }
        return text.substr(pos,end-pos);
    }
    return "";
}