This is an old revision of the document!


Zotero has a built-in HTTP server designed to communicate with the Safari and Chrome extensions, called Connector. This HTTP server is enabled by default in Zotero Standalone and it can be enabled for Zotero in Firefox by setting extensions.zotero.connector.enabled to true in about:config. See the knowledge base article.

The connector server defines four endpoints by default:

  • /translate/list
  • /translate/detect
  • /translate/save
  • /translate/select

These endpoints and the server in general are defined in the Zotero code at https://www.zotero.org/trac/browser/extension/trunk/chrome/content/zotero/xpcom/connector.js#L764

Extending the Connector Server

The following code registers an additional endpoint with the built-in connector HTTP server.

This script must be run with chrome privileges. The best way to do this is to create an extension that registers an XPCOM service to run at Firefox startup. Firefox 4 users also have the option of creating a bootstrapped extension. Alternatively, you could register the endpoint from a overlay or from a chrome URI that you load manually.

Once registered, going to http://127.0.0.1:23119/myendpoint in a web browser should produce a page containing “Hello world.”

When the server receives a request for a given endpoint, it calls the init() method of the specified object, passing three arguments:

  • method - the method of the request (“GET” or “POST”)
  • data - the query string (for a GET request) or POST data (for a POST request)
  • sendResponseCallback - a function to send a response to the HTTP request. This can be passed a response code alone (e.g., sendResponseCallback(404)) or a response code, MIME type, and response body (e.g., sendResponseCallback(200, “text/plain”, “Hello World!”))
var Zotero = Components.classes["@zotero.org/Zotero;1"]
	.getService(Components.interfaces.nsISupports)
	.wrappedJSObject;

MyEndpoint = {};
MyEndpoint.prototype = {
	/**
	 * Gets available translator list
	 * @param {String} method "GET" or "POST"
	 * @param {String} data POST data or GET query string
	 * @param {Function} sendResponseCallback function to send HTTP response
	 */
	"init":function(method, data, sendResponseCallback) {
		// do something with data here?
		Zotero.debug("MyEndpoint loaded");
		
		sendResponseCallback(
			200, 		// 200 OK
			"Hello world"	// Response body
		);
	}
}

// Register endpoint
Zotero.Connector.Endpoints["/myendpoint"] = MyEndpoint;
dev/client_coding/connector_http_server.1303297547.txt.gz · Last modified: 2011/04/20 07:05 by ajlyon