====== Zotero JavaScript API ======
Whereas Zotero's [[dev:web_api|Web API]] allows read and write access to online Zotero libraries, it is also possible to access the local Zotero client through the local JavaScript API. (It is also possible to [[dev/client_coding/direct_sqlite_database_access|directly access the local SQLite database]], but that approach is much more fragile.)
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 =====
Zotero includes an option to run arbitrary privileged JavaScript:
- In the Tools → Developer menu, select Run JavaScript. Opening the Error Console, which appears in the same menu, will also be helpful.
- In the window that opens, enter some JavaScript in the Code textbox and click Run or press Cmd-R/Ctrl-R.
When running **asynchronous** code containing ''await'', the entered code is wrapped in an [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function|async function]], allowing you to wait for the resolution of promises returned by functions. Most Zotero functions that access the database, disk, or network are asynchronous. In this mode, the value of a ''return'' statement will be displayed in the right-hand pane upon successful completion. E.g. returning the currently selected item(s)
var items = Zotero.getActiveZoteroPane().getSelectedItems();
return items;
In **synchronous** mode, the value of the final line will appear in the right-hand pane. The same result as above could be achieved in synchronous mode with
var items = Zotero.getActiveZoteroPane().getSelectedItems();
items;
===== Zotero Code Architecture =====
==== Window Scope vs. Non-Window Scope ===
Zotero code exists in either window scope and non-window scope.
Window scope applies to code that runs within either the main Zotero window or a secondary window (e.g., the Advanced Search window). It has access to the window's DOM and can interact with the UI. The main window-scope object in Zotero is ''ZoteroPane'', from zoteroPane.js, which controls most interactions triggered in the main Zotero window.
Non-window scope applies to lower-level code that doesn't have access to the DOM. This includes the core ''Zotero'' object, which contains all other non-window code, including the data layer used for retrieving and modifying library data. In Zotero, non-window code is contained within the ''xpcom'' subdirectory.
Windows in Zotero can import the core ''Zotero'' object via the include.js script. Zotero methods can then be called from anywhere within the window's scope simply by calling, for example, ''var item = Zotero.Items.get(1);''.
To access Zotero functionality from your own extension, you will need access to the core ''Zotero'' object. If your extension operates within the main Zotero window, you already have access to the ''Zotero'' object and don’t need to take further steps. Otherwise, you can import the Zotero object into other windows by including the script %%chrome://zotero/content/include.js%% within an HTML or XUL file: