[Share] Save/Restore Reader Tabs #129
windingwind
started this conversation in
Action Scripts
Replies: 2 comments 2 replies
-
Thanks for the contribution! Thanks! |
Beta Was this translation helpful? Give feedback.
1 reply
-
I am seeking to implement the maintenance of multiple tag groups based on your script. Below is the modified code I have created, but I am encountering two issues:
const Zotero = require("Zotero");
const window = require("window");
const Zotero_Tabs = require("Zotero_Tabs");
const prefsKey = "extensions.actionsTags.customAction.tabGroup";
function getTabGroups() {
let tabGroupsData = Zotero.Prefs.get(prefsKey, true);
if (!tabGroupsData) {
return "[Tab Group] No tabGroups existed";
}
return JSON.parse(tabGroupsData);
}
function saveTabGroup() {
let tabGroups = getTabGroups();
let tabGroup = {};
let tabs = Zotero_Tabs._tabs.filter(
(t) => !!t.data && ["reader", "reader-reloaded", "note"].includes(t.type)
);
let nameAry = [];
for (let tabGroup of tabGroups) {
nameAry.push(tabGroup.name);
}
let name = "";
while (true) {
let inputName = window.prompt('Enter name of new tabGroup');
if (!nameAry.includes(inputName)) {
name = inputName;
break;
}
}
tabGroup.name = name;
tabGroup.tabs = tabs;
tabGroups.push(tabGroup);
let tabGroupData = JSON.stringify(tabGroups);
Zotero.Prefs.set(prefsKey, tabGroupData, true);
return tabGroupData;
}
function clearTabGroups() {
let tabGroups = getTabGroups();
let nameAry = [];
for (let tabGroup of tabGroups) {
nameAry.push(tabGroup.name);
}
let selectedItems = selectItems(nameAry);
let sortedKeys = Object.keys(selectedItems).map(key => parseInt(key));
sortedKeys.sort((a,b) => b - a);
for (let index of sortedKeys) {
tabGroups.splice(index, 1);
}
Zotero.Prefs.set(prefsKey, JSON.stringify(tabGroups), true);
}
function restoreTabGroup() {
let tabGroups = getTabGroups();
let nameAry = [];
let itemIDAry = [];
for (let tabGroup of tabGroups) {
nameAry.push(tabGroup.name);
}
let tabs = tabGroups[nameAry.indexOf(Object.values(selectItems(nameAry))[0])].tabs;
for (let tab of tabs) {
itemIDAry.push(tab.data.itemID);
}
for (let tab of Zotero_Tabs._tabs) {
if (!itemIDAry.includes(tab.data?.itemID) && tab.type != "library") {
Zotero_Tabs.close(tab.id);
}
}
for (let tab of tabs) {
// open tab if not already opened
if (Zotero_Tabs._tabs.some((t) => t.data?.itemID === tab.data?.itemID)) {
continue;
}
if (tab.type == "reader") tab.type = "reader-unloaded"; // force reload
Zotero_Tabs.add(tab);
}
return "[Tab Group] Restored " + tabs.length + " tabs";
}
function selectItems(itemList) {
var io = { dataIn: itemList, dataOut: null };
window.openDialog("chrome://zotero/content/ingester/selectitems.xhtml",
"_blank", "chrome,modal,centerscreen,resizable=yes", io);
return io.dataOut;
}
let options = ["save", "restore", "delete"];
let selectedItems = selectItems(options);
if (!selectedItems) return;
let selected = Object.values(selectedItems)[0];
switch (selected) {
case "save":
saveTabGroup();
break;
case "restore":
restoreTabGroup();
break;
case "delete":
clearTabGroups();
break;
default:
return "option not existed";
}
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
Save/restore reader tabs
See https://forums.zotero.org/discussion/97425/feature-idea-pdf-tab-groups
Inspired by https://gist.github.com/ifree/a91b68dbe1a6927bd9fd8ba4b67aad6a
Action Settings
Event: none
Type: Script
Data:
Beta Was this translation helpful? Give feedback.
All reactions