Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save smakary-gotrah/512efe60f7d4ff69cefb7e6bf646a643 to your computer and use it in GitHub Desktop.

Select an option

Save smakary-gotrah/512efe60f7d4ff69cefb7e6bf646a643 to your computer and use it in GitHub Desktop.
bit.ly URL shortener bookmarklet
// NOTE: The security policy of some sites (such as this gist.github.com!) will prevent this from working.
// Check your javascript console if something doesn't work.
javascript:(function(){
var apiKey = 'INSERT YOUR API KEY HERE';
if (apiKey == 'INSERT YOUR API KEY HERE') {
alert('Error: You need to add your api key to this bookmarklet!');
return;
}
var xhr = new XMLHttpRequest();
var url = 'https://api-ssl.bitly.com/v4/shorten';
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
prompt('Your short URL is:', json.id);
} else {
alert('Shortener error: ' + xhr.responseText);
}
}
};
var data = JSON.stringify({'group_guid':'Ba1bc23dE4F','domain':'bit.ly','long_url': window.location.href});
xhr.send(data);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment