Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dev:client_coding:javascript_api [2020/10/17 21:05] – [Batch Editing] dstillman | dev:client_coding:javascript_api [2024/08/21 03:55] (current) – [Zotero JavaScript API] dstillman | ||
---|---|---|---|
Line 3: | Line 3: | ||
Whereas Zotero' | Whereas Zotero' | ||
- | Note that the (mostly user-contributed) | + | Note that this documentation of the JavaScript API is not comprehensive. If you use the JavaScript API in ways beyond what's described here, please consider expanding this wiki page or suggesting changes in the forums. |
- | + | ||
===== Running Ad Hoc JavaScript in Zotero ===== | ===== Running Ad Hoc JavaScript in Zotero ===== | ||
Line 36: | Line 34: | ||
Non-window scope applies to lower-level code that doesn' | Non-window scope applies to lower-level code that doesn' | ||
- | Overlays and windows | + | Windows |
- | To access Zotero functionality from your own extension, you will need access to the core '' | + | To access Zotero functionality from your own extension, you will need access to the core '' |
<code html> | <code html> | ||
Line 87: | Line 85: | ||
] | ] | ||
); | ); | ||
- | var itemID = await item.save(); | + | var itemID = await item.saveTx(); |
return itemID; | return itemID; | ||
</ | </ | ||
Line 216: | Line 214: | ||
s.addCondition(' | s.addCondition(' | ||
s.addCondition(' | s.addCondition(' | ||
+ | </ | ||
+ | |||
+ | === Search by creator === | ||
+ | |||
+ | <code javascript> | ||
+ | var name = ' | ||
+ | s.addCondition(' | ||
</ | </ | ||
Line 236: | Line 241: | ||
results: | results: | ||
- | <code javascript> | + | <code javascript> |
This returns the item ids in the search as an array. The next thing to do is to get the Zotero items for the array of IDs: | This returns the item ids in the search as an array. The next thing to do is to get the Zotero items for the array of IDs: | ||
- | <code javascript> | + | <code javascript> |
==== Managing citations and bibliographies ==== | ==== Managing citations and bibliographies ==== | ||
Line 340: | Line 345: | ||
return fulltext; | return fulltext; | ||
</ | </ | ||
+ | |||
+ | ==== File I/O ==== | ||
+ | |||
+ | === Getting the contents of a file === | ||
+ | |||
+ | <code javascript> | ||
+ | var path = '/ | ||
+ | var data = await Zotero.File.getContentsAsync(path); | ||
+ | </ | ||
+ | |||
+ | === Saving data to a file === | ||
+ | |||
+ | <code javascript> | ||
+ | var path = '/ | ||
+ | var data = "This is some text."; | ||
+ | await Zotero.File.putContentsAsync(path, | ||
+ | </ | ||
+ | |||
==== To Do === | ==== To Do === | ||
Line 438: | Line 461: | ||
return "No items found"; | return "No items found"; | ||
} | } | ||
- | for (let id of ids) { | + | await Zotero.DB.executeTransaction(async function () { |
- | let item = Zotero.Items.get(id); | + | |
- | item.addTag(tag, | + | let item = Zotero.Items.get(id); |
- | await item.saveTx(); | + | item.addTag(tag, |
- | } | + | await item.save({ |
+ | skipDateModifiedUpdate: | ||
+ | }); | ||
+ | } | ||
+ | }); | ||
return ids.length + " tag(s) updated";</ | return ids.length + " tag(s) updated";</ | ||