Created
April 6, 2009 10:57
-
-
Save satyr/90718 to your computer and use it in GitHub Desktop.
Shortens a URL by the service of your choice.
This file contains hidden or 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
const Name = 'shorten-url', | |
Shorteners = [{ | |
text: 'tinyurl.com', | |
data: function(lu, cb)({ | |
url: 'http://tinyurl.com/api-create.php', | |
type: 'post', | |
data: {url: lu}, | |
success: cb, | |
}), | |
logo: <h1 style={( | |
'background-color:#009; color:#eee; padding:0 3px;'+ | |
'display:inline; line-height:1')}>TinyURL.com</h1>, | |
}, { | |
text: 'bit.ly', | |
data: function(lu, cb, er)({ | |
url: 'http://api.bit.ly/shorten', | |
data: { | |
version: '2.0.1', longUrl: lu, | |
login: 'ubiquitron', apiKey: 'R_4d6fa3cba425f012582475355652f8b5'}, | |
dataType: 'json', | |
success: function(res){ | |
if(res.statusCode === 'OK') | |
for each(var o in res.results) return cb(o.shortUrl); | |
er({status: res.errorCode, statusText: res.errorMessage}); | |
}, | |
}), | |
logo: (<img style="height:47px; background-color:#fff; padding:0 3px" | |
src="http://bit.ly/static/images/bitly_logo_top.png"/>), | |
}, { | |
text: 'tr.im', | |
data: function(lu, cb)({ | |
url: 'http://api.tr.im/api/trim_simple', | |
data: {url: lu, username: 'ubiquity', password: 'mozilla'}, | |
success: cb, | |
}), | |
logo: (<img width="150" height="83" | |
src="http://tr.im/images/main/logo_home.png"/>), | |
}, { | |
text: 'is.gd', | |
data: function(lu, cb)({ | |
url: 'http://is.gd/api.php', | |
data: {longurl: lu}, | |
success: cb, | |
}), | |
logo: <img width="150" height="93" src="http://is.gd/isgdlogo_small.jpg"/>, | |
}, { | |
text: 'cli.gs', | |
data: function(lu, cb)({ | |
url: 'http://cli.gs/api/v1/cligs/create', | |
data: { | |
url: lu, appid: 'ubiquity', | |
key: 'e36552b87c6d7f9a73b2a16ed310fcdb'}, | |
success: cb, | |
}), | |
logo: <img width="280" height="60" src="http://cli.gs/images/logo.jpg"/>, | |
}, { | |
text: 'snipr.com', | |
data: function(lu, cb)({ | |
url: 'http://sn.im/site/getsnip', | |
type: 'post', | |
data: { | |
sniplink: lu, snipformat: 'simple', | |
snipuser: 'ubiquity', snipapi: '3e2bbc25e90e97a8a8c0abea311927b5'}, | |
success: function(su){ cb(su === 'ERROR' ? lu : su) }, | |
}), | |
logo: (<img width="176" height="45" | |
src="http://cache.snipurl.com/snipr-mast.png"/>), | |
}, { | |
text: 'hurl.ws', | |
data: function(lu, cb)({ | |
url: 'http://www.hurl.ws/api/', | |
type: 'post', | |
data: {url: lu, user: 'ubiquity'}, | |
success: cb, | |
}), | |
logo: (<img style="height:56px; background-color:#fff; padding:3px" | |
src="http://www.hurl.ws/images/logo.gif"/>), | |
}, { | |
text: 'rubyurl.com', | |
data: function(lu, cb, er)({ | |
url: 'http://rubyurl.com/api/links.json', | |
type: 'post', | |
data: {website_url: lu}, | |
dataType: 'json', | |
success: function(res){ | |
if(typeof res === 'object') cb(res.link.permalink); | |
else er({status: 666, statusText: res}); | |
}, | |
}), | |
logo: (<img style="height:49px; background-color:#eee; padding:3px" | |
src="http://rubyurl.com/images/logo-rubyurl.png"/>), | |
}, { | |
text: 'short.ie', | |
data: function(lu, cb)({ | |
url: 'http://short.ie/api', | |
data: { | |
url: lu, format: 'plain', email: '[email protected]', | |
secretKey: '18661baf56e3c18520d3bcf0e7b1ba61208387c8'}, | |
success: cb, | |
}), | |
logo: (<img width="147" height="41" style="background-color:#eee" | |
src="http://short.ie/media/images/logo2.png"/>), | |
}, { | |
text: 'kl.am', | |
data: function(lu, cb)({ | |
url: 'http://kl.am/api/shorten/', | |
data: { | |
url: lu, format: 'text', tags: 'ubiquity mozilla', | |
api_key: '2102eb935a7447618523a65abc324691'}, | |
success: cb, | |
}), | |
logo: <h1 style={( | |
'background-color:#ccc; color:#333; padding:0 3px;'+ | |
'display:inline; line-height:1')}>kl.am</h1>, | |
}, { | |
text: 'poprl.com', | |
data: function(lu, cb, er)({ | |
url: 'http://poprl.com/api/post', | |
type: 'post', | |
data: {url: lu, out: 'text'}, | |
success: function(res){ | |
var [, stat, text] = /^(\d+): (.+)/(res) || 0; | |
stat ? er({status: stat, statusText: text}) : cb(res); | |
}, | |
}), | |
logo: <img width="214" height="64" src="http://poprl.com/img/logo_v3.jpg"/>, | |
}, { | |
text: 'ad.vu', | |
data: function(lu, cb)({ | |
url: 'http://api.adjix.com/shrinkLink', | |
data: { | |
url: lu, ultraShort: 'y', | |
partnerID: '8989aa095f1f4c2b93508666fcc1c27d'}, | |
success: cb, | |
}), | |
logo: (<img width="170" height="80" | |
src="https://adjix.s3.amazonaws.com/images/adjix-logo.gif"/>), | |
} | |
/* | |
, { | |
text: '', | |
data: function(lu, cb)({ | |
url: 'http://', | |
data: {}, | |
success: cb, | |
}), | |
logo: <img width="" height="" src=""/>, | |
} | |
*/ | |
].sort(function({text: a}, {text: b}) | |
!(a.length === b.length ? a <= b : a.length <= b.length)), | |
{prefs} = Application, | |
PSU = 'extensions.ubiquity.'+ Name +'.', | |
PDef = PSU + 'default', | |
Default = prefs.getValue(PDef, Math.random() * Shorteners.length | 0); | |
Base = <div class={Name}><style><![CDATA[ | |
$pre {overflow:hidden; margin-top:0} | |
$a {text-decoration:none} | |
$kbd {text-decoration:underline} | |
$.to {font-weight:bolder} | |
$.logo img {border:none} | |
$.success .to:before {content:'\2192'; margin-right:0.7em} | |
$.loading .to {display:none} | |
$.loading .logo {opacity:0.4} | |
$.error .to {font-style:oblique} | |
$.error .logo {opacity:0.7} | |
]]></style><div id={Name} | |
><pre class="to"> </pre | |
><pre class="from"> </pre | |
><div class="logo"> </div | |
></div></div>.toXMLString().replace(/\$/g, '.'+ Name +' '); | |
var pu, ps, result; | |
function set(u){ | |
CmdUtils.setSelection(u.link(u), {text: u}); | |
CmdUtils.copyToClipboard(u); | |
} | |
Shorteners.forEach(function(s){ | |
var {text, icon, logo} = s, link = 'http://'+ text; | |
s.summary = text; | |
s.logo = ''+<a href={link} accesskey="l">{logo}</a>; | |
if(!icon) s.icon = link +'/favicon.ico'; | |
}); | |
CmdUtils.CreateCommand({ | |
name: Name, | |
description: 'Shortens a URL by the service of your choice.', | |
help: ( | |
'Powered by: '+ | |
Shorteners.map(function({text}) text.link('http://'+ text)).join(', ') + | |
<form>Default: { | |
Shorteners.reduce( | |
function(select, {text}, i){ | |
var opt = <option value={i}>{text}</option>; | |
if(i === Default) opt.@selected = 'selected'; | |
return select.appendChild(opt); | |
}, | |
<select onmouseover={( | |
'this.selectedIndex = Application.prefs.getValue("'+ PDef + | |
'", eval(this.value))')} | |
onchange={('Application.prefs.setValue("'+ PDef + | |
'", eval(this.value))')}/>)} | |
</form>), | |
synonyms: [Shorteners[Default].text], | |
icon: Shorteners[Default].icon, | |
takes: {URL: { | |
_name: 'url', | |
default: function() CmdUtils.makeSugg(CmdUtils.getWindow().location.href), | |
suggest: function(txt, htm, cb, sx){ | |
if(/^@| @ /(txt)) return []; | |
var [u] = /^\S{1,}/(txt) || 0; | |
if(!u) return [this.default()]; | |
if(!/^[a-z]+:\/\/./.test(u)) u = 'http://'+ u; | |
return [CmdUtils.makeSugg(u, u, 0, sx)]; | |
}, | |
}}, | |
modifiers: {'@': { | |
_name: 'url shortener', | |
default: function() Shorteners[prefs.getValue(PDef, Default)], | |
suggest: function(txt, htm, cb, sx){ | |
if(sx) return []; | |
if(!txt) return [this.default()]; | |
try { var sf = [], re = RegExp(txt, 'i') } catch(_){} | |
if(re) for(var i = 0, l = Shorteners.length; i < l; ++i){ | |
var it = Shorteners[i], x = it.text.search(re); | |
if(~x) sf[x * l + i] = it; | |
} | |
if(sf.length){ | |
var ss = sf.filter(Boolean), [s] = ss; | |
this.default = function() s; | |
return ss; | |
} | |
return Shorteners; | |
}, | |
}}, | |
execute: function({text: U}, {'@': S}){ | |
if(result) return set(result); | |
var opts = S.data(U, set); | |
opts.error = function(x, s, e){ | |
displayMessage({ | |
icon: S.icon, title: S.text, exception: e, | |
text: x.status +' '+ x.statusText +' ('+ s +')' }); | |
}; | |
jQuery.ajax(opts); | |
}, | |
preview: function(pbl, {text: U}, {'@': S}){ | |
do var $c = jQuery('#'+ Name, pbl); | |
while(!$c[0] && (ps = '', pbl.innerHTML = Base)); | |
if(ps !== (ps = S)){ | |
this.icon = S.icon; | |
this.synonyms = [S.text]; | |
$c.find('.logo')[0].innerHTML = S.logo; | |
pu = ''; | |
} | |
if(!U || (pu === (pu = U))) return; | |
result = ''; | |
try { var du = decodeURIComponent(U) } catch(_){} | |
$c.find('.from')[0].textContent = du || U; | |
$c[0].className = 'loading'; | |
var err = function(x){ | |
$c.find('.to')[0].textContent = x.status +' '+ x.statusText; | |
$c[0].className = 'error'; | |
}, | |
opts = S.data(U, function(u){ | |
result = u; | |
$c.find('.to')[0].innerHTML = | |
'<a href="'+ u +'" accesskey="h"><kbd>h</kbd>'+ u.slice(1) +'</a>'; | |
$c[0].className = 'success'; | |
}, err); | |
opts.error = err; | |
CmdUtils.previewAjax(pbl, opts); | |
}, | |
previewDelay: 222, | |
author: 'satyr'.link('http://ubigist.appjet.net/?o=satyr'), license: 'MIT', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment