-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathHeinOnline.js
280 lines (262 loc) · 7.54 KB
/
HeinOnline.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
{
"translatorID": "3dcbb947-f7e3-4bbd-a4e5-717f3701d624",
"label": "HeinOnline",
"creator": "Frank Bennett",
"target": "^https?://(www\\.)?heinonline\\.org/HOL/(LuceneSearch|Page|IFLPMetaData|AuthorProfile)\\?",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2022-08-23 02:00:49"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2015-2016 Frank Bennett
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 *****
*/
/*
***************
** Utilities **
***************
*/
// Get any search results from current page
// Used in detectWeb() and doWeb()
function getSearchResults(doc) {
var results = doc.getElementsByClassName("lucene_search_result_b"),
items = {},
found = false;
for (var i = 0, ilen = results.length; i < ilen; i++) {
var url = getXPathStr("href", results[i], './/a[1]');
var title = getXPathStr("textContent", results[i], './/a[1]');
title = ZU.trimInternal(title);
// title = title.replace(/\s*\[[^\]]*\]$/, '');
if (!title || !url) continue;
items[url] = title;
found = true;
}
return found ? items : false;
}
// Get the string value of the first object matching XPath
function getXPathStr(attr, elem, path) {
var res = ZU.xpath(elem, path);
res = res.length ? res[0][attr] : '';
return res ? res : '';
}
// Extract query values to keys on an object
function extractQueryValues(url) {
var ret = {};
ret.base = url.replace(/[a-zA-Z]+\?.*/, "");
var query = url.replace(/.*?\?/, "");
query = query.split("&");
for (var i = 0, ilen = query.length; i < ilen; i++) {
var pair = query[i].split("=");
ret[pair[0]] = pair[1];
}
return ret;
}
// Not all pages have a downloadable PDF
function translateRIS(ris, pdfURL) {
var trans = Zotero.loadTranslator('import');
trans.setTranslator('32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7');// https://github.com/zotero/translators/blob/master/RIS.js
trans.setString(ris);
trans.setHandler('itemDone', function (obj, item) {
if (pdfURL) {
item.attachments = [{
title: "Full Text PDF",
url: pdfURL,
mimeType: "application/pdf"
}];
}
item.complete();
});
trans.getTranslatorObject(function (transObject) {
transObject.options.fieldMap = {
VO: "volume"
};
transObject.doImport();
});
}
function translateCOinS(COinS) {
var item = new Zotero.Item();
Zotero.Utilities.parseContextObject(COinS, item);
item.complete();
}
// Build URL for RIS, and for PDF if available
function scrapePage(doc, url) {
// We need the id= and the handle= of the current target item.
// From that, we can build URL for RIS.
// Check for an RIS popup link in the page.
var risPopupLink = getXPathStr("href", doc, '//form[@id="pagepicker"]//a[contains(@href, "PrintRequest")][1]');
if (risPopupLink) {
// Get the id from pageSelect.
var pageID = doc.getElementById("pageSelect").value;
// Get other parameters from the page URL.
var docParams = extractQueryValues(url);
// Compose the RIS link.
var risURL = docParams.base
+ "CitationFile?kind=ris&handle=" + docParams.handle
+ "&id=" + pageID
+ "&base=js";
ZU.doGet(risURL, function (ris) {
// the PDF URL gives us a page that will refresh itself to the PDF.
var pdfPageURL = attr(doc, '[data-original-title*="Download PDF"]', 'href');
if (pdfPageURL) {
pdfPageURL = docParams.base + pdfPageURL;
// Z.debug(pdfPageURL)
ZU.processDocuments(pdfPageURL, function (pdfDoc) {
// Call to pdfPageURL prepares PDF for download via META refresh URL
var pdfURL = null;
var m = pdfDoc.querySelector('meta[http-equiv="Refresh"]');
// Z.debug(pdfPage)
// Z.debug(m)
if (m) {
var refreshURL;
var parts = m.getAttribute('content').split(/;\s*url=/);
if (parts.length === 2) {
refreshURL = parts[1].trim().replace(/^'(.+)'/, '$1');
}
else {
refreshURL = m.getAttribute('url');
}
pdfURL = docParams.base + refreshURL;
}
translateRIS(ris, pdfURL);
});
}
else {
translateRIS(ris);
}
}, null);
}
else {
// No RIS available in page, try COinS
var COinS = getXPathStr("title", doc, '//span[contains(@class, "Z3988")]');
if (COinS) {
translateCOinS(COinS);
}
}
}
/*
*********
** API **
*********
*/
function detectWeb(doc, url) {
var COinS = getXPathStr("title", doc, '//span[contains(@class, "Z3988")]');
var RIS = getXPathStr("href", doc, '//form[@id="pagepicker"]//a[contains(@href, "PrintRequest")][1]');
if (url.includes("/LuceneSearch?") || url.includes("/AuthorProfile?")) {
if (getSearchResults(doc)) {
return "multiple";
}
}
else if (COinS || RIS) {
return "journalArticle";
}
return false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) === "multiple") {
Zotero.selectItems(getSearchResults(doc), function (items) {
if (!items) {
return;
}
var urls = [];
for (var i in items) {
urls.push(i);
}
ZU.processDocuments(urls, scrapePage);
});
}
else {
scrapePage(doc, url);
}
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://heinonline.org/HOL/IFLPMetaData?type=article&id=53254&collection=iflp&men_tab=srchresults&set_as_cursor=8",
"items": [
{
"itemType": "journalArticle",
"title": "Initiative test.",
"creators": [
{
"firstName": "D.",
"lastName": "Andrews",
"creatorType": "author"
}
],
"date": "2007",
"libraryCatalog": "HeinOnline",
"pages": "38",
"publicationTitle": "International Financial Law Review",
"url": "https://heinonline.org/HOL/IFLPMetaData?type=article&id=53254&collection=iflp",
"volume": "26",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://heinonline.org/HOL/LuceneSearch?terms=test&collection=all&searchtype=advanced&typea=text&tabfrom=&submit=Go&all=true",
"items": "multiple"
},
{
"type": "web",
"url": "https://heinonline.org/HOL/Page?handle=hein.journals/alterlj18&div=22&start_page=76&collection=journals&set_as_cursor=4&men_tab=srchresults",
"items": [
{
"itemType": "journalArticle",
"title": "Means Test or Mean Test Pension Entitlements for Farmers",
"creators": [
{
"lastName": "Voyce",
"firstName": "Malcolm",
"creatorType": "author"
}
],
"date": "1993",
"issue": "2",
"journalAbbreviation": "Alternative L.J.",
"language": "eng",
"libraryCatalog": "HeinOnline",
"pages": "76-85",
"publicationTitle": "Alternative Law Journal",
"url": "https://heinonline.org/HOL/P?h=hein.journals/alterlj18&i=80",
"volume": "18",
"attachments": [
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://heinonline.org/HOL/AuthorProfile?action=edit&search_name=de%20Silva%20de%20Alwis%2C%20Rangita&collection=journals",
"items": "multiple"
}
]
/** END TEST CASES **/