1

Is there a way around the fact that

window.getSelection()

does not seem to work in Google Docs? I want to capture text from a highlighted selection from Google Docs. My implementation is with Chrome Extensions, but that is not as relevant as the fact that window.getSelection() doesn't seem to work there (though it does elsewhere).

Thanks!

3

As mentioned on Docs Blog - What’s different about the new Google Docs?, Google Docs eschews the browser's native capabilities and implements everything — including text formatting and positioning, cursor movement, and selection handling — in its own Javascript.

Since Docs doesn't provide a public in-browser API to the document currently being edited, you'll either have to dig through its private innards or give up.

2
  • Good luck with digging through the private innards (I've tried). Here's some reverse-engineered source: github.com/benjamn/kix-standalone
    – Tim Down
    Jul 2, 2012 at 8:27
  • This doesn't explain why window.getSelection() fails. May 15, 2016 at 22:04
-1

I got the selected text with this:

let selection = DocumentApp.getActiveDocument().getSelection();
console.log(selection.getRangeElements()[0].getElement().asText().getText());

But it gives you the whole paragraph.

1
  • Welcome to SO. Your answere sounds a bit like a question. If so, please make a own question instead of an answere on an old one. You can link to this then.
    – Andy A.
    May 18, 2021 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.