Just a quick and dirty batch link generator for bit.ly. Done on @HugoGiraudel request.
Created
April 14, 2015 08:45
-
-
Save anonymous/56c6cc5f6753039faf28 to your computer and use it in GitHub Desktop.
Bit.ly Batch Link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea id="links"></textarea> | |
<button id="short">shorten!</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(exports){ | |
// Trim polyfill | |
''.trim || (String.prototype.trim = function(){ | |
return this.replace(/^\s+|\s+$/g,''); | |
}); | |
var get = function(request, callback){ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", request); | |
xhr.onreadystatechange = function() { | |
if(xhr.readyState == 4) { | |
if(xhr.status==200) { | |
callback(xhr.responseText, undefined); | |
return; | |
} | |
} | |
callback(undefined, xhr); | |
} | |
xhr.send(); | |
}; | |
exports.bitly = { | |
baseUrl : 'https://api-ssl.bitly.com/', | |
setUsernameKey : function(username, apikey){ | |
this.username = username; | |
this.apikey = apikey; | |
}, | |
shorten : function(url, callback){ | |
if (!this.username || !this.apikey){ | |
callback(undefined); | |
} | |
var request = this.baseUrl+'/v3/shorten?format=txt&login='+this.username+'&apiKey='+this.apikey+'&longUrl='+encodeURIComponent(url); | |
get(request, function(data, error){ | |
callback({longLink :url, shortLink : data}, error); | |
}); | |
} | |
}; | |
})(window); | |
var shortN = document.getElementById('short'), | |
linksN = document.getElementById('links') | |
shortN.addEventListener('click', function(){ | |
var username = "o_53ada8s0pn", | |
apikey = "R_44478b7a6c066e52c93f3c4724229971", | |
links = linksN.value; | |
if (username.length !== 0 && apikey.length !== 0 && links.length !== 0){ | |
bitly.setUsernameKey(username, apikey); | |
var linksArr = links.split('\n'); | |
var replaceWithShort = function(longLink, shortLink){ | |
linksN.value = links = links.replace(longLink, shortLink); | |
} | |
for (var i=0;i<linksArr.length;i++){ | |
bitly.shorten(linksArr[i].trim(), function(data, error){ | |
if (!error){ | |
replaceWithShort(data.longLink, data.shortLink.trim()); | |
} | |
}); | |
} | |
}else{ | |
// Feedback | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro); | |
body{ | |
font-family: 'Source Sans Pro', sans-serif; | |
} | |
.user-data{ | |
max-width:400px; | |
} | |
.block{ | |
display:block; | |
border:1px solid gray; | |
border-radius:.5em; | |
padding:.5em 1.25em 1em 1em; | |
margin:.5em; | |
float:left; | |
text-align:center; | |
width:100%; | |
} | |
textarea{ | |
width:100%; | |
height:300px; | |
} | |
label{ | |
display:block; | |
text-align:left; | |
} | |
label:after{ | |
content:':'; | |
} | |
input{ | |
width:100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment