I do this in my Docs to Markdown add-on, which exists as a bound script, but is now published as an add-on. It has code to use the openByUrl() function, but in practice it using just uses getActiveDocument since an add-on acts like a bound script. You can see the selection handling in
this part of the source. It's a little bit different from what they're doing in the translation example, but it works well for partial selections.