Scroll to text fragment seems like a separate matter—the core feature is to highlight text, not scroll to it, yes? Like the crowdsourced highlights in Kindle?
I was involved in a project about ten years ago that involved highlighting arbitrary runs of HTML in an online Bible. The browser Selection and Range API was a mess back then—looks like it's come a long way, and may actually be reliable: https://developer.mozilla.org/en-US/docs/Web/API/Selection
If there are still any cross-browser gotchas, the Rangy JS library may help paper them over: https://github.com/timdown/rangy
We used the selection interface to inject spans into the DOM, then styled those spans with classes. We had a set of colors that customers could use to highlight arbitrary Bible passages. We even supported overlapping colors! 😅
Making it work on arbitrary sites with arbitrary styling will be a nightmare. For your use case, you could follow Kindle and do a simple underline or other non-color decoration.
Also, because the web evolves over time, you'll have the problem of text that shifts on you, getting edited, completely rewritten, or outright moved. (People forget that *Cool URIs Don't Change*!) There's only so much one can do about this.
In our application, we controlled the text, so we could use a simple addressing system based on chapter/verse number and character offset. You won't have it so easy. :(
I saw a presentation on this problem a number of years ago, and the advice from someone with experience was to "chunk the text and hash the chunks". If a chunk of text changes, the hash changes, and your highlights disappear.
Looks like there's been movement toward a robust Text fragment linking interface, but it's not widely supported: https://developer.mozilla.org/en-US/docs/Web/Text_fragments
Prior work you might look into for inspiration:
- "Purple" numbers (paragraph-level addressing): https://eekim.com/software/purple/purple.html
- Dissenter browser extension (a centralized Gab-led effort to add "a comment section for the internet"—might be defunct?)
Good luck!



