Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* Use cli.gs to shorten URLs in Ubiquity. */
CmdUtils.CreateCommand({
names: ["cligs", "cli.gs", "cl.gs"],
icon: "http://cli.gs/favicon.ico",
description: "Shortens a URL using cli.gs",
help: "Type the URL to shorten. When executed, it's copied to the clipboard.",
author: {name: "Elad Kehat", email: "eladkehat@gmail.com"},
license: "GPL",
homepage: "http://philobuster.wordpress.com/",
arguments: [{role: 'object', nountype: noun_arb_text, label: "URL to shorten"},
{role: 'alias', nountype: noun_arb_text, label: "Title for the short URL"}],
preview: function preview(pblock, args) {
var escapedUrl = encodeURIComponent(args.object.text);
var apiUrl = "http://cli.gs/api/v1/cligs/create?url=" + escapedUrl;
var urlTitle = args.alias.text;
if (urlTitle.length > 0) {
apiUrl += "&title=" + encodeURIComponent(urlTitle);
}
pblock.innerHTML = "Replace the long URL with (calling " + apiUrl + "...)";
var me = this;
jQuery.get(apiUrl, {}, function(response) {
if (response.indexOf("http") == 0) {
me.shortUrl = response;
pblock.innerHTML = "Replaces the long URL with <strong>" + me.shortUrl + "</strong>";
} else {
pblock.innerHTML = "Error shortening URL";
}
}, 'text');
},
execute: function execute() {
if (this.shortUrl) {
CmdUtils.setSelection(this.shortUrl);
CmdUtils.copyToClipboard(this.shortUrl);
displayMessage({icon: this.icon, title: "Copied to clipboard", text: this.shortUrl});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.