zotero/translators/zotero.org.js

198 lines
6 KiB
JavaScript
Raw Normal View History

2009-03-10 05:51:13 +00:00
{
"translatorID":"c82c574d-7fe8-49ca-a360-a05d6e34fec0",
"translatorType":4,
"label":"zotero.org",
"creator":"Dan Stillman",
"target":"^https?://[^/]*zotero\\.org/(groups/)?[^/]+/[0-9]+/(items(/?[0-9]+?)?|items/collection/[0-9]+)(\\?.*)?$",
2009-03-10 05:51:13 +00:00
"minVersion":"1.0",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2009-05-20 21:10:00"
2009-03-10 05:51:13 +00:00
}
function detectWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
// don't display for private groups
if (url.match(/\/groups\/[0-9]+\/items/)) {
return false;
}
var a = doc.evaluate('//li[@id="library-tab"]/a[text()="My Library"]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
2009-03-10 05:51:13 +00:00
// Skip current user's library
if (a && url.indexOf(a.href.match(/(^.+)\/items/)[1]) == 0) {
return false;
}
// Library and collections
if (url.match(/\/items\/?(\?.*)?$/) || url.match(/\/items\/collection\/[0-9]+(\?.*)?$/)) {
if (!doc.getElementById("field-table")) {
return false;
}
return "multiple";
}
2009-03-10 05:51:13 +00:00
// Individual item
else if (url.match(/\/items\/[0-9]+(\?.*)?$/)) {
// TODO: embed in page, because this is absurd
var typeMap = {
"Note": "note",
"Attachment": "attachment",
"Book": "book",
"Book Section": "bookSection",
"Journal Article": "journalArticle",
"Magazine Article": "magazineArticle",
"Newspaper Article": "newspaperArticle",
"Thesis": "thesis",
"Letter": "letter",
"Manuscript": "manuscript",
"Interview": "interview",
"Film": "film",
"Artwork": "artwork",
"Web Page": "webpage",
"Report": "report",
"Bill": "bill",
"Case": "case",
"Hearing": "hearing",
"Patent": "patent",
"Statute": "statute",
"E-mail": "email",
"Map": "map",
"Blog Post": "blogPost",
"Instant Message": "instantMessage",
"Forum Post": "forumPost",
"Audio Recording": "audioRecording",
"Presentation": "presentation",
"Video Recording": "videoRecording",
"TV Broadcast": "tvBroadcast",
"Radio Broadcast": "radioBroadcast",
"Podcast": "podcast",
"Computer Program": "computerProgram",
"Conference Paper": "conferencePaper",
"Document": "document",
"Encyclopedia Article":"encyclopediaArticle",
"Dictionary Entry": "dictionaryEntry"
};
var td = doc.evaluate('//div[@id="content"]/div[@class="major-col"]/table//tr[th[text()="Item Type"]]/td', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext();
return td ? typeMap[td.textContent] : "book";
}
}
function xmlToItem(xmlItem) {
// "with ({});" needed to fix default namespace scope issue
// See https://bugzilla.mozilla.org/show_bug.cgi?id=330572
default xml namespace = 'http://zotero.org/namespaces/transfer'; with ({});
var itemType = xmlItem.@itemType;
// FIXME: translate.js only allow top-level attachments in import mode
if (itemType == 'attachment') {
itemType = 'document';
}
var newItem = new Zotero.Item(itemType);
// Don't auto-set repository
newItem.repository = false;
for each(var field in xmlItem.field) {
var fieldName = field.@name.toString();
newItem[fieldName] = field.toString();
}
// Item creators
for each(var creator in xmlItem.creator) {
var data = {
creatorType: creator.@creatorType
};
if (creator.creator.fieldMode == 1) {
data.fieldMode = 1;
data.lastName = creator.creator.name.toString();
}
else {
data.firstName = creator.creator.firstName.toString();
data.lastName = creator.creator.lastName.toString();
}
newItem.creators.push(data);
}
// Both notes and attachments might have parents and notes
if (itemType == 'note' || itemType == 'attachment') {
newItem.note = xmlItem.note.toString();
}
// Attachment metadata
if (itemType == 'attachment') {
//newItem.attachmentLinkMode = parseInt(xmlItem.@linkMode);
newItem.mimeType = xmlItem.@mimeType.toString();
newItem.charset = xmlItem.@charset.toString();
var path = xmlItem.path.toString();
if (path) {
newItem.path = path;
}
}
newItem.complete();
}
2.0b3 megacommit - Support for group libraries - General support for multiple libraries of different types - Streamlined sync support - Using solely libraryID and key rather than itemID, and removed all itemID-changing code - Combined two requests for increased performance and decreased server load - Added warning on user account change - Provide explicit error message on SSL failure - Removed snapshot and link toolbar buttons and changed browser context menu options and drags to create parent items + snapshots - Closes #786, Add numPages field - Fixes #1063, Duplicate item with tags broken in Sync Preview - Added better purging of deleted tags - Added local user key before first sync - Add clientDateModified to all objects for more flexibility in syncing - Added new triples-based Relation object type, currently used to store links between items copied between local and group libraries - Updated zotero.org translator for groups - Additional trigger-based consistency checks - Fixed broken URL drag in Firefox 3.5 - Disabled zeroconf menu option (no longer functional) Developer-specific changes: - Overhauled data layer - Data object constructors no longer take arguments (return to 1.0-like API) - Existing objects can be retrieved by setting id or library/key properties - id/library/key must be set for new objects before other fields - New methods: - ZoteroPane.getSelectedLibraryID() - ZoteroPane.getSelectedGroup(asID) - ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot) - ZoteroPane.addItemFromURL(url, itemType) - ZoteroPane.canEdit() - Zotero.CollectionTreeView.selectLibrary(libraryID) - New Zotero.URI methods - Changed methods - Many data object methods now take a libraryID - ZoteroPane.addAttachmentFromPage(link, itemID) - Removed saveItem and saveAttachments parameters from Zotero.Translate constructor - translate() now takes a libraryID, null for local library, or false to not save items (previously on constructor) - saveAttachments is now a translate() parameter - Zotero.flattenArguments() better handles passed objects - Zotero.File.getFileHash() (not currently used)
2009-05-14 18:23:40 +00:00
2009-03-10 05:51:13 +00:00
function doWeb(doc, url) {
2.0b3 megacommit - Support for group libraries - General support for multiple libraries of different types - Streamlined sync support - Using solely libraryID and key rather than itemID, and removed all itemID-changing code - Combined two requests for increased performance and decreased server load - Added warning on user account change - Provide explicit error message on SSL failure - Removed snapshot and link toolbar buttons and changed browser context menu options and drags to create parent items + snapshots - Closes #786, Add numPages field - Fixes #1063, Duplicate item with tags broken in Sync Preview - Added better purging of deleted tags - Added local user key before first sync - Add clientDateModified to all objects for more flexibility in syncing - Added new triples-based Relation object type, currently used to store links between items copied between local and group libraries - Updated zotero.org translator for groups - Additional trigger-based consistency checks - Fixed broken URL drag in Firefox 3.5 - Disabled zeroconf menu option (no longer functional) Developer-specific changes: - Overhauled data layer - Data object constructors no longer take arguments (return to 1.0-like API) - Existing objects can be retrieved by setting id or library/key properties - id/library/key must be set for new objects before other fields - New methods: - ZoteroPane.getSelectedLibraryID() - ZoteroPane.getSelectedGroup(asID) - ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot) - ZoteroPane.addItemFromURL(url, itemType) - ZoteroPane.canEdit() - Zotero.CollectionTreeView.selectLibrary(libraryID) - New Zotero.URI methods - Changed methods - Many data object methods now take a libraryID - ZoteroPane.addAttachmentFromPage(link, itemID) - Removed saveItem and saveAttachments parameters from Zotero.Translate constructor - translate() now takes a libraryID, null for local library, or false to not save items (previously on constructor) - saveAttachments is now a translate() parameter - Zotero.flattenArguments() better handles passed objects - Zotero.File.getFileHash() (not currently used)
2009-05-14 18:23:40 +00:00
if (url.indexOf("/groups/") == -1) {
var userID = url.match(/^http:\/\/[^\/]*zotero\.org\/[^\/]+\/([0-9]+)/)[1];
var apiPrefix = "https://api.zotero.org/users/" + userID + "/";
var itemRe = /^http:\/\/[^\/]*zotero\.org\/[^\/]+\/[0-9]+\/items\/([0-9]+)/;
2.0b3 megacommit - Support for group libraries - General support for multiple libraries of different types - Streamlined sync support - Using solely libraryID and key rather than itemID, and removed all itemID-changing code - Combined two requests for increased performance and decreased server load - Added warning on user account change - Provide explicit error message on SSL failure - Removed snapshot and link toolbar buttons and changed browser context menu options and drags to create parent items + snapshots - Closes #786, Add numPages field - Fixes #1063, Duplicate item with tags broken in Sync Preview - Added better purging of deleted tags - Added local user key before first sync - Add clientDateModified to all objects for more flexibility in syncing - Added new triples-based Relation object type, currently used to store links between items copied between local and group libraries - Updated zotero.org translator for groups - Additional trigger-based consistency checks - Fixed broken URL drag in Firefox 3.5 - Disabled zeroconf menu option (no longer functional) Developer-specific changes: - Overhauled data layer - Data object constructors no longer take arguments (return to 1.0-like API) - Existing objects can be retrieved by setting id or library/key properties - id/library/key must be set for new objects before other fields - New methods: - ZoteroPane.getSelectedLibraryID() - ZoteroPane.getSelectedGroup(asID) - ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot) - ZoteroPane.addItemFromURL(url, itemType) - ZoteroPane.canEdit() - Zotero.CollectionTreeView.selectLibrary(libraryID) - New Zotero.URI methods - Changed methods - Many data object methods now take a libraryID - ZoteroPane.addAttachmentFromPage(link, itemID) - Removed saveItem and saveAttachments parameters from Zotero.Translate constructor - translate() now takes a libraryID, null for local library, or false to not save items (previously on constructor) - saveAttachments is now a translate() parameter - Zotero.flattenArguments() better handles passed objects - Zotero.File.getFileHash() (not currently used)
2009-05-14 18:23:40 +00:00
} else {
var groupID = url.match(/^http:\/\/[^\/]*zotero\.org\/groups\/[^\/]+\/([0-9]+)/)[1];
var apiPrefix = "https://api.zotero.org/groups/" + groupID + "/";
var itemRe = /^http:\/\/[^\/]*zotero\.org\/groups\/[^\/]+\/[0-9]+\/items\/([0-9]+)/;
2.0b3 megacommit - Support for group libraries - General support for multiple libraries of different types - Streamlined sync support - Using solely libraryID and key rather than itemID, and removed all itemID-changing code - Combined two requests for increased performance and decreased server load - Added warning on user account change - Provide explicit error message on SSL failure - Removed snapshot and link toolbar buttons and changed browser context menu options and drags to create parent items + snapshots - Closes #786, Add numPages field - Fixes #1063, Duplicate item with tags broken in Sync Preview - Added better purging of deleted tags - Added local user key before first sync - Add clientDateModified to all objects for more flexibility in syncing - Added new triples-based Relation object type, currently used to store links between items copied between local and group libraries - Updated zotero.org translator for groups - Additional trigger-based consistency checks - Fixed broken URL drag in Firefox 3.5 - Disabled zeroconf menu option (no longer functional) Developer-specific changes: - Overhauled data layer - Data object constructors no longer take arguments (return to 1.0-like API) - Existing objects can be retrieved by setting id or library/key properties - id/library/key must be set for new objects before other fields - New methods: - ZoteroPane.getSelectedLibraryID() - ZoteroPane.getSelectedGroup(asID) - ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot) - ZoteroPane.addItemFromURL(url, itemType) - ZoteroPane.canEdit() - Zotero.CollectionTreeView.selectLibrary(libraryID) - New Zotero.URI methods - Changed methods - Many data object methods now take a libraryID - ZoteroPane.addAttachmentFromPage(link, itemID) - Removed saveItem and saveAttachments parameters from Zotero.Translate constructor - translate() now takes a libraryID, null for local library, or false to not save items (previously on constructor) - saveAttachments is now a translate() parameter - Zotero.flattenArguments() better handles passed objects - Zotero.File.getFileHash() (not currently used)
2009-05-14 18:23:40 +00:00
}
2009-03-10 05:51:13 +00:00
var nsAtom = new Namespace('http://www.w3.org/2005/Atom');
var nsZXfer = new Namespace('http://zotero.org/namespaces/transfer');
if (detectWeb(doc, url) == "multiple") {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
2.0b3 megacommit - Support for group libraries - General support for multiple libraries of different types - Streamlined sync support - Using solely libraryID and key rather than itemID, and removed all itemID-changing code - Combined two requests for increased performance and decreased server load - Added warning on user account change - Provide explicit error message on SSL failure - Removed snapshot and link toolbar buttons and changed browser context menu options and drags to create parent items + snapshots - Closes #786, Add numPages field - Fixes #1063, Duplicate item with tags broken in Sync Preview - Added better purging of deleted tags - Added local user key before first sync - Add clientDateModified to all objects for more flexibility in syncing - Added new triples-based Relation object type, currently used to store links between items copied between local and group libraries - Updated zotero.org translator for groups - Additional trigger-based consistency checks - Fixed broken URL drag in Firefox 3.5 - Disabled zeroconf menu option (no longer functional) Developer-specific changes: - Overhauled data layer - Data object constructors no longer take arguments (return to 1.0-like API) - Existing objects can be retrieved by setting id or library/key properties - id/library/key must be set for new objects before other fields - New methods: - ZoteroPane.getSelectedLibraryID() - ZoteroPane.getSelectedGroup(asID) - ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot) - ZoteroPane.addItemFromURL(url, itemType) - ZoteroPane.canEdit() - Zotero.CollectionTreeView.selectLibrary(libraryID) - New Zotero.URI methods - Changed methods - Many data object methods now take a libraryID - ZoteroPane.addAttachmentFromPage(link, itemID) - Removed saveItem and saveAttachments parameters from Zotero.Translate constructor - translate() now takes a libraryID, null for local library, or false to not save items (previously on constructor) - saveAttachments is now a translate() parameter - Zotero.flattenArguments() better handles passed objects - Zotero.File.getFileHash() (not currently used)
2009-05-14 18:23:40 +00:00
var column = doc.evaluate('//table[@id="field-table"]//td[1][@class="title"][not(contains(./a, "Unpublished Note"))]', doc, nsResolver, XPathResult.ANY_TYPE, null);
2009-03-10 05:51:13 +00:00
var elems = [], td;
while (td = column.iterateNext()) {
elems.push(td);
}
var items = Zotero.Utilities.getItemArray(doc, elems);
items = Zotero.selectItems(items);
if (!items) {
return true;
}
var apiURLs = [], itemID, apiURL;
for (var url in items) {
itemID = url.match(itemRe)[1];
apiURL = apiPrefix + "items/" + itemID + "?content=full";
apiURLs.push(apiURL);
}
Zotero.Utilities.HTTP.doGet(apiURLs, function(text) {
// Strip XML declaration and convert to E4X
var entry = new XML(text.replace(/<\?xml.*\?>/, ''));
xmlToItem(entry.nsAtom::content.nsZXfer::item);
}, function () { Zotero.done(); });
}
else {
var itemID = url.match(itemRe)[1];
var apiURL = apiPrefix + "items/" + itemID + "?content=full";
Zotero.Utilities.doGet(apiURL, function (text) {
// Strip XML declaration and convert to E4X
var entry = new XML(text.replace(/<\?xml.*\?>/, ''));
xmlToItem(entry.nsAtom::content.nsZXfer::item);
Zotero.done();
});
}
Zotero.wait();
}