summaryrefslogtreecommitdiff
path: root/zotero.org.js
blob: 240a6502ae11bd33fa7ce74ef226b6768d931187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{
	"translatorID": "c82c574d-7fe8-49ca-a360-a05d6e34fec0",
	"label": "zotero.org",
	"creator": "Dan Stillman and Aurimas Vinckevicius",
	"target": "^https?://[^/]*zotero\\.org(:\\d+)?/.+/items(/|$)",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsv",
	"lastUpdated": "2013-12-18 22:57:58"
}

function textToXML(text) {
	try {
		var parser = new DOMParser();
		return parser.parseFromString(text, 'text/xml');
	} catch(e) {
		Zotero.debug(e);
		return null;
	}
}

function scrape(text) {
	var xml;
	if( ( xml = textToXML(text) ) &&
		xml.documentElement.nodeName != 'parsererror' ) {

		var itemJSON = xml.documentElement.getElementsByTagName('content')[0].textContent;

		var item = JSON.parse(itemJSON);

		var newItem = new Zotero.Item();
		for(var property in item) {
			newItem[property] = item[property];
		}
		if(!newItem.title) newItem.title = "[Untitled]";

		newItem.complete();
	}
}

function getListTitles(doc) {
	return ZU.xpath(doc, '//table[@id="field-table"]//td[@class="title"]'
		+ '[./a[not(contains(text(), "Unpublished Note"))]'
			+ '/span[not(contains(@class,"sprite-treeitem-attachment"))]]');
}

var apiKey;

function getLibraryURI(doc) {
	var feed = ZU.xpath(doc, '//a[@type="application/atom+xml" and @rel="alternate"]')[0]
	if(!feed) return;
	var url = feed.href.match(/^.+?\/(?:users|groups)\/\w+/);
	
	if(!url) {
		//personal library. see if we can find an API key
		var key = ZU.xpathText(doc, '//script[contains(text(),"apiKey")]');
		if(!key) return;
		
		key = key.match(/apiKey\s*:\s*(['"])(.+?)\1/);
		if(!key) return;
		apiKey = key[2];
		
		url = decodeURIComponent(feed.href)
			.match(/https?:\/\/[^\/]+\/(?:users|groups)\/\w+/);
		if(!url) return;
	}
	
	return url[0] + '/items/';
}

function detectWeb(doc, url) {
	//disable for libraries where we can't get a library URI or an apiKey
	if(!getLibraryURI(doc)) return;
	
	//single item
	if( url.match(/\/itemKey\/\w+/) ) {
		return ZU.xpathText(doc, '//div[@id="item-details-div"]//td[preceding-sibling::th[text()="Item Type"]]/@class')
				|| false;
	}

	// Library and collections
	if ( ( url.match(/\/items\/?([?#].*)?$/)
		|| url.indexOf('/collectionKey/') != -1
		|| url.match(/\/collection\/\w+/)
		|| url.indexOf('/tag/') != -1 )	
		&& getListTitles(doc).length ) {
		return "multiple";
	}
}

function doWeb(doc, url) {
	var libraryURI = getLibraryURI(doc);
	if(Zotero.isBookmarklet) {
		libraryURI = libraryURI.replace("https://api.zotero.org", "https://www.zotero.org/api");
	}
	var apiOpts = '?format=atom&content=json' + (apiKey ? '&key=' + apiKey : '' );
	var itemRe = /\/itemKey\/(\w+)/;

	if (detectWeb(doc, url) == "multiple") {
		var elems = getListTitles(doc);
		var items = ZU.getItemArray(doc, elems);
		
		Zotero.selectItems(items, function(selectedItems) {
			if( !selectedItems ) return true;

			var apiURIs = [], itemID;
			for (var url in selectedItems) {
				itemID = url.match(itemRe)[1];
				apiURIs.push(libraryURI + itemID + apiOpts);
			}

			Zotero.Utilities.HTTP.doGet(apiURIs, scrape);
		});
	} else {
		var itemID = url.match(itemRe)[1];
		var itemURI = libraryURI + itemID + apiOpts;
		Zotero.Utilities.doGet(itemURI, scrape);
	}
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "https://www.zotero.org/groups/all_things_zotero/items/itemKey/HXTTNJGD",
		"defer": true,
		"items": [
			{
				"itemType": "journalArticle",
				"creators": [
					{
						"creatorType": "author",
						"firstName": "Mark",
						"lastName": "Desirto"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "Expert Searching, Zotero: A New Bread of Search Tool",
				"publicationTitle": "Medical Library Association Newsletter",
				"date": "April 2007",
				"callNumber": "0000",
				"extra": "Cited by 0000"
			}
		]
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/marksample/items/collectionKey/5RN69IBP/itemKey/58VT7DAA",
		"defer": true,
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"creatorType": "author",
						"firstName": "Mark",
						"lastName": "Osteen"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "American Magic and Dread: Don DeLillo’s Dialogue with Culture",
				"place": "Philadelphia",
				"publisher": "University of Pennsylvania Press",
				"date": "2000",
				"ISBN": "0812235517",
				"shortTitle": "American Magic"
			}
		]
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/groups/all_things_zotero/items",
		"items": "multiple",
		"defer": true
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/groups/all_things_zotero/items/collectionKey/XX99JMW8",
		"items": "multiple",
		"defer": true
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/marksample/items",
		"items": "multiple",
		"defer": true
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/marksample/items/collectionKey/5RN69IBP",
		"items": "multiple",
		"defer": true
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/marksample/items/collection/5RN69IBP",
		"items": "multiple",
		"defer": true
	},
	{
		"type": "web",
		"url": "https://www.zotero.org/groups/devtesting/items/tag/tag2",
		"items": "multiple",
		"defer": true
	}
]
/** END TEST CASES **/