Retrieve highlighted text in Google Docs

Google Docs Chrome extension fails to capture selected text. For instance, this snippet does not work:

const chosenText = fetchHighlightedContent();

Which alternative method can capture the highlighted portion?

hey, try using documet.getSelection() triggered on mouseup. it captures the text more reliably than some extensions. hope it helps!

I have experimented with various methods to capture the highlighted portion in Google Docs. Rather than relying solely on an extension’s mechanism, my experience suggests that using window.getSelection() in combination with a slight delay in execution tends to be more reliable. This approach gives the selection time to stabilize before attempting to retrieve the text. Additionally, monitoring events like selectionchange can help determine the most appropriate moment for text extraction. This method has proven more robust in different contexts within the document.