summaryrefslogtreecommitdiff
path: root/Scopus.js
blob: 06168d48cd1fd91a9ef5ec250c971f0f2790ab53 (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
{
	"translatorID": "a14ac3eb-64a0-4179-970c-92ecc2fec992",
	"label": "Scopus",
	"creator": "Michael Berkowitz, Rintze Zelle and Avram Lyon",
	"target": "^https?://www\\.scopus\\.com[^/]*",
	"minVersion": "2.1",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsib",
	"lastUpdated": "2014-04-14 16:54:39"
}

/*
   Scopus Translator
   Copyright (C) 2008-2014 Center for History and New Media and Sebastian Karcher

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function detectWeb(doc, url) {
	if (url.indexOf("/results/") !== -1 &&
		getBoxes(doc).iterateNext()) {
		return "multiple";
	} else if (url.indexOf("/record/") !== -1) {
		return "journalArticle";
	}
}

function getEID(url) {
	return url.match(/eid=([^&]+)/)[1];
}

function getBoxes(doc) {

	return doc.evaluate('//div[@id="resultsBody"]//span[@class="docTitle"]', doc, null, XPathResult.ANY_TYPE, null);
}

function doWeb(doc, url) {
	var articles = new Array();
	if (detectWeb(doc, url) == "multiple") {
		items = new Object();
		var boxes = getBoxes(doc);
		var box;
		while (box = boxes.iterateNext()) {
			var link = doc.evaluate('.//a', box, null, XPathResult.ANY_TYPE, null).iterateNext();
			items[link.href] = Zotero.Utilities.trimInternal(link.textContent);
		}
		Zotero.selectItems(items, function (items) {
			for (var i in items) {
				articles.push(i);
			}
			Zotero.Utilities.processDocuments(articles, scrape);
		});
	} else {
		scrape(doc, url);
	}
}

function scrape(doc, url) {
	//DOI, ISBN, language, and ISSN are not in the export data - get them from the page
	var doi = ZU.xpathText(doc, '//div[contains(@class, "formatSourceExtended")]/span[strong[contains(text(), "DOI:")]]');
	var ISSN = ZU.xpathText(doc, '//div[contains(@class, "formatSourceExtended")]/span[strong[contains(text(), "ISSN:")]]');
	var ISBN = ZU.xpathText(doc, '//div[contains(@class, "formatSourceExtended")]/span[strong[contains(text(), "ISBN:")]]');
	var language = ZU.xpathText(doc, '//div[contains(@class, "formatSourceExtended")]/span[strong[contains(text(), "Original language:")]]');
	var prefix= url.match(/^https?:\/\//)[0];
	var get = prefix + doc.location.host + 
		'/onclick/export.url?oneClickExport=%7b%22Format%22%3a%22RIS%22%2c%22View%22%3a%22CiteAbsKeyws%22%7d&origin=recordpage&eid=';
		//this is the encoded version of oneClickExport={"Format":"RIS","View":"CiteAbsKeyws"} but since it's always the same, no need to run encodeURL
	var eid = getEID(url)
	var rislink = get + eid;	
	Z.debug(rislink)
	Zotero.Utilities.HTTP.doGet(rislink, function(text) {
		// load translator for RIS
		//Z.debug(text)
		if (text.search(/T2  -/)!=-1 && text.search(/JF  -/)!=-1){
			//SCOPUS RIS mishandles alternate titles and journal titles
			//if both fields are present, T2 is the alternate title and JF the journal title
			text = text.replace(/T2  -/, "N1  -" ).replace(/JF  -/, "T2  -");
			
		}
			var translator = Zotero.loadTranslator("import");
		translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
		translator.setString(text);
		translator.setHandler("itemDone", function(obj, item) {
			var notes = [];
			for (i in item.notes) {
				if (item.notes[i]['note'].match(/Export Date:|Source:/))
					continue;
				notes.push(item.notes[i]);
			}
			item.notes = notes;
			item.url = "";
			item.attachments.push({document: doc, title: "SCOPUS Snapshot", mimeType: "text/html"});
			if (doi) item.DOI = doi.replace(/DOI:/, "").trim();
			if (ISSN) item.ISSN = ZU.cleanISSN(ISSN);
			if (ISBN) item.ISBN = ZU.cleanISBN(ISBN);
			if (language) item.language = language.replace(/Original language:/, "").trim();
			item.complete();
		});
		translator.translate();
	});;
}