Skip to content

Instantly share code, notes, and snippets.

@jpwatts
Created May 30, 2010 02:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpwatts/418716 to your computer and use it in GitHub Desktop.
Save jpwatts/418716 to your computer and use it in GitHub Desktop.
A bookmarklet for finding a short URL for a page
// javascript:(function(){var l=(document.querySelector('[rev~="canonical"]')||document.querySelector('[rel~="alternate"][rel~="short"]')||document.querySelector('[rel~="shortlink"]')||document.querySelector('[rel~="shorturl"]')||document.querySelector('[rel~="alternate"][rel~="shorter"]'));if(l){prompt("Short URL:",l.getAttribute('href'));}else{window.shortUrlBookmarkletCallback=function(bitly){if(bitly.status_code===200){prompt("Bit.ly URL:",bitly.data.url);}else{alert("Bit.ly error:"+bitly.status_txt);}};var s=document.createElement('script');s.type='text/javascript';s.src=('http://api.bit.ly/v3/shorten'+'?longURL='+encodeURIComponent(window.location.href)+'&domain=j.mp'+'&format=json'+'&callback=shortUrlBookmarkletCallback'+'&login=BITLY_USERNAME'+'&apiKey=BITLY_API_KEY');document.querySelector('head').appendChild(s);}})();
(function() {
var l = (document.querySelector('[rev~="canonical"]')
|| document.querySelector('[rel~="alternate"][rel~="short"]')
|| document.querySelector('[rel~="shortlink"]')
|| document.querySelector('[rel~="shorturl"]')
|| document.querySelector('[rel~="alternate"][rel~="shorter"]'));
if (l) {
prompt("Short URL:", l.getAttribute('href'));
} else {
window.shortUrlBookmarkletCallback = function(bitly) {
if (bitly.status_code === 200) {
prompt("Bit.ly URL:", bitly.data.url);
} else {
alert("Bit.ly error: " + bitly.status_txt);
}
};
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = ('http://api.bit.ly/v3/shorten'
+ '?longURL=' + encodeURIComponent(window.location.href)
+ '&domain=j.mp'
+ '&format=json'
+ '&callback=shortUrlBookmarkletCallback'
+ '&login=BITLY_USERNAME'
+ '&apiKey=BITLY_API_KEY');
document.querySelector('head').appendChild(s);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment