Last active
July 26, 2017 16:43
-
-
Save masnick/429809c30036a558bc44cd21d48f863d to your computer and use it in GitHub Desktop.
Universal "find at my library" bookmarklet for journal articles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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