Skip to content

Instantly share code, notes, and snippets.

@danixland
Last active October 3, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danixland/5693635 to your computer and use it in GitHub Desktop.
Save danixland/5693635 to your computer and use it in GitHub Desktop.
goo.gl bookmarklet - this bookmarklet will return the shortened url of the current page
<a href="javascript:var%20uri=window.location.href;xhr=new%20XMLHttpRequest();xhr.open('POST','https://www.googleapis.com/urlshortener/v1/url',false);xhr.setRequestHeader('Content-Type','application/json');xhr.send(JSON.stringify({longUrl:uri}));var%20json=xhr.responseText;var%20obj=JSON.parse(json);alert(obj.id);">goo.gl this page</a>
@rlstne
Copy link

rlstne commented May 27, 2014

hi,

i used the JS as bookmarklet.. it works great but i dont have the option to copy the link.. any idea?

thanks,

@GYFHAS
Copy link

GYFHAS commented Mar 7, 2015

'Ctrl'+'C'

@TekBear
Copy link

TekBear commented Aug 12, 2016

I kept getting this error message in Firefox console "[09:35:41.459] POST https://www.googleapis.com/urlshortener/v1/url [HTTP/1.1 403 Forbidden 250ms]"
I managed to fix it by adding "url?key=xxx" where xxx is your personal API key you create using https://console.developers.google.com/apis/credentials. Create a project item then create a browser API key for "urlshortener" and it will give you a unique string "xxx". Also prefer "void(prompt('Short%20url',obj.id))" rather than "alert(obj.id)".

Is an API key now mandatory for "urlshortener"? Any shortners created with "key=xxx" are not listed at http://goo.gl is there a way to fix that?

Does anyone know if you can use Oauth 2.0 key? I could not figure out how to set it up as it looks like it has to be bound to a domain.

@davchana
Copy link

davchana commented Oct 3, 2017

Working Code as of Now

var url = window.location.href;
xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXXXXXXX', false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    longUrl: url
}));
var json = xhr.responseText;
var obj = JSON.parse(json);
prompt('short url', obj.id);

Get Key from https://developers.google.com/url-shortener/v1/getting_started#APIKey (&replace XXXXXXXX with it)

Minified above code: (replace XXXX with your key):

var a=window.location.href;xhr=new XMLHttpRequest;xhr.open("POST","https://www.googleapis.com/urlshortener/v1/url?key=XXXXXX",!1);xhr.setRequestHeader("Content-Type","application/json");xhr.send(JSON.stringify({a:a}));var b=JSON.parse(xhr.responseText);prompt("short url",b.id);

Bookmarkified above code: (replace XXXX with your key):

javascript:(function()%7Bvar%20a%3Dwindow.location.href%3Bxhr%3Dnew%20XMLHttpRequest%3Bxhr.open(%22POST%22%2C%22https%3A%2F%2Fwww.googleapis.com%2Furlshortener%2Fv1%2Furl%3Fkey%3DXXXXXX%22%2C!1)%3Bxhr.setRequestHeader(%22Content-Type%22%2C%22application%2Fjson%22)%3Bxhr.send(JSON.stringify(%7Ba%3Aa%7D))%3Bvar%20b%3DJSON.parse(xhr.responseText)%3Bprompt(%22short%20url%22%2Cb.id)%7D)()

above code will not work without Valid key (Error: Bad Key)

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