Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Relaxed the constraints of Google Scholar queries; added URL encoding…

… of titles and author names; added a 1 minute delay after every 50 items; bumped up the version to 1.5
  • Loading branch information...
commit 2c42f207bba3a9d38c51108f292e284f3d291cb9 1 parent 5876220
@beloglazov authored
View
6 README.md
@@ -1,6 +1,8 @@
# Zotero Scholar Citations
-This add-on is a plugin for Zotero, a research sources management tool, which is a Firefox extension. The add-on automatically fetches numbers of citations of sources from Google Scholar and makes it possible to sort your sources by the citations. Moreover, it allows batch updating of the citations, as they may change over time.
+This add-on is a plugin for Zotero, a research source management tool, which is a Firefox extension. The add-on automatically fetches numbers of citations of your Zotero items from Google Scholar and makes it possible to sort your items by the citations. Moreover, it allows batch updating the citations, as they may change over time.
+
+When updating multiple citations in a batch, the plugins makes a 1 minute delay after every 50 items to try to avoid being blocked by Google Scholar for multiple automated requests.
Currently, Zotero doesn't have any special field for the number of citations, that's why it is stored in the "Call Number" field. To sort by this field you have to add it in the source listing table.
@@ -8,6 +10,6 @@ Read about how it was made: http://blog.beloglazov.info/2009/10/zotero-citations
# License
-Copyright (C) 2011 Anton Beloglazov
+Copyright (C) 2011-2012 Anton Beloglazov
Distributed under the Mozilla Public License (MPL).
View
2  build
@@ -1,2 +1,2 @@
#!/bin/sh
-zip -r builds/zotero-scholar-citations-1.4-fx.xpi chrome/* chrome.manifest install.rdf
+zip -r builds/zotero-scholar-citations-1.5-fx.xpi chrome/* chrome.manifest install.rdf
View
BIN  builds/zotero-scholar-citations-1.5-fx.xpi
Binary file not shown
View
316 chrome/content/scripts/zoteroscholarcitations.js
@@ -6,180 +6,198 @@ 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);
-}
+ // 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));
- }
- }
-}
+ 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;
+ 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 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();
+ 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.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);
- }
- }
+ 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(items);
+};
Zotero.ScholarCitations.updateItems = function(items) {
- if (items.length == 0) {
- return;
- }
-
+ 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.failed = false;
- Zotero.ScholarCitations.updateNextItem();
-}
+
+ 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.current >= Zotero.ScholarCitations.toUpdate) {
- //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;
+ 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.");
}
-
- Zotero.ScholarCitations.updateItem(Zotero.ScholarCitations.itemsToUpdate[Zotero.ScholarCitations.current]);
-
- Zotero.ScholarCitations.current++;
-}
+ 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?as_q=&num=1&as_q=" + item.getField('title').replace(/ /g, '+');
-
- var date = item.getField('date');
- if (date != '') {
- url += '&as_ylo=' + date + '&as_yhi=' + date;
+ var url = "http://scholar.google.com/scholar?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 += '+';
}
-
- var authors = '&as_sauthors=';
- var creators = item.getCreators();
- for (var i=0; i<creators.length; i++) {
- if (i != 0) {
- authors += '+';
- }
- authors += creators[i].ref.lastName.replace(/ /g, '+');
+ 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();
}
- 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);
- //alert(item.getField('title') + " - " + citations);
- try {
- item.setField('callNumber', citations);
- item.save();
- } catch (e) {
- //alert(e);
- }
- }
- } else {
- Zotero.ScholarCitations.failed = true;
- }
-
- Zotero.ScholarCitations.updateNextItem();
- }
- };
-
- req.send(null);
-}
+ };
+
+ 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;
-}
+ 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));
-}
+ 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);
+ Zotero.ScholarCitations.init();
+ }, false);
View
2  install.rdf
@@ -9,7 +9,7 @@
<RDF:Description RDF:about="urn:mozilla:install-manifest"
em:id="zoteroscholarcitations@beloglazov.info"
em:name="Zotero Scholar Citations"
- em:version="1.4"
+ em:version="1.5"
em:type="2"
em:creator="Anton Beloglazov"
em:description="Zotero plugin for auto-fetching numbers of citations from Google Scholar"
Please sign in to comment.
Something went wrong with that request. Please try again.