aboutsummaryrefslogtreecommitdiffstats
path: root/opener.js
blob: 49ffed766e0086b5818ed8c0572284cfcf19d497 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// PLUGIN_INFO {{{
let PLUGIN_INFO = xml`
<VimperatorPlugin>
  <name>opener</name>
  <name lang="ja">opener</name>
  <description> --- </description>
  <description lang="ja">URL 移動時にそのURLが既に開かれていたら、そのタブに移動する</description>
  <version>1.0.0</version>
  <author homepage="http://vimperator.g.hatena.ne.jp/voidy21/">voidy21</author>
  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
  <updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/opener.js</updateURL>
  <minVersion>2.3</minVersion>
  <maxVersion>2.3</maxVersion>
  <detail><![CDATA[
    URL 移動時にそのURLが既に開かれていたら、そのタブに移動する
  ]]></detail>
  <detail lang="ja"><![CDATA[
    URL 移動時にそのURLが既に開かれていたら、そのタブに移動する
  ]]></detail>
</VimperatorPlugin>`;
// }}}
// INFO {{{
let INFO = xml`
  <plugin name="opener" version="1.0.0"
          href="http://github.com/vimpr/vimperator-plugins/blob/master/opener.js"
          summary="URL 移動時にそのURLが既に開かれていたら、そのタブに移動する"
          xmlns="http://vimperator.org/namespaces/liberator">
    <author>voidy21</author>
    <author email="anekos@snca.net">anekos</author>
    <project name="Vimperator" minVersion="2.3"/>
    <p>URL 移動時にそのURLが既に開かれていたら、そのタブに移動する</p>
  </plugin>
`;
// }}}

/*
 * Original version by voidy21:
 *  http://vimperator.g.hatena.ne.jp/voidy21/20100119/1263907211
 *  http://vimperator.g.hatena.ne.jp/voidy21/20100127/1264542669
 */

(function () {
  let U = liberator.plugins.libly.$U;

  function jump (_url) {
    let index = 0;
    let url = util.stringToURLArray(_url).toString();
    if (url == buffer.URL){
      return false;
    }
    for each ( [,tab] in tabs.browsers ) {
      if (url == tab.currentURI.spec){
        tabs.select(index, false, true);
        return true;
      }
      ++index;
    }
    return false;
  }

  "open tabopen edit".split(/\s/).forEach(
    function (name) {
      let command = commands.get(name);
      if (!command)
        return;
      U.around(
        command,
        "action",
        function (next, args) {
          let url = args[0].string;
          if (!(url && jump(url)))
            return next();
        }
      );
    }
  );

  //buffer.followLink()を変更
  //hint-a-hint時[f,F]に対象のタブが既に開いてあったらjump
  let (ignore = false) {
    let ignoreBlock = function (block) {
      ignore = true;
      let result = block();
      ignore = false;
      return result;
    };

    U.around(
      buffer,
      "followLink",
      function (next, args) {
        return ignoreBlock(function () {
          let [elem,] = args;
          let url = elem.href;
          if (!(url && jump(url))){
            liberator.echo("Now Loading... " + url);
            return next();
          }
        });
      }
    );

    document.addEventListener(
      'click',
      function (event) {
        if (ignore)
          return;
        let e = event.target;
        if (e && e.tagName.match(/^a$/i) && e.href && jump(e.href)) {
          event.preventDefault();
          event.stopPropagation();
        }
      },
      true
    );
  }

  U.around(
    quickmarks,
    'jumpTo',
    function (next, args) {
      let qmark = args[0];
      let url = quickmarks._qmarks.get(qmark);
      if (!(url && jump(url))) {
        return next();
      }
    }
  );

})();