This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafari Books Online.js
391 lines (376 loc) · 16.8 KB
/
Safari Books Online.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
{
"translatorID": "ec491fc2-10b1-11e3-99d7-1bd4dc830245",
"label": "Safari Books Online",
"creator": "Jeffrey Jones",
"target": "^https?://([^/]+\\.)?safaribooksonline\\.[a-zA-Z]+/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 150,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsib",
"lastUpdated": "2014-12-08 05:03:12"
}
/*
Safari Books Translator
Copyright (C) 2014 ProQuest LLC
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/>.
*/
function detectWeb(doc, url) {
var nav = doc.getElementById("lefttoc");
if (nav && ZU.xpathText(nav,".//a[@class='current']")) {
return "bookSection";
}
else if (url.indexOf("/book/") > -1 || /\/[0-9]{10}/.test(url)) {
return "book";
}
else if (getSearchResults(doc,true)) {
return "multiple";
}
}
function doWeb(doc, url) {
var type = detectWeb(doc, url)
if (type == "book" || type == "bookSection") {
importBook(doc, url);
}
else if (type == "multiple"){
var results = getSearchResults(doc);
Zotero.selectItems(results, function (ids) {
if (!ids) return true;
var toProcess = [];
for(var id in ids){
toProcess.push(id);
}
ZU.processDocuments(toProcess, importBook);
});
}
}
function getSearchResults(doc,quick) {
var titles = doc.getElementsByClassName('bookTitle'),
items = {},
found = false;
if(quick) return titles.length;
for(var i = 0; i < titles.length; i++) {
var title = ZU.xpathText(titles[i],"./a/@title");
var link = ZU.xpath(titles[i],"./a")[0].href;
items[link] = title;
found = true;
}
return found ? items : false;
}
function importBook(doc, url,section) {
//if we're not on the book page itself but some sub-page (likely a reader page)
//then we try and process the book root page, it has more data
//parts 1 and 2 are the full url up to and including the book id
var parts = url.match(/(.+\/[0-9]{10,13})\/?(.*)/);
if (parts && parts[2]) {
//a sub-page would likely be a book section, so let's check and save it as such
var nav = doc.getElementById("lefttoc");
var sectionTitle = nav ? ZU.xpathText(nav,".//a[@class='current']") : false;
var prefixes = /^(?:(?:Part|Chapter|Pt|Ch)\.? )?(?:[0-9]{1,3}\.?[0-9]{0,3}|(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3}))\b[.: ]+/;
if(sectionTitle) sectionTitle = sectionTitle.replace(prefixes,"");
ZU.processDocuments([parts[1]], function(newDoc,newUrl){
var section = sectionTitle ?
{"sectionTitle":sectionTitle,"originalUrl":url}
: false;
importBook(newDoc,newUrl,section);
});
return;
}
var mapping = {
name: "title",
inLanguage: "language",
publisher: "publisher",
datePublished: "date",
bookEdition: "edition",
isbn: "ISBN",
numberOfPages: "numPages",
description: "abstractNote"
};
var item = new Z.Item(section ? "bookSection" : "book"),
props = ZU.xpath(doc, '//*[@itemprop]'),
isbn = {};
for(var i = 0; i < props.length; i++) {
var name = mapping[props[i].attributes["itemprop"].value];
if (name) item[name] = ZU.trimInternal(props[i].textContent);
if(name == "edition"
&& (item.edition.toLowerCase() == "first" || item.edition == "1")) {
delete item.edition;
}
if(name == "ISBN") {
var label = props[i].previousSibling
if(label) isbn[label.textContent] = item[name];
}
}
//many isbn, prefer web over print, and 13 over 10
item.ISBN = isbn["Web ISBN-13"]
|| isbn["Web ISBN-10"]
|| isbn["Print ISBN-13"]
|| isbn["Print ISBN-10"]
|| item.ISBN; // in case there's no ISBN with props[i].previousSibling above
//author is poorly defined, have to search for it by text
var dataItems = ZU.xpath(doc, '//ul[@class="metadatalist"]//p[contains(@class,"data")]');
for(var i = 0; i < dataItems.length; i++) {
var field = dataItems[i].textContent,
label = ZU.trimInternal(field.substr(0, field.indexOf(":"))).toLowerCase(),
value = ZU.trimInternal(field.substr(field.indexOf(":") + 1));
if (label == "by") {
var authors = value.split(";")
for(var j = 0; j < authors.length; j++) {
if(authors[j]) item.creators.push(ZU.cleanAuthor(authors[j], 'author'));
}
break;
}
}
var itemUrl = section ? section.originalUrl : url;
item.url = itemUrl.replace(/[?#].*/, '');
if(section) {
item.bookTitle = item.title;
item.title = section.sectionTitle;
}
item.complete();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://proquestcombo.safaribooksonline.com/search?q=the%20good%20parts#X2ludGVybmFsX3NlYXJjaHJlc3VsdHM/c2VhcmNobmV3d29yZD0mc2VhcmNocmVzdWx0c2xpc3Rib3g9JnNyY2ZpbHRlcnM9NyZzb3J0PXJhbmsmb3JkZXI9ZGVzYyZzcmN1c2VycXVlcnk9KHRoZStnb29kK3BhcnRzKQ==",
"items": "multiple"
},
{
"type": "web",
"url": "http://proquestcombo.safaribooksonline.com/category/desktop-and-web-applications/entertainment-and-gaming",
"items": "multiple"
},
{
"type": "web",
"url": "http://proquest.techbus.safaribooksonline.de/book/programming/java/9780133922745/chapter-1dot-introduction/ch01lev1sec2_html?query=((the+java+virtual+machine))#snippet",
"items": [
{
"itemType": "bookSection",
"title": "The Java Virtual Machine",
"creators": [
{
"firstName": "Tim",
"lastName": "Lindholm",
"creatorType": "author"
},
{
"firstName": "Frank",
"lastName": "Yellin",
"creatorType": "author"
},
{
"firstName": "Gilad",
"lastName": "Bracha",
"creatorType": "author"
},
{
"firstName": "Alex",
"lastName": "Buckley",
"creatorType": "author"
}
],
"date": "May 07, 2014",
"ISBN": "978-0-13-392274-5",
"abstractNote": "Written by the inventors of the technology, The Java® Virtual Machine Specification, Java SE 8 Edition is the definitive technical reference for the Java Virtual Machine. The book provides complete, accurate, and detailed coverage of the Java Virtual Machine. It fully describes the new features added in Java SE 8, including the invocation of default methods and the class file extensions for type annotations and method parameters. The book also clarifies the interpretation of class file attributes and the rules of bytecode verification.",
"bookTitle": "The Java® Virtual Machine Specification, Java SE 8 Edition",
"language": "en",
"libraryCatalog": "Safari Books Online",
"publisher": "Addison-Wesley Professional",
"url": "http://proquest.techbus.safaribooksonline.de/book/programming/java/9780133922745/chapter-1dot-introduction/ch01lev1sec2_html",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://proquest.techbus.safaribooksonline.de/book/programming/java/9780133922745",
"items": [
{
"itemType": "book",
"title": "The Java® Virtual Machine Specification, Java SE 8 Edition",
"creators": [
{
"firstName": "Tim",
"lastName": "Lindholm",
"creatorType": "author"
},
{
"firstName": "Frank",
"lastName": "Yellin",
"creatorType": "author"
},
{
"firstName": "Gilad",
"lastName": "Bracha",
"creatorType": "author"
},
{
"firstName": "Alex",
"lastName": "Buckley",
"creatorType": "author"
}
],
"date": "May 07, 2014",
"ISBN": "978-0-13-392274-5",
"abstractNote": "Written by the inventors of the technology, The Java® Virtual Machine Specification, Java SE 8 Edition is the definitive technical reference for the Java Virtual Machine. The book provides complete, accurate, and detailed coverage of the Java Virtual Machine. It fully describes the new features added in Java SE 8, including the invocation of default methods and the class file extensions for type annotations and method parameters. The book also clarifies the interpretation of class file attributes and the rules of bytecode verification.",
"language": "en",
"libraryCatalog": "Safari Books Online",
"numPages": "600",
"publisher": "Addison-Wesley Professional",
"url": "http://proquest.techbus.safaribooksonline.de/book/programming/java/9780133922745",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://proquestcombo.safaribooksonline.com/book/software-engineering-and-development/9780596510046/31dot-emacspeak-the-complete-audio-desktop/emacspeak_the_complete_audio_desktop?query=((code+complete))#snippet",
"items": [
{
"itemType": "bookSection",
"title": "Emacspeak: The Complete Audio Desktop",
"creators": [
{
"firstName": "Andy",
"lastName": "Oram",
"creatorType": "author"
},
{
"firstName": "Greg",
"lastName": "Wilson",
"creatorType": "author"
}
],
"date": "June 26, 2007",
"ISBN": "0-596-51004-7",
"abstractNote": "How do the experts solve difficult problems in software development? In this unique and insightful book, leading computer scientists offer case studies that reveal how they found unusual, carefully designed solutions to high-profile projects. You will be able to look over the shoulder of major coding and design experts to see problems through their eyes. This is not simply another design patterns book, or another software engineering treatise on the right and wrong way to do things. The authors think aloud as they work through their project's architecture, the tradeoffs made in its construction, and when it was important to break rules. This book contains 33 chapters contributed by Brian Kernighan, Karl Fogel, Jon Bentley, Tim Bray, Elliotte Rusty Harold, Michael Feathers, Alberto Savoia, Charles Petzold, Douglas Crockford, Henry S. Warren, Jr., Ashish Gulhati, Lincoln Stein, Jim Kent, Jack Dongarra and Piotr Luszczek, Adam Kolawa, Greg Kroah-Hartman, Diomidis Spinellis, Andrew Kuchling, Travis E. Oliphant, Ronald Mak, Rogerio Atem de Carvalho and Rafael Monnerat, Bryan Cantrill, Jeff Dean and Sanjay Ghemawat, Simon Peyton Jones, Kent Dybvig, William Otte and Douglas C. Schmidt, Andrew Patzer, Andreas Zeller, Yukihiro Matsumoto, Arun Mehta, TV Raman, Laura Wingerd and Christopher Seiwald, and Brian Hayes. Beautiful Code is an opportunity for master coders to tell their story. All author royalties will be donated to Amnesty International.",
"bookTitle": "Beautiful Code",
"language": "en",
"libraryCatalog": "Safari Books Online",
"publisher": "O'Reilly Media, Inc.",
"shortTitle": "Emacspeak",
"url": "http://proquestcombo.safaribooksonline.com/book/software-engineering-and-development/9780596510046/31dot-emacspeak-the-complete-audio-desktop/emacspeak_the_complete_audio_desktop",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://proquestcombo.safaribooksonline.com/book/databases/microsoft-sql-server-2012/9781118282175/part-i-laying-the-foundations/9781118282175p01_xhtml?query=((Part+I.+Laying+the+Foundation))#snippet",
"items": [
{
"itemType": "bookSection",
"title": "Laying the Foundations",
"creators": [
{
"firstName": "Adam",
"lastName": "Jorgensen",
"creatorType": "author"
},
{
"firstName": "Jorge",
"lastName": "Segarra",
"creatorType": "author"
},
{
"firstName": "Patrick",
"lastName": "LeBlanc",
"creatorType": "author"
},
{
"firstName": "Jose",
"lastName": "Chinchilla",
"creatorType": "author"
},
{
"firstName": "Aaron",
"lastName": "Nelson",
"creatorType": "author"
}
],
"date": "August 28, 2012",
"ISBN": "978-1-118-28217-5",
"abstractNote": "Harness the powerful new SQL Server 2012 Microsoft SQL Server 2012 is the most significant update to this product since 2005, and it may change how database administrators and developers perform many aspects of their jobs. If you're a database administrator or developer, Microsoft SQL Server 2012 Bible teaches you everything you need to take full advantage of this major release. This detailed guide not only covers all the new features of SQL Server 2012, it also shows you step by step how to develop top-notch SQL Server databases and new data connections and keep your databases performing at peak. The book is crammed with specific examples, sample code, and a host of tips, workarounds, and best practices. In addition, downloadable code is available from the book's companion web site, which you can use to jumpstart your own projects. Serves as an authoritative guide to Microsoft's SQL Server 2012 for database administrators and developersCovers all the software's new features and capabilities, including SQL Azure for cloud computing, enhancements to client connectivity, and new functionality that ensures high-availability of mission-critical applicationsExplains major new changes to the SQL Server Business Intelligence tools, such as Integration, Reporting, and Analysis ServicesDemonstrates tasks both graphically and in SQL code to enhance your learningProvides source code from the companion web site, which you can use as a basis for your own projectsExplores tips, smart workarounds, and best practices to help you on the job Get thoroughly up to speed on SQL Server 2012 with Microsoft SQL Server 2012 Bible.",
"bookTitle": "Microsoft SQL Server 2012 Bible",
"language": "en",
"libraryCatalog": "Safari Books Online",
"publisher": "John Wiley & Sons",
"url": "http://proquestcombo.safaribooksonline.com/book/databases/microsoft-sql-server-2012/9781118282175/part-i-laying-the-foundations/9781118282175p01_xhtml",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://proquestcombo.safaribooksonline.com/9781118282175/9781118282175c01_xhtml",
"items": [
{
"itemType": "bookSection",
"title": "The World of SQL Server",
"creators": [
{
"firstName": "Adam",
"lastName": "Jorgensen",
"creatorType": "author"
},
{
"firstName": "Jorge",
"lastName": "Segarra",
"creatorType": "author"
},
{
"firstName": "Patrick",
"lastName": "LeBlanc",
"creatorType": "author"
},
{
"firstName": "Jose",
"lastName": "Chinchilla",
"creatorType": "author"
},
{
"firstName": "Aaron",
"lastName": "Nelson",
"creatorType": "author"
}
],
"date": "August 28, 2012",
"ISBN": "978-1-118-28217-5",
"abstractNote": "Harness the powerful new SQL Server 2012 Microsoft SQL Server 2012 is the most significant update to this product since 2005, and it may change how database administrators and developers perform many aspects of their jobs. If you're a database administrator or developer, Microsoft SQL Server 2012 Bible teaches you everything you need to take full advantage of this major release. This detailed guide not only covers all the new features of SQL Server 2012, it also shows you step by step how to develop top-notch SQL Server databases and new data connections and keep your databases performing at peak. The book is crammed with specific examples, sample code, and a host of tips, workarounds, and best practices. In addition, downloadable code is available from the book's companion web site, which you can use to jumpstart your own projects. Serves as an authoritative guide to Microsoft's SQL Server 2012 for database administrators and developersCovers all the software's new features and capabilities, including SQL Azure for cloud computing, enhancements to client connectivity, and new functionality that ensures high-availability of mission-critical applicationsExplains major new changes to the SQL Server Business Intelligence tools, such as Integration, Reporting, and Analysis ServicesDemonstrates tasks both graphically and in SQL code to enhance your learningProvides source code from the companion web site, which you can use as a basis for your own projectsExplores tips, smart workarounds, and best practices to help you on the job Get thoroughly up to speed on SQL Server 2012 with Microsoft SQL Server 2012 Bible.",
"bookTitle": "Microsoft SQL Server 2012 Bible",
"language": "en",
"libraryCatalog": "Safari Books Online",
"publisher": "John Wiley & Sons",
"url": "http://proquestcombo.safaribooksonline.com/9781118282175/9781118282175c01_xhtml",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/