Skip to content

Commit

Permalink
Improved handling of invalid DOIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bwiernik committed Nov 29, 2017
1 parent 907ea10 commit a6d3f93
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh

version=1.0.2
version=1.0.3
rm builds/zotero-shortdoi-${version}.xpi
zip -r builds/zotero-shortdoi-${version}.xpi chrome/* chrome.manifest install.rdf options.xul
67 changes: 29 additions & 38 deletions chrome/content/scripts/zoteroshortdoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ Zotero.ShortDOI.init = function() {
Zotero.ShortDOI.resetState();

stringBundle = document.getElementById('zoteroshortdoi-bundle');
Zotero.ShortDOI.DOInotfoundString = 'DOI not found. taking you to http://shortdoi.org to view the error.';
Zotero.ShortDOI.invalidDOIString = 'Invalid DOI';
Zotero.ShortDOI.invalidDOITagString = 'Invalid DOIs were found. These have been tagged with _Invalid DOI.';
if (stringBundle != null) {
Zotero.ShortDOI.DOInotfoundString = stringBundle.getString('DOInotfoundString');
Zotero.ShortDOI.invalidDOIString = stringBundle.getString('invalidDOIString');
Zotero.ShortDOI.invalidDOITagString = stringBundle.getString('invalidDOITagString');
}

// Register the callback in Zotero as an item observer
Expand All @@ -108,6 +110,16 @@ Zotero.ShortDOI.notifierCallback = {
};

Zotero.ShortDOI.resetState = function() {
if (Zotero.ShortDOI.invalidDOI) {
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);

promptService.alert(window,
Zotero.ShortDOI.invalidDOIString,
Zotero.ShortDOI.invalidDOITagString);

Zotero.ShortDOI.invalidDOI = null;
}
Zotero.ShortDOI.current = -1;
Zotero.ShortDOI.toUpdate = 0;
Zotero.ShortDOI.itemsToUpdate = null;
Expand Down Expand Up @@ -176,20 +188,16 @@ Zotero.ShortDOI.updateNextItem = function() {
Zotero.ShortDOI.itemsToUpdate[Zotero.ShortDOI.current]);
};

Zotero.ShortDOI.generateItemUrl = function(item, attempt) {
Zotero.ShortDOI.generateItemUrl = function(item) {
var baseURL = 'http://shortdoi.org/';
var doi = item.getField('DOI');
if(doi && typeof doi === "string") {
if (doi && typeof doi === "string") {
doi = Zotero.Utilities.cleanDOI(doi);
if(doi) {
if(!!doi.match(/10\/[^\s]*[^\s\.,]/)) {
if (doi) {
if (!!doi.match(/10\/[^\s]*[^\s\.,]/)) {
return false;
}
if(attempt === "initial") {
var url = baseURL + encodeURIComponent(doi) + '?format=json';
} else {
var url = baseURL + encodeURIComponent(doi);
}
var url = baseURL + encodeURIComponent(doi) + '?format=json';

return url;
}
Expand All @@ -200,19 +208,20 @@ Zotero.ShortDOI.generateItemUrl = function(item, attempt) {

Zotero.ShortDOI.updateItem = function(item) {
var req = new XMLHttpRequest();
var url = Zotero.ShortDOI.generateItemUrl(item, "initial");
var url = Zotero.ShortDOI.generateItemUrl(item);
if ( ! url ) {
Zotero.ShortDOI.updateNextItem();
} else {
req.open('GET', url, true);
req.responseType = 'json';

req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200 && req.responseText !== '') {
if (req.status == 200 && req.response) {
if (item.isRegularItem() && !item.isCollection()) {
var doiResponse = JSON.parse(req.responseText);
var doiResponse = req.response;
var shortDOI = doiResponse.ShortDOI;
if(Zotero.ShortDOI.savelong) {
if (Zotero.ShortDOI.savelong) {
var longDOI = doiResponse.DOI;
var longDOIstring = 'Long DOI: ' + longDOI + ' '
try {
Expand All @@ -239,29 +248,11 @@ Zotero.ShortDOI.updateItem = function(item) {
}
}
Zotero.ShortDOI.updateNextItem();
} else if (req.status == 200 ||
req.status == 403 ||
req.status == 503) {
alert(Zotero.ShortDOI.DOInotfoundString);
var url2 = Zotero.ShortDOI.generateItemUrl(item, "notfound");
req2 = new XMLHttpRequest();
req2.open('GET', url2, true);
req2.onreadystatechange = function() {
if (req2.readyState == 4) {
if (typeof Zotero.launchURL !== 'undefined') {
Zotero.launchURL(url);
} else if (typeof Zotero.openInViewer !== 'undefined') {
Zotero.openInViewer(url);
} else if (typeof ZoteroStandalone !== 'undefined') {
ZoteroStandalone.openInViewer(url);
} else {
window.gBrowser.loadOneTab(
url, {inBackground: false});
}
Zotero.ShortDOI.resetState();
}
}
req2.send(null);
} else if (req.status == 400) {
Zotero.ShortDOI.invalidDOI = true;
item.setTags(['_Invalid DOI']);
item.saveTx();
Zotero.ShortDOI.updateNextItem();
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion chrome/locale/en-US/zoteroshortdoi.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DOInotfoundString=DOI not found. taking you to http://shortdoi.org to view the error.
invalidDOIString=Invalid DOI
invalidDOITagString=Invalid DOIs were found. These have been tagged with _Invalid DOI.
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RDF:about="urn:mozilla:install-manifest"
em:id="zoteroshortdoi@wiernik.org"
em:name="Zotero shortDOI Lookup"
em:version="1.0.2"
em:version="1.0.3"
em:type="2"
em:creator="Brenton M. Wiernik"
em:description="Zotero plugin for auto-fetching shortDOI names"
Expand Down
6 changes: 3 additions & 3 deletions update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<em:version>1.0.2</em:version>
<em:version>1.0.3</em:version>
<em:targetApplication>
<rdf:Description>
<em:id>zotero@chnm.gmu.edu</em:id>
<em:minVersion>5.0</em:minVersion>
<em:maxVersion>5.*</em:maxVersion>
<em:updateLink>https://github.com/bwiernik/zotero-shortdoi/releases/download/v1.0.2/zotero-shortdoi-1.0.2.xpi</em:updateLink>
<em:updateLink>https://github.com/bwiernik/zotero-shortdoi/releases/download/v1.0.3/zotero-shortdoi-1.0.3.xpi</em:updateLink>
</rdf:Description>
</em:targetApplication>
<em:targetApplication>
<rdf:Description>
<em:id>juris-m@juris-m.github.io</em:id>
<em:minVersion>5.0</em:minVersion>
<em:maxVersion>5.*</em:maxVersion>
<em:updateLink>https://github.com/bwiernik/zotero-shortdoi/releases/download/v1.0.2/zotero-shortdoi-1.0.2.xpi</em:updateLink>
<em:updateLink>https://github.com/bwiernik/zotero-shortdoi/releases/download/v1.0.3/zotero-shortdoi-1.0.3.xpi</em:updateLink>
</rdf:Description>
</em:targetApplication>
</rdf:Description>
Expand Down

0 comments on commit a6d3f93

Please sign in to comment.