Is it possible to obtain the precise coordinates of the selected text in a PDF document through the plugin

93 views
Skip to first unread message

tonymmg

unread,
Apr 27, 2023, 12:22:38 AM4/27/23
to zotero-dev
I'm sorry for not reading the guide before, and posting the question to the Forum. Here is my original question

I am learning how to develop a zotero plugin.

Specifically, the purpose is to retrieve the coordinates of the selected text in relation to the PDF page, and subsequently display relevant hints proximal to the text. However, such an outcome has yet to be achieved.

I have attempted to obtain the 'selection', then get the `range` by `selection.getRangeAt(0)`. Unfortunately, this approach has been hindered by the persistent outcome whereby `Selection.isCollapsed` is always set to `true`.

Consequently, coordinates for the selected text are consistently registering as 0.

Several methods have been employed in an attempt to obtain the 'selection', including:
- `readerinstance._iframeWindow?.getSelection()`
- `readerinstance._iframeWindow?.document.getSelection()`
- `window.getSelection()` which listens to the `selectionchange` event.

These efforts have proved futile. As such, I am seeking further guidance or recommendations to resolve this issue.

Thank you in advance for your enthusiasm~

Martynas Bagdonas

unread,
Apr 27, 2023, 8:17:17 AM4/27/23
to zotero-dev
Zotero PDF reader doesn't utilize native browser selection. Instead, it performs all the necessary calculations and renders selections on a canvas. Selected ranges are stored internally, but unfortunately, the variable isn't exposed.

Future versions of the Zotero PDF reader will enable plugins to easily access information like this.

In the meantime, the approximate selection position can be determined using the bounding box of the 8-color selection popup: document.querySelector('.selection-popup').getBoundingClientRect().

wyy...@gmail.com

unread,
Apr 27, 2023, 8:29:51 AM4/27/23
to zotero-dev
Thank you, I get it. Looking forward to next version~
Reply all
Reply to author
Forward
Message has been deleted
0 new messages