satyr (owner)

Revisions

gist: 158923 Download_button fork
public
Description:
Trims a URL, or untrims a trimmed URL.
Public Clone URL: git://gist.github.com/158923.git
Embed All Files: show embed
tr.im.ubiq.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
CmdUtils.CreateCommand({
  name: 'tr.im',
  description: 'Trims a URL, or untrims a trimmed URL.',
  author: {name: 'satyr', email: 'murky.satyr@gmail.com'},
  license: 'MIT',
  homepage: 'http://tr.im/',
  icon: 'http://tr.im/favicon.ico',
  arguments: {
    object: noun_type_url,
    'alias custom URL': /\w+/,
    'instrument privacy code': /\w+/,
  },
  execute: function trim_execute(args){
    var me = this;
    this._trim(args, function trim_copy(url, msg){
      displayMessage(
        (url
         ? 'Copied <'+ (Utils.clipboard.text = url) + '> to your clipboard.'
         : msg),
        me);
    });
  },
  preview: function trim_preview(pb, args){
    pb.innerHTML = this._logo;
    this._trim(args, function trim_show(url, msg){
      var [logo] = pb.getElementsByClassName('logo');
      if(!logo) return;
      XML.prettyPrinting = false;
      pb.insertBefore(pb.ownerDocument.createElement('p'), logo).innerHTML = (
        url
        ? (<a href={url} accesskey="h"><b>h</b>{url.slice(1)}</a>)
        : (<em>{msg}</em>)
        ).toXMLString();
      logo.style.opacity = url ? 1 : .7;
    }, pb);
  },
  _trim: function trim_trim({object: {text: url}, alias, instrument}, cb, pb){
    var key = uneval([url, alias.text, instrument.text]), dic = this._dict;
    if(key in dic) return cb(dic[key]);
    var trimpath = (/^http:\/\/tr\.im\/(.+)/(url) || 0)[1];
    var {prefs} = Application;
    var params = {
      url: url,
      trimpath: trimpath,
      custom: alias.text,
      privacy: instrument.text,
      username: prefs.getValue('extensions.trim.username', ''),
      password: prefs.getValue('extensions.trim.password', ''),
      api_key: '7Ctm8v3s5Fr4ePaJ8xzsrJSQGwjC68SFkeAHDrydiuEKjccY',
    };
    for(let k in params) params[k] || delete params[k];
    var me = this, options = {
      url: ('http://tr.im/api/trim_'+
            (trimpath ? 'destination' : 'url') +'.json'),
      data: params,
      dataType: 'json',
      success: function trim_success(res){
        var u = res.url || res.destination;
        if(u) dic[key] = u;
        cb(u, (res.status || 0).message || uneval(res));
      },
      error: function trim_error(x, s){
        displayMessage(x.status +' '+ x.statusText +' ('+ s +')', me);
      },
    };
    pb ? CmdUtils.previewAjax(pb, options) : $.ajax(options);
  },
  _logo: (<a href="http://tr.im/" class="logo" accesskey="l"
          style="display:inline-block; opacity:0.4"
          ><img width="150" height="83" border="0"
          src="http://tr.im/images/main/logo_home.png"/></a>),
  _dict: {},
});