aboutsummaryrefslogtreecommitdiffstats
path: root/goo.gl.js
blob: 6ecde507be720711d27ac4caac63087598ebcb1e (plain)
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
// {{{ INFO
var INFO =xml`
<plugin name="goo.gl.js" version="0.2"
        summary="google url shortener"
        href="http://github.com/vimpr/vimperator-plugins/blob/master/goo.gl.js"
        xmlns="http://vimperator.org/namespaces/liberator">
  <author email="mitsugu.oyama@gmail.com">Mitsugu Oyama</author>
  <license href="http://opensource.org/licenses/mit-license.php">MIT</license>
  <project name="Vimperator" minVersion="3.3"/>
  <p>Shorten URL by used of google</p>
  <item>
    <tags>'googlUrlShortener'</tags>
    <spec>:googlUrlShortener</spec>
    <description>
      <p>Shorten URL by used of google</p>
    </description>
  </item>
</plugin>`;
// }}}

commands.addUserCommand(
  ['googleUrlShortener'],
  'google url shortener',
  function(){
// {{{ environment
    let contents=gBrowser.selectedBrowser.contentDocument;
    const endPoint='https://www.googleapis.com/urlshortener/v1/url';
    const contentType='application/json';
    let xhr;
// }}}

// {{{ edit to json from long URL
    let tmp={};
    tmp.longUrl=contents.URL;
    let jsonString=JSON.stringify(tmp);
//}}}

// {{{ convert success
    function getData(){
      let ret=JSON.parse(xhr.responseText);
      liberator.echo('Shorten URL to \"'+ret.id+'\"');
      util.copyToClipboard(ret.id);
    }
// }}}

// {{{ false convert
    function requestError(){
      liberator.echoerr('cannot convert by used google url shortener');
      return false;
    }
// }}}

// {{{ XMLHttpRequest
    xhr=new XMLHttpRequest();
    xhr.addEventListener("load",getData,false);
    xhr.addEventListener("error",requestError,false);
    xhr.open("POST",endPoint);
    xhr.setRequestHeader('Content-Type',contentType);
    xhr.send(jsonString);
//  }}}
  },
  {},
  true
);
lass="kr">interface; this.__defineGetter__('interface', function(){ if (_interface) return _interface; _interface = {}; var cl; try { cl = Cc[this.name].createInstance(); } catch (e){ try { cl = Cc[this.name].getService(); } catch (ex){ liberator.log(this.name); liberator.log(e); return _interface; } } for (let i in Ci){ if (i == 'nsISupports' || i == 'IDispatch') continue; let obj; try { obj = cl.QueryInterface(Ci[i]); } catch (e){ continue; } if (obj){ _interface[i] = new XPInterface(this.name, i); } } return _interface; }); } //}}} XPClass.prototype = { //{{{ get number() Cc[this.name].number, has: function(filter){ var flag = false; if (filter instanceof RegExp){ for (let i in this.interface){ if(flag = filter.test(i)) break; } } else { flag = (interface in this.interface); } return flag; }, toString: function() this.name }; //}}} function XPInterface(c, i){ //{{{ this.interface = Ci[i]; this.class = Cc[c]; } //}}} XPInterface.prototype = { //{{{ get number() this.interface.number, get name() this.interface.name, get classNumber() this.class.number, get className() this.class.name, toString: function() this.name + ' of ' + this.className, create: function(){ var instance; try { instance = this.class.createInstance(this.interface); } catch (e){ try { instance = this.class.getService(this.interface); }catch (e){} } return instance; } }; //}}} function toKey(str) str.replace(/\W(.)/g, function(m, p) p.toUpperCase()); var tree = {}; for (let c in Cc){ let key = toKey(c); tree[key] = new XPClass(c); tree[c] = tree[key]; } // ---------------------------------------------- // Commands // ---------------------------------------------- commands.addUserCommand(['lscc'], 'List XPCOM class', function(args){ if (!args.length){ liberator.echoerr('No arguments'); return; } if (args.length == 1){ liberator.echo(liberator.XPCOM.listClass(args[0], null, true), true); } else if (args[1] in Ci){ let instance = tree[toKey(args[0])].interface[args[1]].create(); liverator.echo(liberator.modules.util.objectToString(instance, true), true); } else { liberator.echo(liberator.XPCOM.listClass(args[0], args[1], true), true); } }, { completer: function(context, args){ if (!args.length) return; var list = []; var position = 0; var reg; if (args.length == 1){ reg = new RegExp(args[0], 'i'); for (let c in Cc){ if (reg.test(c)) list.push([Cc[c].name, Cc[c].number]); } } else if (args.length == 2 && args[0] in Cc){ reg = new RegExp(args[1], 'i'); for (let i in tree[toKey(args[0])].interface){ if (reg.test(i)) list.push([Ci[i].name, Ci[i].number]); } position = args[0].length + 1; } context.title = ['Name', 'Number']; context.advance(position); context.completions = list; } } ); var manager = { get all() tree, toKey: function(class) toKey(class), toHTML: function(list){ var str = ['<dl>']; list.forEach(function(o){ str.push('<dt>' + o.name + ' ' + o.number + '</dt>'); if (o.interface){ for (let i in o.interface){ str.push('<dd>' + o.interface[i].name + ' ' + o.interface[i].number + '</dd>'); } } else { for (let member in o){ str.push('<dd>' + member + ': ' + o[member] + '</dd>'); } } }); str.push('</dl>'); return str.join(''); }, listClass: function(cFilter, iFilter, format){ var classes = {}; if (!cFilter) return null; cReg = new RegExp(cFilter, 'i'); iReg = new RegExp((iFilter ? iFilter : '.*'), 'i'); for (let c in tree){ if (cReg.test(tree[c].name) && tree[c].has(iReg)){ classes[tree[c].name] = tree[c]; } } var list = [i for each (i in classes)]; if (format) return this.toHTML(list); return list; }, listInterface: function(iFilter, format){ var list = []; if (!iFilter) return null; iReg = new RegExp(iFilter, 'i'); for (let i in Ci){ if (iReg.test(i)) list.push(Ci[i]); } if (format) return this.toHTML(list); return list; } }; return manager; })(); // vim: sw=4 ts=4 sts=0 fdm=marker noet: