summaryrefslogtreecommitdiff
path: root/digibib.net.js
blob: 84e05d24afcd4bd7c70971ce0afae7ed2703fe38 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{
	"translatorID": "e99bd723-39e6-418c-9524-390dbc583e08",
	"label": "digibib.net",
	"creator": "Heiko Jansen (hbz), Ingolf Kuss (hbz), Bernhard Assmann (hbz)",
	"target": "https?://.*\\.digibib\\.net/(Digibib|jumpto|metasearch|opensearch|template)",
	"minVersion": "2.1.9",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcs",
	"lastUpdated": "2013-02-10 23:07:57"
}

/*
   DigiBib Translator - Custom translator for adding metadata records found
   in the DigiBib portal (http://www.digibib.net/) to Zotero

   Copyright (C) 2012 hbz NRW (http://www.hbz-nrw.de/)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Check if the current page really is a page with results (hits) in it.
 * Also checks for the type of results because the different lists have
 * different semantics .
 *
 **/
function detectWeb(doc, url) {
	var namespace = doc.documentElement.namespaceURI;

	var indicator = doc.evaluate('/html/body//span[@id="zotero"]', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

	var indicator_class = indicator.getAttribute('class');
	if (indicator_class.match(/multi/)) {
		// There's one or more result list each containing one or more hits in this page
		return "multiple";
	}
	if (indicator_class.match(/single/)) {
		// There's one hit in this page
		return "single";
	}

	return "";
}

/*
 * Present a list of importable hits in the current page to the user.
 * Custom code is needed to handle the different result list types.
 * Finally import the items selected by the user.
 *
 **/
function doWeb(doc, url) {
	var namespace = doc.documentElement.namespaceURI;

	var indicator = doc.evaluate('/html/body//span[@id="zotero"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext();
	var indicator_class = indicator.getAttribute('class');

	var availableItems = new Object();

	var conf = new Object;
	var type = '';
	// configure the extraction method
	if (indicator_class.match(/multi/)) {
		// There's one or more result list each containing one or more hits in this page
		if (indicator_class.match(/z_metasearch_multi/)) {
			conf["lists_xpath"] = '//div[contains(@class,"listcontainer")]';
			conf["item_xpath"] = './dl[contains(@class,"list")]/dt';
			conf["item_title_xpath"] = './a';
			conf["item_url_xpath"] = 'following-sibling::dd[contains(@class,"func")]//a[contains(@class,"js_formatExport")]';
			conf["url_trnsfrm"] = function (hitUrl) {
				return hitUrl.replace(/\/direct-export/, '/format-export').replace(/FORMAT=TXT/, 'FORMAT=MODS');
			};
		} else if (indicator_class.match(/z_ezb_multi/)) {
			conf["lists_xpath"] = '//div[contains(@class,"listcontainer")]';
			conf["item_xpath"] = './dl[contains(@class,"list")]/dt';
			conf["item_title_xpath"] = './a[contains(@class,"js_getFull")]';
			conf["item_url_xpath"] = './ul/li/a[contains(@class,"js_formatExport")]';
			conf["url_trnsfrm"] = function (hitUrl) {
				return hitUrl.replace(/\/direct-export/, '/format-export').replace(/FORMAT=TXT/, 'FORMAT=MODS');
			};
		} else if (indicator_class.match(/z_digilink_multi/)) {
			conf["lists_xpath"] = '//div[contains(@class,"listcontainer")]';
			conf["item_xpath"] = './dl[contains(@class,"list")]/dt';
			conf["item_title_xpath"] = './strong';
			conf["item_url_xpath"] = 'following-sibling::dd[2]/ul/li/a[contains(@class,"js_formatExport")]';
			conf["url_trnsfrm"] = function (hitUrl) {
				return hitUrl.replace(/\/direct-export/, '/format-export').replace(/FORMAT=TXT/, 'FORMAT=MODS');
			};
		} else if (indicator_class.match(/z_dbis_multi/)) {
			conf["lists_xpath"] = '//div[contains(@class,"listcontainer")]';
			conf["item_xpath"] = './dl[contains(@class,"list")]/dt';
			conf["item_title_xpath"] = './a[contains(@class,"js_getFull")]';
			conf["item_url_xpath"] = './/a[contains(@class,"js_formatExport")]';
			conf["url_trnsfrm"] = function (hitUrl) {
				return hitUrl.replace(/\/direct-export/, '/format-export').replace(/FORMAT=TXT/, 'FORMAT=MODS');
			};
		} else if (indicator_class.match(/z_cart_multi/)) {
			conf["lists_xpath"] = '//div[contains(@class,"listcontainer")]';
			conf["item_xpath"] = './dl[contains(@class,"list")]/dt';
			conf["item_title_xpath"] = 'child::*[2]';
			conf["item_url_xpath"] = 'following-sibling::dd[contains(@class,"last")][1]//a[contains(@class,"js_formatExport")]';
			conf["url_trnsfrm"] = function (hitUrl) {
				return hitUrl.replace(/\/direct-export/, '/format-export').replace(/FORMAT=TXT/, 'FORMAT=MODS');
			};
		}
		availableItems = _get_items_multi(doc, url, namespace, conf);

		Zotero.selectItems(availableItems, function (selectedItems) {
			if (!selectedItems) return true;

			var fetchThese = new Array();
			for (var i in selectedItems) {
				fetchThese.push(i);
			}

			Zotero.Utilities.doGet(fetchThese, importItem);
			Zotero.wait();
		});
	} else if (indicator_class.match(/z_[a-z]*_single/)) {
		var urlNode = doc.evaluate('/html/body/div/div/div[@id="main"]//a[contains(@class,"js_formatExport")]', doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		if (urlNode) {
			var hitUrl = urlNode.singleNodeValue.href.replace(/\/direct-export/, '/format-export').replace(/FORMAT=TXT/, 'FORMAT=MODS');
			Zotero.Utilities.doGet(hitUrl, importItem);
			Zotero.wait();
		}
	}
}

/*
 * Generic extraction of a list of records (title/url pairs) from the page
 * guided by a set of XPaths addressing the relevant html nodes.
 *
 **/
function _get_items_multi(doc, url, namespace, conf) {
	var myItems = new Object;

	// one or more lists in this page
	var listsSnapshot = doc.evaluate(
	conf["lists_xpath"], doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	var isSingleEntryList = listsSnapshot.snapshotLength == 1 ? true : false;

	// walk over every list
	for (var i = 0; i < listsSnapshot.snapshotLength; i++) {
		var cur_list = listsSnapshot.snapshotItem(i);

		// get the entries in the current list
		var listEntriesSnapshot = doc.evaluate(
		conf["item_xpath"], cur_list, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

		// for every entry extract title and url and store these pairs
		// in an hash object; keys are the urls
		for (var j = 0; j < listEntriesSnapshot.snapshotLength; j++) {
			var cur_entry = listEntriesSnapshot.snapshotItem(j);
			var titleNode = doc.evaluate(
			conf["item_title_xpath"], cur_entry, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

			var title, hitUrl;

			if (titleNode.singleNodeValue == null) {
				continue;
			}
			title = _formatTitle(isSingleEntryList, i, j, titleNode.singleNodeValue.textContent);

			var urlNode = doc.evaluate(
			conf["item_url_xpath"], cur_entry, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
			if (titleNode.singleNodeValue == null) {
				continue;
			}

			hitUrl = conf["url_trnsfrm"](urlNode.singleNodeValue.href);
			myItems[hitUrl] = title;
		}
	}

	return myItems;
}

/*
 * Shorten the title of the record if necessary and prefix it
 * with a number
 *
 **/
function _formatTitle(isSingleEntryList, num_a, num_b, text) {
	var prefix = '';
	num_a++;
	num_b++;
	if (isSingleEntryList) prefix = (num_b < 10 ? "0" + num_b : num_b) + ": ";
	else prefix = (num_a < 10 ? "0" + num_a : num_a) + "." + (num_b < 10 ? "0" + num_b : num_b) + ": ";

	text = (text.length > 80) ? text.substr(0, 77) + "..." : text;

	return prefix + text;
}

/*
 * Import a record to Zotero using the standard MODS translator.
 *
 **/
function importItem(mods, resp, url) {
	// import to zotero by relying on standard MODS translator
	var translator = Zotero.loadTranslator("import");
	translator.setTranslator("0e2235e7-babf-413c-9acf-f27cce5f059c"); // MODS-translator
	translator.setHandler("itemDone", cleanup);
	translator.setString(mods);
	translator.translate();
}

/*
 * Custom cleanup for some data fields after the MODS translator
 * has converted the records
 *
 **/
function cleanup(obj, item) {
	if (item.archiveLocation) {
		item.callNumber = item.archiveLocation;
		item.archiveLocation = "";
	}
	item.complete();
}