Skip to content

Instantly share code, notes, and snippets.

@masnick
Last active July 26, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masnick/429809c30036a558bc44cd21d48f863d to your computer and use it in GitHub Desktop.
Save masnick/429809c30036a558bc44cd21d48f863d to your computer and use it in GitHub Desktop.
Universal "find at my library" bookmarklet for journal articles
var url = 'http://something.youruniversity.edu/foo/bar';
var doi;
var suffixes = {
"pmid": "?sid=Entrez:PubMed&id=pmid:",
"doi": "?sid=Entrez:DOI&id=doi:"
};
var re = /^[0-9]{5,15}/;
var found = jQuery(jQuery(".rprtid dd")[0]).text().match(re);
if(found != null) {
location.href = url + suffixes["pmid"] +found[0];
}
else {
// Try to find DOI link
var dois = jQuery.grep(jQuery("a").map(function() {return this.href;}), function(n, i) { return n.indexOf("dx.doi.org") !== -1;});
if(dois && dois.length > 0) {
doi = dois[0].replace(/.*dx.doi.org\//, "");
location.href = url + suffixes["doi"] + doi;
}
else {
// try to find DOI text
// Add spaces between all elements so `text()` will work properly
jQuery( "*" ).each(function( index ) {
jQuery( this ).append(" ");
});
var match = /doi\s*:?\s*(10\.[0-9]+\/[a-z0-9\.]*)/gi.exec(jQuery("body").text());
if(match && match.length > 1) {
doi = match[1];
location.href = url + suffixes["doi"] + doi;
}
else {
alert("Neither PubMed ID nor DOI link found.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment