client scripting API with Zotero standalone (v5)
This page (https://www.zotero.org/support/dev/client_coding/javascript_api) shows how to use the "zotero" js object to do scripting & batch processing of the library. However, it's written in the context of the FF add-on, where it's possible to poke around in the js internals.
Is it possible to do *client* js scripting with the new standalone Zotero? If so, how does one get a hold of the "zotero" js object?
Is it possible to do *client* js scripting with the new standalone Zotero? If so, how does one get a hold of the "zotero" js object?
Install that, go to Config Editor in the Advanced pane of the preferences and set devtools.chrome.enabled to true, and then restart Zotero. You'll then see a Run JavaScript option in the Tools menu, which will open a window that lets you run the sort of privileged JavaScript on the linked page (though what's there is quite out of date by now, so we'll need to update that).
I hacked this together very quickly, so we can certainly improve on it, but it will hopefully do the trick for the moment. For now, you'll want to keep the Error Console (which will also appear as an option in Tools) open to see any errors in the code you run.
I've also updated the linked page to bring most of the examples up to date for Zotero 5, though, as explained there, the source code is still the best reference for now.
You zip that directory up and rename it's extension to ".xpi". This xpi you install as you usually would. Set up a password as per https://github.com/retorquere/zotero-better-bibtex/tree/master/test/fixtures/debug-bridge .
Once installed (and Zotero restarted), you now have an endpoint that listens on http://127.0.0.1:23119/debug-bridge/execute . You can POST (using curl for example) a text body to that URL: or your code will be used as the body of an async function, and the query variables will be available as "query", so the first call would be equivalent to executing if your code errors out, you will get the error back as the response body, and the response code will be 500
if your code executes successfully, you get the JSONified return value of your script (if it doesn't return anything, "null" is returned), and the response code will be 201.
Note that a lot of of the Zotero uses coroutine/yield instead of async/await. I *think* coroutines return promises you can just await on. But in the code you post, you cannot use yield; use await (or old-school .then) instead.
Any POST to the endpoint will already automatically await Zotero.Schema.schemaUpdatePromise before running your code, so that's not necessary, that was just to show how await works in this context; technically, the curl call above is equivalent to Note that if you launch Zotero and immediately after that access this endpoint, you will first get "Failed to connect" for a while during Zotero startup, and after that it will take about 5 seconds for the first script to execute as it is waiting for Zotero.Schema.schemaUpdatePromise to resolve. After that resolves it is not exactly a no-op but can be thought of as such.
In your scripts you can run whatever you would otherwise run in an extension. Zotero.debug(...) works, you can access the database, everything.
If you run long-running scripts this way that do not call await regularly, Zotero will stall and you will get popups with "this script is running for a long time, kill Zotero?" (or something to that effect).
Once done you can either remove or disable the extension but it will remain active until you restart Zotero.
Your debug-bridge certainly has lots of other uses, though.
I was able to install the add-on successfully, I think, and it looks like it loads according to the debug logs, but I am having trouble running anything (getting errors like "Endpoint does not support content-type" when I try to execute the hello world example).
In more detail:
I installed debug-bridge-5.1.105.emile.limonia.xpi
After restarting, the debug log shows: When I go to http://127.0.0.1:23119/debug-bridge in a browser, the response is: The exact hello world example I tried running with curl is:
curl -X POST -H "Content-Type: text/plain" --data "await Zotero.Schema.schemaUpdatePromise; return 'hello ' + query.world" 'http://127.0.0.1:23119/debug-bridge/execute?foo=bar&baz=quux&world=world'
Some Zotero logs from those requests: Any ideas for what might be wrong? Or other debug info that would be useful?