-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanLII.js
112 lines (92 loc) · 2.73 KB
/
CanLII.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
{
"translatorID":"84799379-7bc5-4e55-9817-baf297d129fe",
"translatorType":4,
"label":"CanLII",
"creator":"Bill McKinney",
"target":"http:\\/\\/www\\.canlii\\.org\\/en\\/[^\\/]+\\/[^\\/]+\\/doc\\/.+",
"minVersion":"1.0.0b4.r1",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2007-06-18 18:15:00"
}
function detectWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var canLiiRegexp = /http:\/\/www\.canlii\.org\/en\/[^\/]+\/[^\/]+\/doc\/.+/
if(canLiiRegexp .test(url)) {
return "book";
} else {
var aTags = doc.getElementsByTagName("a");
for(var i=0; i<aTags.length; i++) {
if(articleRegexp.test(aTags[i].href)) {
return "multiple";
}
}
}
}
function associateMeta(newItem, metaTags, field, zoteroField) {
var field = metaTags.namedItem(field);
if(field) {
newItem[zoteroField] = field.getAttribute("content");
}
}
function scrape(doc) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = namespace ? function(prefix) {
if (prefix == 'x') return namespace; else return null;
} : null;
var newItem = new Zotero.Item("case");
var metaTags = doc.getElementsByTagName("meta");
associateMeta(newItem, metaTags, "DC.Title", "title");
associateMeta(newItem, metaTags, "DC.Date", "dateDecided");
associateMeta(newItem, metaTags, "DC.Language", "language");
newItem.url = doc.location.href;
var field = metaTags.namedItem("DC.Title");
var tmpText = "";
if(field) {
tmpText = field.getAttribute("content");
var capRe = /^(.+),\s+(\d{4})\s+(\w+)\s+(\d+)\s+\(([^\)]+)\)/;
var m = capRe.exec(tmpText);
if(m) {
newItem.caseName = m[1]+", "+m[2]+" "+m[3]+" "+m[4];
if (m[3] == 'CanLII') {
newItem.court = m[5];
} else {
newItem.court = m[3];
}
} else {
newItem.caseName = tmpText;
newItem.court = "not found";
}
}
// attach link to pdf version
// NOTE: not working - don't know why
var pdfRe = /^(.+)\.html$/;
var pdfMatch = pdfRe.exec(doc.location.href);
if (pdfMatch) {
var pdfUrl = pdfMatch[1]+".pdf";
newItem.attachments = [{url:pdfUrl, title:"PDF version", mimeType:"application/pdf"}];
}
newItem.complete();
}
function doWeb(doc, url) {
var canLiiRegexp= /http:\/\/www\.canlii\.org\/en\/[^\/]+\/[^\/]+\/doc\/.+/
if(canLiiRegexp.test(url)) {
scrape(doc);
} else {
var items = Zotero.Utilities.getItemArray(doc, doc, canLiiRegexp);
items = Zotero.selectItems(items);
if(!items) {
return true;
}
var urls = new Array();
for(var i in items) {
urls.push(i);
}
Zotero.Utilities.processDocuments(urls, scrape, function() { Zotero.done(); });
Zotero.wait();
}
}