forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clinical Key.js
366 lines (330 loc) · 10.5 KB
/
Clinical Key.js
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
{
"translatorID": "a55463ba-e403-415b-80d4-284d5f9b4b15",
"label": "Clinical Key",
"creator": "Jaret M. Karnuta, Mike Davidson",
"target": "^https?://(www\\.|www-)clinicalkey(\\.|-)com",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2017-01-30 08:08:52"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2017 Jaret M. Karnuta & Mike Davidson
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
/*
This translator is designed specifically for use on book section portions and journal articles of
ClinicalKey. It will not work on book overview pages or journal table of contents pages.
NB: url and doc.location.href are different. I think it is because of the way clinicalkey redirects.
Replicate by going to a content page (/content/book/...) and then going to a broswing page (/browse/book/...).
URL remains the page of the previous page (content page) and doc.location.href is the current page (as it should be).
Hence, I never use url and change its content to doc.location.href in detectWeb, the only function that uses the url
To get journal article metadata, Publisher Item Identifier (PIIs) are extracted from the Scopus EID
and queried using the CrossRef REST API.
Documentation at:
https://github.com/CrossRef/rest-api-doc
CrossReff REST API Return Values at:
https://github.com/CrossRef/rest-api-doc/blob/master/api_format.md
*/
function detectWeb(doc, url) {
//see NB above for explanation
url = doc.location.href;
//contentType depends on url, which is present, but rest of site is loaded via ajax (I think)
//monitor dom and reset if changes
var jsession = doc.getElementById('jsessionid');
if (jsession){
Zotero.monitorDOMChanges(jsession, {attributes:true});
if (!jsession.value){
return;
}
}
var contentType;
//contains /content/book/ and does not contain login?
if (url.indexOf("/content/book/") != -1 && url.indexOf("login?") == -1){
contentType = "bookSection";
}
//similar structure to above
else if (url.indexOf('/browse/book/') != -1 && url.indexOf("login?") == -1){
contentType = "book";
}
// similar structure to above, for journal articles
else if (url.indexOf('/content/journal/') != -1 && url.indexOf("login?") == -1){
contentType = "journalArticle";
}
//contentType not set
if (!contentType){
return;
}
return contentType;
}
function doWeb(doc, url){
var contentType = detectWeb(doc, url);
//if book section
if (contentType == 'bookSection'){
var newItem = new Zotero.Item(contentType);
newItem = scrapeBookSection(doc, newItem);
//pdf (if present)
var pdfLink = getPDFLink(doc);
if (pdfLink) {
newItem.attachments.push({
url:pdfLink,
title:"Book Section PDF",
mimeType:"application/pdf"
});
}
//populate common attributes
//url, see NB above for explanation as to why url is NOT used
newItem.url = doc.location.href;
newItem.complete();
}
//if book, use ISBN translator
//borrowed from amazon translator
else if (contentType == 'book'){
var isbn = ZU.xpath(doc, "//button/@data-metadata-isbn");
if (!isbn){
return;
}
isbn = ZU.cleanISBN(isbn[0].value);
//use search translator to get metadata from isbn
var search = Zotero.loadTranslator("search");
//set translators and search
search.setHandler("translators", function(obj, translators) {
search.setTranslator(translators);
search.setHandler("itemDone", function(obj, lookupItem) {
newItem=lookupItem;
//update ISBN
newItem.ISBN = ZU.cleanISBN(isbn);
//Override library catalog
newItem.libraryCatalog = "Clinical Key";
//update url, see NB for rationale why url not used
newItem.url = doc.location.href;
});
search.translate();
});
//no need to override error handler
//save item
search.setHandler("done", function() {
newItem.complete();
});
search.setSearch({ ISBN: isbn });
search.getTranslators();
}
else if (contentType == 'journalArticle') {
var eid;
var pii;
try {
eid = url.split('/');
pii = eid.pop().slice(7);
if (!/^S(\d{15}X|\d{16})/.test(pii)){
throw new Error('PII from url failed. Trying Xpath');
}
} catch(e) {
Zotero.debug(e);
eid = ZU.xpathText(doc, "//ul/@data-eid");
pii = eid.slice(7);
if (!/^S(\d{15}X|\d{16})/.test(pii)){
throw new Error('PII from Xpath failed');
}
}
queryCrossRef(pii, doc);
}
}
//Search & Processing based on CrossRef.js translator
function queryCrossRef (pii, doc){
crossRefQuery = 'http://api.crossref.org/works?query=' + pii;
//TODO: implement API version request
//acceptHeader = {'Accept': 'application/vnd.crossref-api-message+json; version=1.0'}
ZU.doGet(crossRefQuery, function(responseText) {
processCrossRefREST(responseText, doc);
});
}
function processCrossRefREST(jsonOutput, doc){
var jsonParsed = JSON.parse(jsonOutput);
if (jsonParsed['message']['total-results'] > 1) {
// Multiple results shouldn't occur as pii is unique
// handle only first returned object just in case
Zotero.debug('Returned multiple results. Continue processing first');
}
else if (jsonParsed['message']['total-results'] == 0) {
// If the search failed to find results
Zotero.debug('Crossref API failed to find query match');
return;
}
if (!/^1/.test(jsonParsed['message-version'])){
// check that the API version is compatible with this translator
// translator currently written according to v1
Zotero.debug('Request returned wrong API version');
}
// shorten JSON to the single reference
var ref = jsonParsed['message']['items'][0];
if (ref['type'] == 'journal-article') {
// prep for CSL JSON translator
ref['type'] = 'article-journal';
} else if (ref['type'] != 'journal-article') {
// log the unexpected
Zotero.debug('Returned unexpected reference type');
}
// use CSL JSON translator
var text = JSON.stringify(ref);
var trans = Zotero.loadTranslator('import');
trans.setTranslator('bc03b4fe-436d-4a1f-ba59-de4d2d7a63f7');
trans.setString(text);
//Attempt to download fulltext PDF
var pdfLink = getPDFLink(doc);
trans.setHandler('itemDone', function(obj, item) {
if (pdfLink)
item.attachments.push({
url:pdfLink,
title:"Full Text PDF",
mimeType:"application/pdf"
});
item.complete();
});
trans.translate();
}
function getPDFLink(doc) {
var pdfLink = doc.getElementsByClassName('x-pdf')[0].href;
if (!pdfLink) {
pdfLink = ZU.xpathText(doc, './/*[@data-action="pdfDownload"]/@href');
} else if (!pdfLink) {
pdfLink = ZU.xpathText(doc, './/*[@action="download"]/@href');
}
return pdfLink;
}
function scrapeBookSection(doc, item){
//book title
var bookTitle = ZU.xpathText(doc, '//*[@data-once-text="XocsCtrl.title"]');
item.bookTitle = bookTitle;
//section title
var title = ZU.xpathText(doc, '//*[@ng-bind-html="ContentCtrl.title"]');
item.title = title;
//authors
var authorsList = ZU.xpath(doc, '//ul[@ng-bind-html="XocsCtrl.authorsHtml"]/li/a');
for (var i = 0;i<authorsList.length;i++){
var author = authorsList[i].innerHTML;
if (author.indexOf("<") != -1){
author = author.split("<")[0];
}
item.creators.push(Zotero.Utilities.cleanAuthor(author, 'author'));
}
//chapter and page metadata
var chapterAndPages = ZU.xpathText(doc,'//p[@class="source ng-binding"]');
//make pattern that should capter pages if present
//matches both xxx-xxx (length of #s not important)
//and xxx-xxx.eY
var pagesPattern = /\s\d+-\d+(\.e\d+)?/;
var pagesMatch = chapterAndPages.match(pagesPattern);
if (pagesMatch){
//get whole regex match
item.pages = pagesMatch[0];
}
//make pattern that will match to the chapter number
var chapterPattern = /chapter\s(\d+)/i
var chapterMatch = chapterAndPages.match(chapterPattern);
if (chapterMatch){
//get match within first group
var chapterNumber = chapterMatch[1];
item.notes.push({note:"Chapter: "+chapterNumber});
}
//ISBN metadata
var isbn = ZU.xpath(doc, "//button/@data-metadata-isbn");
if (isbn){
var isbnNo = isbn[0].value;
item.ISBN = isbn;
}
//edition metadata
var edition = ZU.xpathText(doc, '//*[@data-once-text="XocsCtrl.edition"]').split(/edition/i)[0].trim();
//convert to number for correct zotero citation handling
item.edition = textToNumber(edition);
//publisher metadata
var datePub = ZU.xpathText(doc, '//*[@data-once-text="XocsCtrl.copyright"]');
var datePattern = /\d{4}/g;
var dateMatch = datePub.match(datePattern);
if (dateMatch){
item.date = dateMatch[0];
}
if (datePub.indexOf("imprint") != -1){
var imprintPattern = /by\s(.*),.*imprint\sof\s(.*)\sInc/i;
var imprintMatch = datePub.match(imprintPattern);
if (imprintMatch){
//expected number of matches
if (imprintMatch.length == 3){
item.publisher = imprintMatch[2]+"/"+imprintMatch[1];
}
//added for robustness
else {
var imprintPublisher=imprintMatch[0].replace("by","").trim();
item.publisher=imprintPublisher;
}
}
}
else {
var publisherPattern = /by\s(.*?)(,)?\s/;
var publisherMatch = datePub.match(publisherPattern);
if (publisherMatch){
//get first matched group, between by and , or whitespace
item.publisher=publisherMatch[1];
}
}
if (datePub.match(/elsevier/i)){
item.place = "Philadelphia, PA";
}
return item;
}
//Converts ordinal text to number
//Only converting up to 31
//E.g., text=first -> 1
//E.g., Twenty-Second -> 22
function textToNumber(text){
var textarr = [
"first",
"second",
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eighth",
"ninth",
"tenth",
"eleventh",
"twelfth",
"thirteenth",
"fourteenth",
"fifteenth",
"sixteenth",
"seventeenth",
"eighteenth",
"nineteenth",
"twentieth",
"twenty-first",
"twenty-second",
"twenty-third",
"twenty-fourth",
"twenty-fifth",
"twenty-sixth",
"twenty-seventh",
"twenty-eighth",
"twenty-ninth",
"thirtieth",
"thirty-first"
];
var number = textarr.indexOf(text.toLowerCase());
//shift from 0 to 1 based indexing
return (number != -1)? number + 1 : text;
}