Skip to content

Instantly share code, notes, and snippets.

What would you like to do? URL shortener bookmarklet
// Step 1: Get an API key here:
// Step 2: Paste your api key below
// Step 3: Add the code below to your browser as a bookmark
// Step 4: Visit a URL and click your bookmark
// NOTE: The security policy of some sites (such as this!) will prevent this from working.
// Check your javascript console if something doesn't work.
var googleApiKey = 'INSERT YOUR API KEY HERE';
if (googleApiKey == 'INSERT YOUR API KEY HERE') {
alert('Error: You need to add your api key to this bookmarklet!');
var xhr = new XMLHttpRequest();
var url = '' + googleApiKey;'POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
prompt('Your short URL is:',;
} else {
alert('Shortener error: ' + xhr.responseText);
var data = JSON.stringify({'longUrl': window.location.href});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment