Skip to content

Instantly share code, notes, and snippets.

@ifree
Last active November 18, 2025 06:19
Show Gist options
  • Select an option

  • Save ifree/a91b68dbe1a6927bd9fd8ba4b67aad6a to your computer and use it in GitHub Desktop.

Select an option

Save ifree/a91b68dbe1a6927bd9fd8ba4b67aad6a to your computer and use it in GitHub Desktop.
Zotero tab save/load support
async function load_tabs_from_selected_item()
{
let opened_items = Zotero_Tabs._tabs.filter(t => !!t['data']).map(t=>t['data']['itemID'])
let selectedItems = ZoteroPane.getSelectedItems();
if (!selectedItems.length) {
throw new Error("Please select an item first.");
}
for(let item of selectedItems)
{
if(item.itemType == "document" && item.getField("extra"))
{
let tabs = JSON.parse(item.getField("extra"))
for(let tab of tabs)
{
// open tab if not already opened
const tabId = tab['data']['itemID']
if(tabId && (opened_items.indexOf(tabId) > -1))
{
continue;
}
let tab_type = tab['type']
if(tab_type == 'reader')
tab_type = 'reader-unloaded' // force reload
const libraryId = tab['data']['libraryID']
const itemKey = tab['data']['key']
if(!libraryId || !itemKey){
continue
}
let item_id = Zotero.Items.getIDFromLibraryAndKey(libraryId, itemKey)
Zotero_Tabs.add({
type: tab_type,
title: tab['title'] || '',
data: {
itemID: item_id
}
});
}
}
}
}
async function save_tabs_to_item(tab_store) {
let tabs = Zotero_Tabs._tabs
.filter(t => !!t['data'])
.map(t => ({
'type': t['type'],
'title': t['title'],
// itemID is not enough, as it is not unique across libraries
'data': Zotero.Items.getLibraryAndKeyFromID(t['data']['itemID']),
}));
let tab_content = JSON.stringify(tabs);
tab_store.setField("abstractNote", tabs.map(t => t['title']).join(', '))
tab_store.setField("extra", tab_content)
return await tab_store.saveTx()
}
async function save_tabs_to_selected_item() {
let item = ZoteroPane.getSelectedItems()[0];
if (!item) {
throw new Error("Please select an item first.");
}
await save_tabs_to_item(item)
}
async function save_tabs_to_selected_collection() {
let collection = ZoteroPane.getSelectedCollection();
if (!collection) {
throw new Error("Please select a collection first.");
}
let item = new Zotero.Item("document");
item.libraryID = collection.libraryID;
item.setField("title", "__tab_history_" + new Date().toISOString().replace(/:/g, "_"));
await save_tabs_to_item(item)
await Zotero.DB.executeTransaction(async () => { await collection.addItems([item.id]) })
}
//execute save_tabs_to_selected_collection() or load_tabs_from_selected_item()
@ifree
Copy link
Author

ifree commented Mar 12, 2024

@munael Seems that zotero 7 will add support for tab groups, see. Btw, the custom Zutilo can be installed by zipping(+renaming to xpi) plugin folder and install from zotero

@lewallen
Copy link

This used to work for me but now I can succesfully save an item, but "load_tabs_from_selected_item()" doesn't do anything. I'm running Zotero Version 7.0.29 on a Mac. Do you know of any reason it would have stopped working, or have any advice for debugging it? Thank you!

@ifree
Copy link
Author

ifree commented Nov 18, 2025

Hi @lewallen , I have made a quick fix to address you problem

@lewallen
Copy link

lewallen commented Nov 18, 2025

@ifree -- Thank you, it works now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment