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!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Rio
  • 14,182
  • 21
  • 67
  • 107

2 Answers2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ephemient
  • 198,619
  • 38
  • 280
  • 391
0

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.

Andy A.
  • 1,392
  • 5
  • 15
  • 28
  • 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 '21 at 06:25
  • It's not a question. I'm a little confused about why you would think that. I gave a partial solution remarking that the function returns the paragraph of the selected text. – EzequielS121 Apr 09 '23 at 00:14