forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCfr (BnF).js
162 lines (147 loc) · 4.33 KB
/
CCfr (BnF).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
{
"translatorID": "899d10f5-3f35-40e6-8dfb-f8ee2dfb1849",
"label": "CCfr (BnF)",
"creator": "Sylvain Machefert, Aurimas Vinckevicius",
"target": "^https?://ccfr\\.bnf\\.fr/portailccfr/.*\\b(action=search|menu=menu_view_grappage|search\\.jsp)\\b",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "g",
"lastUpdated": "2014-09-18 14:08:05"
}
function detectWeb(doc, url) {
if (getSearchResults(doc))
{
return "multiple";
}
else if (url.indexOf("menu=menu_view_grappage") != -1) {
return CCFRTypeDoc(doc);
}
}
function doWeb(doc, url)
{
var items = getSearchResults(doc);
if (items)
{
Z.selectItems(items, function(selectedItems) {
if(!selectedItems) return true;
var links = new Array();
for(var i in selectedItems) {
links.push(getMarcUrl(i));
}
ZU.processDocuments(links, scrape);
});
} else {
// Looking for ID
var memRecordId = ZU.xpathText(doc, '(//input[@id="memRecordId"])[1]/@value');
var urlMarc = getMarcUrl(memRecordId);
if (urlMarc)
{
ZU.processDocuments(urlMarc, scrape);
}
}
}
function getMarcUrl(memRecordId)
{
if (match = memRecordId.match(/^([^:]*):(.*)$/))
{
var url = '/portailccfr/jsp/ccfr/view/';
if (memRecordId.indexOf('oai') == 0)
{
url = url + "oai/";
}
url = url + encodeURIComponent(match[1]) + '_pro.jsp?recordId=' + encodeURIComponent(memRecordId);
return url;
}
else
{
return false;
}
}
function scrape(newDoc, uri)
{
var translator = Zotero.loadTranslator("import");
translator.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
translator.getTranslatorObject(function (marc) {
var record = new marc.record();
var xpath = '//table/tbody/tr';
var elmts = newDoc.evaluate(xpath, newDoc, null, XPathResult.ANY_TYPE, null);
while (elmt = elmts.iterateNext()) {
var field = newDoc.evaluate('./th[1]/text()[1]', elmt, null, XPathResult.ANY_TYPE, null).iterateNext().nodeValue;
var ind = newDoc.evaluate('./td[1]/text()[1]', elmt, null, XPathResult.ANY_TYPE, null).iterateNext().nodeValue;
ind = ZU.trimInternal(ind);
var value = newDoc.evaluate('./td[2]', elmt, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
value = ZU.trimInternal(value);
value = value.replace(/\$/g, marc.subfieldDelimiter);
if (field == "LABEL")
{
record.leader = value;
}
else
{
record.addField(field, ind, value);
}
}
var newItem = new Zotero.Item();
record.translate(newItem);
newItem.complete();
});
}
function CCFRTypeDoc(doc)
{
if ( (ZU.xpathText(doc, "//div[@class='notice-contenu']")) && (ZU.xpathText(doc, "//div[@id='vueCourante']/table/tbody/tr")) )
{
var xpath = "//div[@id='vueCourante']/table/tbody/tr";
var rows = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
while (row = rows.iterateNext())
{
var label = ZU.trimInternal(doc.evaluate("./th[@class='view-field-label-ccfr']", row, null, XPathResult.ANY_TYPE, null).iterateNext().textContent);
if (label == "Type document")
{
var value = doc.evaluate("./td[@class='view-field-value-ccfr']", row, null, XPathResult.ANY_TYPE, null).iterateNext();
var valueTxt = ZU.trimInternal(value.textContent);
switch (valueTxt) {
case "Livre":
return "book";
break;
case "Document électronique":
return "book";
break;
case "Document sonore":
return "audioRecording";
break;
case "Images Animées":
return "film";
case "Carte":
return "map";
default:
Zotero.debug("Unmanaged doc type : " + valueTxt);
return "book";
}
}
}
}
else
{
return null;
}
}
function getSearchResults(doc) {
var items = {},
rec = doc.getElementsByName('record');
if (!doc.getElementById('sourceResultsPane')) return false;
var xpath = "//form[@name='frmSearchResult']/table";
var rows = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);
var row;
var found = false;
while (row = rows.iterateNext())
{
found = true;
var id = doc.evaluate(".//td[@class='ident-check']/input[@type='checkbox']", row, null, XPathResult.ANY_TYPE, null).iterateNext().value;
var title = ZU.trimInternal(doc.evaluate(".//td[@class='Ident']/span/a[@title='Voir la Notice']", row, null, XPathResult.ANY_TYPE, null).iterateNext().textContent);
items[id] = title;
}
return found ? items : false;
}