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
);