简体   繁体   中英

Get selection from Google Docs

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM