aboutsummaryrefslogtreecommitdiffstats
path: root/googlekanji.js
blob: 59eed153a371d1486f9ef417edd89bea1079ebab (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ==VimperatorPlugin==
// @name           Google-Kanji
// @description-ja グーグルを使って漢字を検索
// @license        Creative Commons 2.1 (Attribution + Share Alike)
// @version        1.0
// ==/VimperatorPlugin==
//
// Usage:
//    :gkanji うぶめ
//    のようにひらがななどで読みを入力します。
//    すると、
//    :gkcopy
//    が開き、補完が可能になるので、正しそうな漢字を選びます。
//    すると、クリップボードにその漢字がコピーされます。

(function () { try {

  var copycompl = [];

  function getKanji (word) {
    var re = /[一-龠]+/g;
    var ignore = /検索|関連/;
    var req = new XMLHttpRequest();
    var word = encodeURIComponent(word);
    liberator.log(word);
    req.open('GET', 'http://www.google.co.jp/search?hl=ja&q=' + word + '&lr=lang_ja', true);
    var f = function () {
      var cnt = {};
      for each (let it in req.responseText.match(re)) {
        if (ignore.test(it))
          continue;
        if (cnt[it])
          cnt[it] += 1;
        else
          cnt[it] = 1;
      }
      var cnta = [];
      for (let i in cnt) {
        if (cnt[i] < 3)
          continue;
        cnta.push([i, cnt[i]]);
      }
      cnta.sort(function (a, b) b[1] - a[1]);
      copycompl = cnta;
      liberator.commandline.open(":", "gkcopy ", liberator.modes.EX);
    };
    req.onreadystatechange = function (aEvt) {
      if (req.readyState == 4 && req.status == 200) {
        f();
      }
    };
    req.send(null);
  }

  liberator.commands.addUserCommand(
    ['gkanji', 'googlekanji'],
    'Google kanji',
    getKanji
  );

  function copyToClipboard (copytext) {
    const supstr = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
    const trans  = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
    const clipid = Ci.nsIClipboard;
    const clip   = Cc["@mozilla.org/widget/clipboard;1"].getService(clipid);

    supstr.data = copytext;
    trans.addDataFlavor("text/unicode");
    trans.setTransferData("text/unicode", supstr, copytext.length * 2);

    clip.setData(trans, null, clipid.kGlobalClipboard);
  }

  liberator.commands.addUserCommand(
    ['gkcopy'],
    'Google kanji',
    copyToClipboard,
    { completer: function (args) [0, copycompl] }
  );


} catch (e) { liberator.log(e) } })();