Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
sample_plugin [2007/12/13 15:24] – dstillman | dev:sample_plugin [2019/10/01 09:30] (current) – [A Sample Zotero Plugin] bwiernik | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | < | ||
+ | in the process of updating the documentation for | ||
+ | <a href=" | ||
+ | may be outdated in the meantime. Thanks for your understanding.</ | ||
+ | |||
+ | ====== A Sample Zotero Plugin ====== | ||
+ | |||
+ | To help you get started creating Zotero plugins, we’ve created a simple Hello World plugin that demonstrates how to access Zotero from an external extension. See [[dev: | ||
+ | |||
+ | You can obtain the [[https:// | ||
+ | |||
+ | |||
+ | For more information on the basic elements and layout of a Firefox extension, see the [[https:// | ||
+ | |||
+ | The most important code is in [[https:// | ||
+ | |||
+ | Once installed, the plugin will display an alert whenever an item is added, modified or deleted in Zotero. (You' | ||
+ | |||
+ | **chrome/ | ||
+ | |||
+ | //Note: This file is loaded by %%chrome:// | ||
+ | |||
+ | <code javascript> | ||
+ | |||
+ | Zotero.HelloWorldZotero = { | ||
+ | DB: null, | ||
+ | |||
+ | init: function () { | ||
+ | // Connect to (and create, if necessary) helloworld.sqlite in the Zotero directory | ||
+ | this.DB = new Zotero.DBConnection(' | ||
+ | |||
+ | if (!this.DB.tableExists(' | ||
+ | this.DB.query(" | ||
+ | this.DB.query(" | ||
+ | } | ||
+ | |||
+ | // Register the callback in Zotero as an item observer | ||
+ | var notifierID = Zotero.Notifier.registerObserver(this.notifierCallback, | ||
+ | |||
+ | // Unregister callback when the window closes (important to avoid a memory leak) | ||
+ | window.addEventListener(' | ||
+ | Zotero.Notifier.unregisterObserver(notifierID); | ||
+ | }, false); | ||
+ | }, | ||
+ | |||
+ | insertHello: | ||
+ | var data = { | ||
+ | title: " | ||
+ | company: " | ||
+ | creators: | ||
+ | [' | ||
+ | [' | ||
+ | ], | ||
+ | version: ' | ||
+ | company: ' | ||
+ | place: ' | ||
+ | url: ' | ||
+ | }; | ||
+ | Zotero.Items.add(' | ||
+ | }, | ||
+ | |||
+ | // Callback implementing the notify() method to pass to the Notifier | ||
+ | notifierCallback: | ||
+ | notify: function(event, | ||
+ | if (event == ' | ||
+ | // Increment a counter every time an item is changed | ||
+ | Zotero.HelloWorldZotero.DB.query(" | ||
+ | |||
+ | if (event != ' | ||
+ | // Retrieve the added/ | ||
+ | var items = Zotero.Items.get(ids); | ||
+ | } | ||
+ | else { | ||
+ | var items = extraData; | ||
+ | } | ||
+ | |||
+ | // Loop through array of items and grab titles | ||
+ | var titles = []; | ||
+ | for each(var item in items) { | ||
+ | // For deleted items, get title from passed data | ||
+ | if (event == ' | ||
+ | titles.push(item.old.title ? item.old.title : '[No title]' | ||
+ | } | ||
+ | else { | ||
+ | titles.push(item.getField(' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (!titles.length) { | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // Get the localized string for the notification message and | ||
+ | // append the titles of the changed items | ||
+ | var stringName = ' | ||
+ | switch (event) { | ||
+ | case ' | ||
+ | stringName += " | ||
+ | break; | ||
+ | |||
+ | case ' | ||
+ | stringName += " | ||
+ | break; | ||
+ | |||
+ | case ' | ||
+ | stringName += " | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | var str = document.getElementById(' | ||
+ | getFormattedString(stringName, | ||
+ | titles.join(" | ||
+ | } | ||
+ | |||
+ | alert(str); | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // Initialize the plugin | ||
+ | window.addEventListener(' | ||
+ | |||
+ | |||
+ | </ |