Revisions

gist: 141881 Download_button fork
public
Public Clone URL: git://gist.github.com/141881.git
Embed All Files: show embed
x.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
CmdUtils.CreateCommand({
  names: ["bitly"],
  arguments: noun_type_url,
  icon: "http://bit.ly/favicon.png",
  description: "Replaces the selected URL with a shortened version from <a href=\"http://bit.ly/go\">Bit.ly</a><br/>And copies it to your clipboard for further use",
  preview: function(pblock, {object: {text}}){
    if (!text) {
      pblock.innerHTML = this.description;
      return;
    }
    var me = this;
    pblock.innerHTML = _("Replaces the selected URL with...");
    CmdUtils.previewGet(pblock, this._api(text), function(tinyUrl) {
      if(tinyUrl !== "Error")
        pblock.innerHTML = _("Replaces the selected URL with <b>${tinyurl}</b>.",
                             {tinyurl:me._link(tinyUrl)});
    });
  },
  execute: function(args) {
    var me = this;
    jQuery.get(this._api(args.object.text), function(tinyUrl) {
      CmdUtils.setSelection(me._link(tinyUrl), {text: tinyUrl});
      Utils.clipboard.text = tinyUrl;
      displayMessage("Copied link: " + tinyUrl);
    });
  },
  _api: function(url)("http://bit.ly/api?url=" +
                      encodeURIComponent(url)),
  _link: function(url) {
    var eu = Utils.escapeHtml(url);
    return eu.link(eu);
  },
});