forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternet Archive Wayback Machine.js
63 lines (61 loc) · 1.92 KB
/
Internet Archive Wayback Machine.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
{
"translatorID":"513a53f5-b95e-4df6-a03e-3348d9ec9f44",
"translatorType":4,
"label":"Internet Archive Wayback Machine",
"creator":"Sean Takats",
"target":"^http://web.archive.org/web/",
"minVersion":"1.0",
"maxVersion":null,
"priority":100,
"inRepository":true,
"lastUpdated":"2009-01-08 08:19:07"
}
function detectWeb(doc, url){
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == "x") return namespace; else return null;
} : null;
var xpath = '//td[@class="mainBody"]/a';
var links = doc.evaluate(xpath, doc, nsResolver, XPathResult.ANY_TYPE, null);
if (links.iterateNext()){
return "multiple";
}
return "webpage";
}
function doWeb(doc, url){
var uris = new Array();
var dateRe = new RegExp("^http://web.archive.org/web/([0-9]+)");
if (dateRe.test(url)){ //handle single item
uris.push(url);
} else{//handle multiple items
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == "x") return namespace; else return null;
} : null;
var xpath = '//td[@class="mainBody"]/a';
var links = doc.evaluate(xpath, doc, nsResolver, XPathResult.ANY_TYPE, null);
var items=new Array();
var link;
while (link = links.iterateNext()){
items[link.href] = link.textContent;
}
items=Zotero.selectItems(items);
for (var i in items) {
uris.push(i);
}
}
Zotero.Utilities.processDocuments(uris, function(newDoc) {
//create new webpage Item from page
var newItem = new Zotero.Item("webpage");
newItem.title = newDoc.title;
//parse date and add
var m = dateRe.exec(newDoc.location.href);
var date = m[1];
date = date.substr(0, 4) + "-" + date.substr(4,2) + "-" + date.substr(6,2);
newItem.date = date;
//create snapshot
newItem.attachments = [{url:newDoc.location.href, title:newDoc.title, mimeType:"text/html"}];
newItem.complete();
}, function() {Zotero.done();});
Zotero.wait();
}