summaryrefslogtreecommitdiff
path: root/World History Connected.js
blob: 7cd2ef41de85dcae84f06b700a8b5342c0f21477 (plain)
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": "0507797c-9bc4-4374-92ca-9e3763b6922b",
	"label": "World History Connected",
	"creator": "Frederick Gibbs",
	"target": "worldhistoryconnected\\.press|historycooperative.*/whc/",
	"minVersion": "1.0.0b4.r5",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-02-27 23:05:02"
}

function scrape(doc) {
	
	var newItem = new Zotero.Item("journalArticle");
	newItem.url = doc.location.href;
	
	var titlePath, title;
	var bookTitle;
	var month, year;
	var metaTags = doc.getElementsByTagName("meta");
	
	newItem.publicationTitle = ZU.xpathText(doc, '//meta[@name="Journal"]/@content');
	newItem.volume = ZU.xpathText(doc, '//meta[@name="Volume"]/@content')
	newItem.issue = ZU.xpathText(doc, '//meta[@name="Issue"]/@content')
	// in the case of book reviews, the title field is blank
	//but quotes are not escaped properly, so if an article title begins with quotes, then the title tag looks blank even though it is not.
	//(though semantically it is)
	//they use the meta tag 'FileType' to indicate Aritlce or Book Review. silly, but we can use it.
	
	if (ZU.xpathText(doc, '//meta[@name="File Type"]/@content') == 'Book Review') {
		//for a book review, title of reviewed book is
		titlePath = '/html/body/table[4]/tbody/tr[3]/td[1]/i';	
		newItem.title = "Review of " + doc.evaluate(titlePath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;

	} else {
		//it would be nice to grab the title from the meta tags, but quotations are properly escaped and the tags are therefore malformed.
		titlePath = '/html/body/table[4]/tbody/tr[2]/td[1]/h2';
		title = doc.evaluate(titlePath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
		if( title ) {
			newItem.title = Zotero.Utilities.trimInternal(Zotero.Utilities.superCleanString(title.textContent));
		}
	}

	var authors = ZU.xpath(doc, '//meta[@name="Author"]/@content')
		for(j in authors) {
			authors[j] = authors[j].textContent.replace("Reviewed by ", "");
			newItem.creators.push(Zotero.Utilities.cleanAuthor(authors[j], "author"));
		}
	
	var month = ZU.xpathText(doc, '//meta[@name="PublicationMonth"]/@content')
	var year = ZU.xpathText(doc, '//meta[@name="PublicationYear"]/@content')
	if(month || year) {
		newItem.date = month +" "+ year;
	}
	
	newItem.attachments.push({document:doc, title:"World History Connected Snapshot"});
	
	newItem.complete();
}

function detectWeb(doc, url) {
	if(doc.title.indexOf("Contents") != -1 ) {
		return 'multiple';
	} else if( doc.title.indexOf("Search results") != -1 &&
		Zotero.Utilities.xpath(doc, '/html/body/dl/dt/strong/a[starts-with(text(),"World History Connected | Vol.")]').length ) {
		return 'multiple';
	} else if( url.match(/\/\d+\.\d+\/[^\/]+/) ) {
		return 'journalArticle';
	}
}

function doWeb(doc, url) {
	
	var searchLinks;
	
	if(doc.title.indexOf("Contents") != -1 || doc.title.indexOf("Search results") != -1) {

		if(doc.title.indexOf("Contents |") != -1) {
		searchLinks = doc.evaluate('//tbody/tr[2]/td[1]/table//a', doc, null, XPathResult.ANY_TYPE, null);	
		} 
		else if ( doc.title.indexOf("| Search results") != -1) {
		searchLinks = doc.evaluate('/html/body/dl/dt/strong/a[starts-with(text(),"World History Connected | Vol.")]', doc, null, XPathResult.ANY_TYPE, null);
		}
		
		var link;
		var title;
		var items = new Object();
		var uris = new Array();
		
		while (elmt = searchLinks.iterateNext()) {
			//Zotero.debug(elmt.href);
			title = Zotero.Utilities.superCleanString(elmt.textContent);
			link = elmt.href;
			if (title && link){
				items[link] = title;
			}
		}
		Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}
			for (var i in items) {
				uris.push(i);
			}
			Zotero.Utilities.processDocuments(uris, scrape);
		});
	} else {
		scrape(doc);
	}
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://worldhistoryconnected.press.illinois.edu/9.1/chaiklin.html",
		"items": [
			{
				"itemType": "journalArticle",
				"creators": [
					{
						"firstName": "Martha",
						"lastName": "Chaiklin",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"document": {
							"location": {}
						},
						"title": "World History Connected Snapshot"
					}
				],
				"url": "http://worldhistoryconnected.press.illinois.edu/9.1/chaiklin.html",
				"publicationTitle": "World History Connected",
				"volume": "9",
				"issue": "1",
				"title": "The Merchant's Ark: Live Animal Gifts in Early Modern Dutch-Japanese Relations",
				"date": "February 2012",
				"libraryCatalog": "World History Connected",
				"accessDate": "CURRENT_TIMESTAMP",
				"shortTitle": "The Merchant's Ark"
			}
		]
	},
	{
		"type": "web",
		"url": "http://worldhistoryconnected.press.illinois.edu/9.1/",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://worldhistoryconnected.press.illinois.edu/cgi-bin/htsearch?method=and&format=builtin-long&sort=score&config=whc&restrict=&exclude=&words=world",
		"items": "multiple"
	}
]
/** END TEST CASES **/