JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://call.jsonlib.com/jsonlib.js"></script>
JavaScript
var longUrl = "http://google.com";
document.write("Long Url: "+longUrl);
function googlurl(url, cb) {
jsonlib.fetch({
url: 'https://www.googleapis.com/urlshortener/v1/url',
header: 'Content-Type: application/json',
data: JSON.stringify({longUrl: url})
}, function (m) {
var result = null;
try {
result = JSON.parse(m.content).id;
if (typeof result != 'string') result = null;
} catch (e) {
result = null;
}
cb(result);
});
}
// Make a short URL for a nicely written book.
googlurl(longUrl , function(s) { document.write("<BR>Short Url: "+s); });