/
zoteroscholarcitations.js
203 lines (175 loc) · 5.96 KB
/
zoteroscholarcitations.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Zotero.ScholarCitations = {};
Zotero.ScholarCitations.toUpdate = 0;
Zotero.ScholarCitations.current = 0;
Zotero.ScholarCitations.failed = false;
Zotero.ScholarCitations.itemsToUpdate = null;
Zotero.ScholarCitations.init = function() {
// Register the callback in Zotero as an item observer
var notifierID = Zotero.Notifier.registerObserver(Zotero.ScholarCitations.notifierCallback, ['item']);
// Unregister callback when the window closes (important to avoid a memory leak)
window.addEventListener('unload', function(e) {
Zotero.Notifier.unregisterObserver(notifierID);
}, false);
};
Zotero.ScholarCitations.notifierCallback = {
notify: function(event, type, ids, extraData) {
if (event == 'add') {
Zotero.ScholarCitations.updateItems(Zotero.Items.get(ids));
}
}
};
Zotero.ScholarCitations.updateSelectedEntity = function(libraryId) {
if (!ZoteroPane.canEdit()) {
ZoteroPane.displayCannotEditLibraryMessage();
return;
}
var collection = ZoteroPane.getSelectedCollection();
var group = ZoteroPane.getSelectedGroup();
if (collection) {
var items = [];
var _items = collection.getChildren(true, false, 'item');
for each(var item in _items) {
items.push(Zotero.Items.get(item.id));
}
Zotero.ScholarCitations.updateItems(items);
} else if (group) {
if (!group.editable) {
alert("This group is not editable!");
return;
}
var collections = group.getCollections();
var items = [];
for each(collection in collections) {
var _items = collection.getChildren(true, false, 'item');
for each(var item in _items) {
items.push(Zotero.Items.get(item.id));
}
}
Zotero.ScholarCitations.updateItems(items);
} else {
Zotero.ScholarCitations.updateAll();
}
};
Zotero.ScholarCitations.updateSelectedItems = function() {
Zotero.ScholarCitations.updateItems(ZoteroPane.getSelectedItems());
};
Zotero.ScholarCitations.updateAll = function() {
var items = [];
var _items = Zotero.Items.getAll();
for each(var item in _items) {
if (item.isRegularItem() && !item.isCollection()) {
var libraryId = item.getField("libraryID");
if (libraryId == null || libraryId == '' || Zotero.Libraries.isEditable(libraryId)) {
items.push(item);
}
}
}
Zotero.ScholarCitations.updateItems(items);
};
Zotero.ScholarCitations.updateItems = function(items) {
if (items.length == 0) {
return;
}
// Disabled progress window
//Zotero.UnresponsiveScriptIndicator.disable();
//Zotero_File_Interface.Progress.show("Updating citations...");
Zotero.ScholarCitations.current = 0;
Zotero.ScholarCitations.toUpdate = items.length;
Zotero.ScholarCitations.itemsToUpdate = items;
Zotero.ScholarCitations.numberOfUpdatedItems = 0;
Zotero.ScholarCitations.delay = false;
Zotero.ScholarCitations.failed = false;
Zotero.ScholarCitations.updateNextItem();
};
Zotero.ScholarCitations.updateNextItem = function() {
if (!Zotero.ScholarCitations.delay &&
Zotero.ScholarCitations.numberOfUpdatedItems > 0 &&
Zotero.ScholarCitations.numberOfUpdatedItems % 50 == 0) {
// 60 seconds delay after every 50 items
Zotero.ScholarCitations.delay = true;
setTimeout(Zotero.ScholarCitations.updateNextItem, 60000);
return;
}
Zotero.ScholarCitations.delay = false;
Zotero.ScholarCitations.numberOfUpdatedItems++;
if (Zotero.ScholarCitations.current >= Zotero.ScholarCitations.toUpdate) {
// Disabled progress window
//Zotero_File_Interface.Progress.close();
//Zotero.UnresponsiveScriptIndicator.enable();
if (Zotero.ScholarCitations.failed) {
alert("Some of the requests to Google Scholar failed. Probably due to large number of requests.");
}
return;
}
Zotero.ScholarCitations.updateItem(Zotero.ScholarCitations.itemsToUpdate[Zotero.ScholarCitations.current]);
Zotero.ScholarCitations.current++;
};
Zotero.ScholarCitations.updateItem = function(item) {
var url = "http://scholar.google.com/scholar?hl=en&as_q=" + encodeURIComponent(item.getField('title')).replace(/ /g, '+') + "&num=1";
var date = item.getField('date');
if (date != '') {
//url += '&as_ylo=' + date + '&as_yhi=' + date;
url += '&as_ylo=' + date;
}
var creators = item.getCreators();
if (creators.length > 0) {
url += '&as_sauthors=' + encodeURIComponent(creators[0].ref.lastName).replace(/ /g, '+');
}
/*
var authors = '&as_sauthors=';
for (var i=0; i<creators.length; i++) {
if (i != 0) {
authors += '+';
}
authors += creators[i].ref.lastName.replace(/ /g, '+');
}
url += authors;
*/
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function(aEvt) {
if (req.readyState == 4) {
if (req.status == 200) {
if (item.isRegularItem() && !item.isCollection()) {
var citations = Zotero.ScholarCitations.getCitationCount(req.responseText);
try {
item.setField('callNumber', citations);
item.save();
} catch (e) {
}
}
} else {
Zotero.ScholarCitations.failed = true;
}
Zotero.ScholarCitations.updateNextItem();
}
};
req.send(null);
};
Zotero.ScholarCitations.fillZeros = function(number) {
var output = '';
var cnt = 4 - number.length;
for (var i = 0; i < cnt; i++) {
output += '0';
}
output += number;
return output;
};
Zotero.ScholarCitations.getCitationCount = function(responseText) {
if (responseText == "") {
return '0000';
}
var citeStringLength = 15;
var lengthOfCiteByStr = 9;
var citeArray = new Array();
var citeExists = responseText.search('Cited by');
if (citeExists == -1) {
return '0000';
}
var tmpString = responseText.substr(citeExists, citeStringLength);
var end = tmpString.indexOf("<") - lengthOfCiteByStr;
return Zotero.ScholarCitations.fillZeros(tmpString.substr(lengthOfCiteByStr, end));
};
window.addEventListener('load', function(e) {
Zotero.ScholarCitations.init();
}, false);