Skip to content

Instantly share code, notes, and snippets.

@ifree
Last active March 12, 2024 15:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifree/a91b68dbe1a6927bd9fd8ba4b67aad6a to your computer and use it in GitHub Desktop.
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
if(opened_items.indexOf(tab['data']['itemID']) > -1)
{
continue;
}
let tab_type = tab['type']
if(tab_type == 'reader')
tab_type = 'reader-unloaded' // force reload
let item_id = Zotero.Items.getIDFromLibraryAndKey(tab['data']['libraryID'], tab['data']['key'])
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]) })
}
@ifree
Copy link
Author

ifree commented Jul 3, 2023

How to

  1. Create a collection to store tab histories image
  2. Tools -> Developer -> Run Javascript
  3. Execute desired function save_tabs_to_selected_item, save_tabs_to_selected_collection, load_tabs_from_selected_item
  4. profit

@ifree
Copy link
Author

ifree commented Jul 3, 2023

@ifree
Copy link
Author

ifree commented Sep 15, 2023

Instruction zotero_tabs

@munael
Copy link

munael commented Mar 11, 2024

The custom Zutilo integration doesn't seem to have built releases :T

Regarding this implementation, would it be possible to have an option that adds the parent documents of the open items to a sub-collection as children (as you can do manually, and they would be shared among several sub-collections)?

@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

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