summaryrefslogtreecommitdiff
path: root/Oxford Music and Art Online.js
blob: 78c571be2a33c5dc3cc90016182895809f51c665 (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
{
	"translatorID": "f203db7f-7b7b-4dc4-b018-115b7885fe3b",
	"label": "Oxford Music and Art Online",
	"creator": "Michael Berkowitz",
	"target": "http://[^/]*www.oxford(music|art)online.com[^/]*/",
	"minVersion": "1.0.0b4.r5",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2013-06-23 12:06:33"
}

function detectWeb(doc, url) {
	if (url.match(/search_results/)) {
		return "multiple";
	} else if (url.match(/\/article\//)) {
		return "journalArticle";
	}
}

function doWeb(doc, url) {
	var host = doc.location.host;
	var site = host.match(/oxford(.*)online/)[1];
	var ids = new Array();
	if (detectWeb(doc, url) == "multiple") {
		var items = new Object();
		var links = doc.evaluate('//ul[@class="search_result_list"]/li/p/a', doc, null, XPathResult.ANY_TYPE, null);
		var link;
		while (link = links.iterateNext()) {
			items[link.href] = link.textContent;
		}
		Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}
			for (var i in items) {
				ids.push(i.match(/(article)\/([^?]+)/)[2]);
			}
			scrape(ids, host);
		});
	} else {
		ids = [url.match(/(article)\/([^?]+)/)[2]];
		scrape(ids, host);
	}	
}	

function scrape(ids, host){
	for each (var id in ids) {
		var get = 'http://' + host + '/subscriber/article_export_citation/' + id;
		Zotero.Utilities.HTTP.doGet(get, function(text) {
			var translator = Zotero.loadTranslator("import");
			translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
			translator.setString(text);
			translator.setHandler("itemDone", function(obj, item) {
				var authors = new Array();
				for (var i in item.creators) {
				if (!item.creators[i].firstName){
					names = item.creators[i].lastName.match(/(.*)\s([^\s]+)$/);
					item.creators[i] = {firstName:names[1], lastName:names[2], creatorType:"author"};
				} 
				}
				item.complete();
			});
			translator.translate();
		});
	}
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.oxfordmusiconline.com/subscriber/search_results?q=Bach&hbutton_search.x=0&hbutton_search.y=0&source=omo_epm&source=omo_t237&source=omo_gmo&source=omo_t114&search=quick",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://www.oxfordmusiconline.com/subscriber/article/grove/music/40023?q=Bach&hbutton_search.x=0&hbutton_search.y=0&source=omo_epm&source=omo_t237&source=omo_gmo&source=omo_t114&search=quick&pos=1&_start=1#firsthit",
		"items": [
			{
				"itemType": "journalArticle",
				"creators": [
					{
						"firstName": "Christoph",
						"lastName": "Wolff",
						"creatorType": "author"
					},
					{
						"firstName": "Walter",
						"lastName": "Emery",
						"creatorType": "author"
					},
					{
						"firstName": "Peter",
						"lastName": "Wollny",
						"creatorType": "author"
					},
					{
						"firstName": "Ulrich",
						"lastName": "Leisinger",
						"creatorType": "author"
					},
					{
						"firstName": "Stephen",
						"lastName": "Roe",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "Bach",
				"publicationTitle": "Grove Music Online",
				"libraryCatalog": "Oxford Music and Art Online"
			}
		]
	}
]
/** END TEST CASES **/