Adding context menu items for text selection in PDF reader?

21 views
Skip to first unread message

José Fernandes

unread,
Dec 24, 2025, 8:00:21 AM (7 days ago) Dec 24
to zotero-dev

Hello,

I'm trying to add a custom menu item to the context menu when text is selected in the PDF reader, but haven't been able to get it working.

What I've tried:


The new MenuManager API - Checked the source at https://github.com/zotero/zotero/blob/main/chrome/content/zotero/xpcom/pluginAPI/menuManager.js but the VALID_TARGETS array doesn't include any targets for PDF reader content area menus.

Reader.registerEventListener with createViewContextMenu:

Zotero.Reader.registerEventListener(
  'createViewContextMenu',
  (event) => {
    const { append } = event;
    append({
      label: 'My Custom Action',
      onCommand: () => { /* ... */ }
    });
  },
  'my-plugin-id'
);
The event fires but I can't access the selected text.

Annotation context menu - This works fine:
Zotero.Reader.registerEventListener(
  'createAnnotationContextMenu',
  (event) => { /* This works */ },
  'my-plugin-id'
);


Questions:

  1. Is it possible to add menu items to the text selection context menu in the PDF reader?
  2. If yes, how can I access the selected text?
  3. If not currently supported, could this be considered as a feature request? It would be useful for various plugin functionalities.


Environment:
Zotero 8.0-beta.21+
Testing the toolbar button and annotation menus work fine, just can't get text selection menu to work


Thanks for any guidance!



Xiangyu Wang

unread,
Dec 24, 2025, 9:04:02 AM (7 days ago) Dec 24
to zoter...@googlegroups.com
> Is it possible to add menu items to the text selection context menu in the PDF reader?

Yes. your first code will add the context menu item.

> If yes, how can I access the selected text?

Use `event.reader._iframeWindow.wrappedJSObject._reader._primaryView._selectionRanges` (suppose it's in pdf reader's primary view; you may want to add further conditions e.g `event.reader.type === 'pdf'` or `event.reader._iframeWindow.wrappedJSObject._reader._state.primary` to decide what logic to use).

> could this be considered as a feature request?

We will consider adding it directly to the `event.params` so that plugins can access it directly. For now, please try the solution above.

--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/zotero-dev/8f7c790a-7ae7-4a2a-818c-8c47d08cb0d9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages