forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Agencia del ISBN.js
77 lines (72 loc) · 2.71 KB
/
Agencia del ISBN.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
{
"translatorID": "b8a86e36-c270-48c9-bdd1-22aaa167ef46",
"label": "Agencia del ISBN",
"creator": "Michael Berkowitz",
"target": "^https?://www\\.mcu\\.es/webISBN",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcs",
"lastUpdated": "2015-06-10 11:33:38"
}
function detectWeb(doc, url) {
if (doc.evaluate('//div[@class="isbnResultado"]/div[@class="isbnResDescripcion"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
return "multiple";
} else if (doc.evaluate('//div[@class="fichaISBN"]/div[@class="cabecera"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
return "book";
}
}
function doWeb(doc, url) {
var books = new Array();
if (detectWeb(doc, url) == "multiple") {
var items = new Object();
var boxes = doc.evaluate('//div[@class="isbnResultado"]/div[@class="isbnResDescripcion"]', doc, null, XPathResult.ANY_TYPE, null);
var box;
while (box = boxes.iterateNext()) {
var book = doc.evaluate('./p/span/strong/a', box, null, XPathResult.ANY_TYPE, null).iterateNext();
items[book.href] = book.textContent;
}
Zotero.selectItems(items, function (items) {
if (!items) {
return true;
}
for (var i in items) {
books.push(i);
}
Zotero.Utilities.processDocuments(books, scrape);
});
} else {
scrape(doc, url);
}
}
function scrape (doc, url){
var data = new Object();
var rows = doc.evaluate('//div[@class="fichaISBN"]/table/tbody/tr', doc, null, XPathResult.ANY_TYPE, null);
var next_row;
while (next_row = rows.iterateNext()) {
var heading = doc.evaluate('./th', next_row, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
var value = doc.evaluate('./td', next_row, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
data[heading.replace(/\W/g, "")] = value;
}
var isbn = Zotero.Utilities.trimInternal(doc.evaluate('//span[@class="cabTitulo"]/strong', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent);
var item = new Zotero.Item("book");
item.ISBN = isbn;
item.title = Zotero.Utilities.trimInternal(data['Ttulo']);
item.title= item.title.replace(/\s+:/, ":");
author = data['Autores'];
if (author) {
var authors = author.match(/\b.*,\s+\w+[^([]/g);
for (var i=0; i<authors.length; i++) {
var aut = Zotero.Utilities.trimInternal(authors[i]);
item.creators.push(Zotero.Utilities.cleanAuthor(Zotero.Utilities.trimInternal(aut), "author", true));
}
}
if (data['Publicacin']) item.publisher = Zotero.Utilities.trimInternal(data['Publicacin']);
if (data['FechaEdicin']) item.date = Zotero.Utilities.trimInternal(data['FechaEdicin']);
item.complete();
}
/** BEGIN TEST CASES **/
var testCases = []
/** END TEST CASES **/