summaryrefslogtreecommitdiff
path: root/zotero-import/Wiley InterScience.js
blob: 923f2a3e422698bc0fbdfcbf8e5cfb235aa6b1ec (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
{
	"translatorID":"fe728bc9-595a-4f03-98fc-766f1d8d0936",
	"translatorType":4,
	"label":"Wiley InterScience",
	"creator":"Sean Takats and Michael Berkowitz",
	"target":"https?:\\/\\/(?:www3\\.|www\\.)?interscience\\.wiley\\.com[^\\/]*\\/(?:search\\/|(cgi-bin|journal)\\/[0-9]+\\/abstract|journal)",
	"minVersion":"1.0.0b4.r5",
	"maxVersion":"",
	"priority":100,
	"inRepository":true,
	"lastUpdated":"2009-05-05 15:45: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 xpath = '//input[@name="ID"][@type="checkbox"]';
	if(doc.evaluate(xpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
		return "multiple";
	}
	if (url.match(/journal\/\d+\/(issue|home)$/)) {
		return "multiple";
	}
	var m = url.match(/https?:\/\/[^\/]*\/(cgi-bin|journal)(\/(abstract|summary))?\/[0-9]+\/abstract/);
	if (m){
		return "journalArticle";
	}
}

function doWeb(doc, url){
	var namespace = doc.documentElement.namespaceURI;
	var nsResolver = namespace ? function(prefix) {
		if (prefix == 'x') return namespace; else return null;
	} : null;
	var host = 'http://' + doc.location.host + "/";
	Zotero.debug(host);
	var m = url.match(/https?:\/\/[^\/]*\/(journal|cgi-bin\/summary)\/([0-9]+)\/(abstract)?/);
	var ids = new Array();
	if(detectWeb(doc, url) == "multiple") {  //search
		var id;
		var title;
		var availableItems = new Array();
		var xpath = '//tr[td/input[@name="ID"][@type="checkbox"]]';
		if (doc.evaluate(xpath, doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
			elmts = doc.evaluate(xpath, doc, nsResolver, XPathResult.ANY_TYPE, null);
			var elmt = elmts.iterateNext();
			do {
				title = doc.evaluate('./td/strong', elmt, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
				id = doc.evaluate('./td/input[@name="ID"][@type="checkbox"]', elmt, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().value;
				availableItems[id] = title;
			} while (elmt = elmts.iterateNext())
		} else {
			var xpath = '//div[@id="contentCell"]/div[*/a]';
			var elmts = doc.evaluate(xpath, doc, nsResolver, XPathResult.ANY_TYPE, null);
			var elmt = elmts.iterateNext();
			do {
				title = Zotero.Utilities.trimInternal(doc.evaluate('.//strong', elmt, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent);
				id = doc.evaluate('.//a[1]', elmt, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().href.match(/\/([\d]+)\/abstract/)[1];
				availableItems[id] = title;
			} while (elmt = elmts.iterateNext())
		}
		var items = Zotero.selectItems(availableItems);
		if(!items) {
			return true;
		}
		for(var id in items) {
			ids.push(id);
		}
		
	} else if (m){ //single article
		ids.push(m[2]);
	}
	
	
	var sets = [];
	for each (id in ids) {
		var uri = host + 'tools/citex';
		var poststring = "clienttype=1&subtype=1&mode=1&version=1&id=" + id;
		sets.push({ id: id, uri: uri, poststring: poststring });
	}
	
	var setupCallback = function (set, next) {
		Zotero.Utilities.HTTP.doPost(set.uri, set.poststring, function () {
			next();
		});
	}
	
	var processCallback = function (set, next) {
		var id = set.id;
		var uri = host+"tools/CitEx";
		var poststring = "mode=2&format=3&type=2&file=3&exportCitation.x=16&exportCitation.y=10&exportCitation=submit";
		Zotero.Utilities.HTTP.doPost(uri, poststring, function(text) {
			var m = text.match(/%A\s(.*)/);  //following lines fix Wiley's incorrect %A tag (should be separate tags for each author)
			if (m){
				var newauthors ="";
				var authors = m[1].split(",")
				for each (var author in authors){
					if (author != ""){
						newauthors = newauthors + "%A "+Zotero.Utilities.unescapeHTML(Zotero.Utilities.trimInternal(author))+"\n";
					}
				}
				text = text.replace(/%A\s.*\n/, newauthors);
			}
			var translator = Zotero.loadTranslator("import");
			translator.setTranslator("881f60f2-0802-411a-9228-ce5f47b64c7d"); //EndNote/Refer/BibIX
			translator.setString(text);
			translator.setHandler("itemDone", function(obj, item) {
				var pdfurl = host + "cgi-bin/fulltext?ID=" + id + "&PLACEBO=IE.pdf&mode=pdf";
				item.attachments.push({url:pdfurl, title:"Wiley Interscience PDF", mimeType:"application/pdf"});
				item.DOI = item.url.match(/\.org\/(.*)$/)[1];
				item.complete();
			});
			translator.translate();
			
			next();
		});
	}
	
	var callbacks = [setupCallback, processCallback];
	Zotero.Utilities.processAsync(sets, callbacks, function () { Zotero.done(); });
	Zotero.wait();
}