How can I get the selected text of the current document in the logseq plugin?

I want to develop a plugin that can replace the currently selected text with the name of the note, how can I get the selected text content of the current document in the logseq plugin?

document.getSelection().toString()

Thanks mentaloid, I used the following hotkey to set the code to display the selected text in the alert

const reNameshortcutKey = "ctrl+y";    

logseq.App.registerCommandShortcut(
  {
    binding: reNameshortcutKey,
  },
  () => {

    // alert("触发块删除")
    // toolkit_tags_collect();

    const selection = document.getSelection();
    // if (selection && selection.toString()) {
    if (selection) {  
      const selectedText = selection.toString();
      // console.log(`Selected text: ${selectedText}`);
      alert ("SelectedText is : "+selectedText);
    }

  }
);

But the selected text can’t be displayed, how can I change it?

Make sure that document is the same document with the one the selected text is in.

I’m a beginner in java and typescript, and I don’t know much about the operation mechanism here, how can I modify my code above to achieve my purpose?

Apparently your plugin runs inside an iframe, which is a child of Logseq’s main window. To access Logseq’s main window, use parent (or window.top). All in all, one way is parent.getSelection().toString()

Thank you so much! parent.getSelection().toString() did solve the problem that has been bothering me for a long time!