Last active
March 12, 2024 15:47
-
-
Save ifree/a91b68dbe1a6927bd9fd8ba4b67aad6a to your computer and use it in GitHub Desktop.
Zotero tab save/load support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) }) | |
} |
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)?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to
save_tabs_to_selected_item
,save_tabs_to_selected_collection
,load_tabs_from_selected_item