Skip to content

Instantly share code, notes, and snippets.

@mkuhn
Last active February 6, 2023 17:10
Show Gist options
  • Save mkuhn/840769 to your computer and use it in GitHub Desktop.
Save mkuhn/840769 to your computer and use it in GitHub Desktop.
ShortDOI Bookmarklet
javascript:var%20metas=document.getElementsByTagName('meta');var%20a='';for(i=0;i<metas.length;i++){if(metas[i].getAttribute('name')=='citation_doi'){a=metas[i].getAttribute('content')}}if(a){location.href='http://shortdoi.org/'+a}else{a=document.documentElement.innerHTML.match(/doi%20*[:=]%20*10[-/0-9a-z.]\/[-/0-9a-z.]+/mig)||document.documentElement.innerHTML.match(/dx.doi.org\/10[-/0-9a-z.]+/mig);if(a==null){alert('Could%20not%20find%20DOI!')}else{a=a.map(function(x){return%20x.match('10.*')[0]});var%20d=a[0];if(a.length>1){var%20c=new%20Array();for(i%20in%20a){if(c[a[i]]){c[a[i]]++}else{c[a[i]]=1}}var%20m=0;d=null;for(i%20in%20c){if(c[i]>m){m=c[i];d=i}else%20if(c[i]==m){d=null}}}if(d==null){alert('More%20than%20one%20DOI%20found!')}else{location.href='http://shortdoi.org/'+d}}}
var metas = document.getElementsByTagName('meta');
var a = '';
for (i=0; i<metas.length; i++) {
if (metas[i].getAttribute('name') == 'citation_doi') {
a = metas[i].getAttribute('content');
}
}
if (a) {
location.href='http://shortdoi.org/'+a;
} else {
a = document.documentElement.innerHTML.match(/doi *[:=] *10[-/0-9a-z.]\/[-/0-9a-z.]+/mig) || document.documentElement.innerHTML.match(/dx.doi.org\/10[-/0-9a-z.]+/mig);
if (a==null) {
alert('Could not find DOI!')
} else {
a=a.map(function(x){
return x.match('10.*')[0];}
);
var d=a[0];
if(a.length>1){
var c=new Array();
for(i in a) {
if(c[a[i]]) {
c[a[i]]++;
} else{
c[a[i]]=1;
}
}
var m=0;
d=null;
for (i in c) {
if(c[i]>m)
{
m=c[i];
d=i;
} else if (c[i]==m) {
d=null;
}
}
}
if(d==null){
alert('More than one DOI found!');
} else {
location.href='http://shortdoi.org/'+d;
}
}
}
@tamunro
Copy link

tamunro commented Feb 6, 2023

This is wonderful, thanks! I use it frequently. I had noticed it fails to detect DOIs on some pages with valid links, such as ACS journals, e.g.:
http://doi.org/crxxtd
I think the problem is the search for 'dx.doi.org', which is now deprecated. I don't know javascript. I deleted the 'dx.', and the DOI is then detected, but it only captures the prefix. So apparently my edit breaks the script, or something else is weird about the ACS links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment