Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Created March 5, 2012 10:40
Show Gist options
  • Save hongymagic/1977829 to your computer and use it in GitHub Desktop.
Save hongymagic/1977829 to your computer and use it in GitHub Desktop.
Bookmarklet to shorten URLs with hon.gy!
/* vim: set noexpandtab ts=4 sw=4 ai si: */
(function (window) {
var document = window.document,
body = document.body,
url = document.location,
//
// Watch for ready state change on <script> block, and trigger given
// callback when <script> has been loaded
watch = function (callback) {
return function () {
if (/ded|co/.test(this.readyState)) {
callback();
}
};
},
//
// Download and load a given <script> block. If callback is supplied,
// callback will be called when <script> has been loaded
js = function (src, callback) {
var script = document.createElement('scr' + 'ipt');
script.src = src;
body.appendChild(script);
if (script.onreadystatechange === null) {
script.onreadystatechange = watch(callback);
} else {
script.onload = callback;
}
},
//
// Notify the user of the shortened url
notify = function (response) {
var shortened = response.short;
if (shortened) {
alert('Short URL is: ' + shortened);
}
};
//
// The server needs to output CORS header when asked for OPTIONS:
// Access-Control-Allow-Origin: *
js('https://raw.github.com/ded/reqwest/master/reqwest.min.js', function () {
reqwest({
url: 'http://hon.gy/',
method: 'post',
data: { url: url.href },
type: 'json',
success: notify
});
});
}(this));
/* vim: set noexpandtab ts=4 sw=4 ai si: */(function(a){var b=a.document,c=b.body,d=b.location,e=function(a){return function(){/ded|co/.test(this.readyState)&&a()}},f=function(a,d){var f=b.createElement("script");f.src=a,c.appendChild(f),f.onreadystatechange===null?f.onreadystatechange=e(d):f.onload=d},g=function(a){var b=a.short;b&&alert("Short URL is: "+b)};f("https://raw.github.com/ded/reqwest/master/reqwest.min.js",function(){reqwest({url:"http://hon.gy/",method:"post",data:{url:d.href},type:"json",success:g})})})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment