zotero://select: honour `itemKey` inside a collection

I'm writing an Obsidian plugin that lists a note's sources and has a button to select them all in Zotero, using one link: `zotero://select/library/items?itemKey=A,B,C`. That works, but `SelectExtension` calls `selectLibrary()` first, so Zotero always leaves the collection the user is working in. I'd like to select the note's sources that are in the open collection without leaving it.

The link for that seems to exist already: `zotero://select/library/collections/COLKEY/items?itemKey=A,B,C` (and `groups/ID/collections/COLKEY/items?...`). The router matches it, and `SelectExtension` selects the collection before selecting the items. But in `Zotero.API.getResultsFromParams` (`chrome/content/zotero/xpcom/api.js`), the `collections` scope narrows the results only by `objectKey`, never by `itemKey`. So the link selects every item in the collection instead of the listed ones.

Proposed change, in the `case 'collections':` branch:

```js
results = col.getChildItems();
if (params.objectKey) {
let item = results.find(item => item.key == params.objectKey);
results = item ? [item] : [];
}
else if (params.itemKey) {
let keys = new Set(params.itemKey);
results = results.filter(item => keys.has(item.key));
}
```

The `searches` scope has the same gap and could get the same filter. Today a link with `itemKey` in a collection selects the whole collection, which is unlikely to be what anyone means, so this shouldn't break existing links.
Sign In or Register to comment.