Last active
May 19, 2021 21:19
-
-
Save jryans/f2631cf3b2c500817cdf79a791209bca to your computer and use it in GitHub Desktop.
Zotero: Recapture web snapshot
This file contains hidden or 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
var search = new Zotero.Search(); | |
search.addCondition('itemType', 'is', 'webpage'); | |
var itemIDs = await search.search(); | |
var items = await Zotero.Items.getAsync(itemIDs); | |
var itemsByAttachmentCount = {}; | |
for (let item of items) { | |
var attachments = item.getAttachments(); | |
if (!itemsByAttachmentCount[attachments.length]) { | |
itemsByAttachmentCount[attachments.length] = []; | |
} | |
itemsByAttachmentCount[attachments.length].push(item); | |
} | |
// Check for items with unusual attachment counts | |
// var itemsByAttachmentCountSummary = {}; | |
// for (let [length, array] of Object.entries(itemsByAttachmentCount)) { | |
// itemsByAttachmentCountSummary[length] = array.length; | |
// } | |
// Focus on webpage items with 1 attachment | |
items = itemsByAttachmentCount[1] || []; | |
var itemsToSnapshot = []; | |
// Look for attachments with multiple files | |
for (let item of items) { | |
var attachmentID = item.getAttachments()[0]; | |
var attachment = await Zotero.Items.getAsync(attachmentID); | |
var hasMultipleFiles = await Zotero.Attachments.hasMultipleFiles(attachment); | |
if (hasMultipleFiles) { | |
itemsToSnapshot.push(item); | |
} | |
} | |
Zotero.debug({ | |
totalItems: items.length, | |
itemsToSnapshot: itemsToSnapshot.length, | |
}) | |
// Process a fixed number of items to snapshot | |
var snapshotsLeft = Math.min(itemsToSnapshot.length, 1); | |
while (snapshotsLeft) { | |
var item = itemsToSnapshot.shift(); | |
var libraryID = item.libraryID; | |
var parentItemID = item.id; | |
var url = item.getField("url"); | |
var oldAttachmentID = item.getAttachments()[0]; | |
Zotero.debug(`Taking snapshot: ${item.getField('title')}`) | |
try { | |
await Zotero.Attachments.importFromURL({ | |
libraryID, | |
parentItemID, | |
url, | |
title: "Snapshot", | |
contentType: "text/html", | |
}); | |
await Zotero.Items.trashTx(oldAttachmentID); | |
Zotero.debug(`Snapshot done: ${item.getField('title')}`) | |
} catch (e) { | |
Zotero.debug(e); | |
} | |
snapshotsLeft--; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment