summaryrefslogtreecommitdiff
path: root/APS.js
blob: 6e786ba201f40096e3a4dc5aa259c6a29ff2af3b (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
	"translatorID": "2c310a37-a4dd-48d2-82c9-bd29c53c1c76",
	"label": "APS",
	"creator": "Aurimas Vinckevicius",
	"target": "^https?://journals\\.aps\\.org",
	"minVersion": "3.0.12",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-05-08 21:55:24"
}

function getSearchResults(doc) {
	var articles = doc.getElementsByClassName('article');
	var results = [];
	for(var i=0; i<articles.length; i++) {
		if(articles[i].getElementsByClassName('reveal-export').length) {
			results.push(articles[i]);
		}
	}
	
	return results;
}

function detectWeb(doc, url) {
	if(getSearchResults(doc).length){
		return "multiple";
	}
	
	var title = doc.getElementById('title');
	if(title && ZU.xpath(title, './/a[@data-reveal-id="export-article"]').length) {
		return "journalArticle";
	}
}

function doWeb(doc, url) {
	if(detectWeb(doc, url) == 'multiple') {
		var results = getSearchResults(doc);
		var items = {};
		for(var i=0; i<results.length; i++) {
			var title = ZU.xpath(results[i], './/h5[@class="title"]/a')[0];
			items[title.href] = cleanMath(title.textContent);
		}
		
		Z.selectItems(items, function(selectedItems) {
			if(!selectedItems) return true;
			
			var urls = [];
			for(var i in selectedItems) {
				urls.push(i);
			}
			ZU.processDocuments(urls, scrape);
		});
	} else {
		scrape(doc, url);
	}
}

function scrape(doc, url) {
	url = url.replace(/[?#].*/, '').replace(/\/abstract\//, '/{REPLACE}/');
	// fetch RIS
	var risUrl = url.replace('{REPLACE}', 'export')
			   + '?type=ris&download=true';
	ZU.doGet(risUrl, function(text) {
		text = text.replace(/^ID\s+-\s+/mg, 'DO  - ');
		var trans = Zotero.loadTranslator('import');
		trans.setTranslator('32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7'); //RIS
		trans.setString(text);
		trans.setHandler('itemDone', function(obj, item) {
			// scrape abstract from page
			item.abstractNote = ZU.trimInternal(cleanMath(
				ZU.xpathText(doc, '//section[contains(@class,"abstract")]/div[@class="content"]/p[1]')
			));
			
			// attach PDF
			if(ZU.xpath(doc, '//section[@id="title"]//a[starts-with(text(), "PDF")]').length) {
				item.attachments.push({
					title: 'Full Text PDF',
					url: url.replace('{REPLACE}', 'pdf'),
					mimeType: 'application/pdf'
				});
			}
			
			item.attachments.push({
				title: "APS Snapshot",
				document: doc
			});
			
			if(Z.getHiddenPref && Z.getHiddenPref('attachSupplementary')) {
				try {
					// Fetch supplemental info as JSON
					ZU.doGet(url.replace('{REPLACE}', 'supplemental'), function(text) {
						var suppInfo = JSON.parse(text);
						var asLink = Z.getHiddenPref('supplementaryAsLink');
						for(var i=0; i<suppInfo.components.length; i++) {
							var supp = suppInfo.components[i];
							if(!supp.path) continue;
							var title = supp.filename || 'Supplementary Data';
							if(asLink) {
								item.attachments.push({
									title: title,
									url: supp.path,
									mimeType: 'text/html',
									snapshot: false
								});
							} else {
								item.attachments.push({
									title: title,
									url: supp.path
									//probably PDF, but not sure it's always the case
								});
							}
						}
					}, function() { item.complete() });
				} catch(e) {
					Z.debug('Could not attach supplemental data. ' + e.message);
					item.complete();
				}
			} else {
				item.complete();
			}
		});
		trans.translate();
	});
}

function cleanMath(str) {
	//math tags appear to have duplicate content and are somehow left in even after textContent
	return str.replace(/<(math|mi)[^<>]*>.*?<\/\1>/g, '');
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://journals.aps.org/prd/abstract/10.1103/PhysRevD.84.077701",
		"items": [
			{
				"itemType": "journalArticle",
				"creators": [
					{
						"lastName": "Raidal",
						"firstName": "Martti",
						"creatorType": "author"
					},
					{
						"lastName": "Strumia",
						"firstName": "Alessandro",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Full Text PDF",
						"mimeType": "application/pdf"
					},
					{
						"title": "APS Snapshot"
					}
				],
				"DOI": "10.1103/PhysRevD.84.077701",
				"url": "http://link.aps.org/doi/10.1103/PhysRevD.84.077701",
				"journalAbbreviation": "Phys. Rev. D",
				"issue": "7",
				"abstractNote": "We reconsider Higgs boson invisible decays into Dark Matter in the light of recent Higgs searches at the LHC. Present hints in the Compact Muon Solenoid and ATLAS data favor a nonstandard Higgs boson with approximately 50% invisible branching ratio, and mass around 143 GeV. This situation can be realized within the simplest thermal scalar singlet Dark Matter model, predicting a Dark Matter mass around 50 GeV and direct detection cross section just below present bound. The present runs of the Xenon100 and LHC experiments can test this possibility.",
				"libraryCatalog": "APS",
				"title": "Hints for a nonstandard Higgs boson from the LHC",
				"publicationTitle": "Physical Review D",
				"volume": "84",
				"pages": "077701",
				"date": "October 21, 2011"
			}
		]
	},
	{
		"type": "web",
		"url": "http://journals.aps.org/prd/issues/84/7",
		"items": "multiple"
	}
]
/** END TEST CASES **/