root/lang/javascript/vimperator-plugins/branches/2.1/garbage_finder.js

Revision 33036, 5.5 kB (checked in by anekos, 16 months ago)

クリップボードにコピーも出来るようにした

Line 
1/* {{{
2Copyright (c) 2008-2009, anekos.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8    1. Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10    2. Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13    3. The names of the authors may not be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25THE POSSIBILITY OF SUCH DAMAGE.
26
27
28###################################################################################
29# http://sourceforge.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license       #
30# に参考になる日本語訳がありますが、有効なのは上記英文となります。                #
31###################################################################################
32
33}}} */
34
35// PLUGIN_INFO {{{
36let PLUGIN_INFO =
37<VimperatorPlugin>
38  <name>Garbage Finder</name>
39  <name lang="ja">ゴミ探し</name>
40  <description>Find the taints in global(window object)</description>
41  <description lang="ja">グローバル(window オブジェクト)の汚染を調べる</description>
42  <version>1.0.2</version>
43  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
44  <license>new BSD License (Please read the source code comments of this plugin)</license>
45  <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
46  <minVersion>2.0pre</minVersion>
47  <maxVersion>2.0pre</maxVersion>
48  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/garbage_finder.js</updateURL>
49  <detail><![CDATA[
50    == Commands ==
51      :garbages:
52        Displays (removed|appended) variables.
53  ]]></detail>
54  <detail lang="ja"><![CDATA[
55    前回(Firefox)起動時の window オブジェクトにおける変数と、現在のそれの差分を取ります。
56    インストールした拡張が、グローバルを汚していないか調べるのに便利かもしれません。
57    == Commands ==
58      :garbages:
59        (追加|削除)された変数を表示。
60  ]]></detail>
61</VimperatorPlugin>;
62// }}}
63
64(function () {
65
66  const STORAGE_NAME = 'plugin-garbage-finder';
67  const IGNORES = (let (gv = liberator.globalVariables.garbage_finder_ignore)
68                      gv === undefined ? 'DownloadUtils PluralForm' : gv).split(/\s+/);
69
70  function Somali (n)
71    let(V,[l,s,j,t]=liberator.eval('[loadPref,savePref,json,Object]',storage.newObject))
72      ({load:function(d)let(v=l(n,true,t))(V=v?v.value:d),
73        save:function(v)s({store:true,name:n,serial:j.encode({value:v===undefined?V:v})})});
74
75  function vars () {
76    let result = [];
77    for (let name in window)
78      result.push(name);
79    return result.sort();
80  }
81
82  function has (ary, v)
83    ary.some(function (it) v == it);
84
85  function diff (oldList, newList) {
86    function sub (n, o)
87      n.filter(function (it) !has(o, it));
88    return {
89      appended: ignore(sub(newList, oldList)),
90      removed: ignore(sub(oldList, newList))
91    };
92  }
93
94  function id (value)
95    value;
96
97  function ignore (ary)
98    ary.filter(function (it) !has(IGNORES, it));
99
100
101  let store = new Somali(STORAGE_NAME);
102  let prevVars = store.load(vars());
103
104  function save ()
105    store.save(vars());
106
107  autocommands.add(
108    'VimperatorEnter',
109    /.*/,
110    function () setTimeout(save, 2000)
111  );
112
113  commands.addUserCommand(
114    ['garbages'],
115    'Display garbages',
116    function (args) {
117      function makeLI (list) {
118        if (list.length) {
119          let result = <></>;
120          list.forEach(function (it) (result += <li>{it}</li>));
121          return <ol>{result}</ol>;
122        }
123        // XXX 駄目くさいけどめんどくさいので…
124        return <ol>Nothing</ol>;
125      }
126
127      if (args.bang) {
128        save();
129        liberator.echo('Current variables was saved.');
130      } else {
131        let gs = diff(prevVars, vars());
132        let as = makeLI(gs.appended), rs = makeLI(gs.removed);
133        let output = <div><h1>Appended</h1><div>{as}</div><h1>Removed</h1><div>{rs}</div></div>;
134        if (args['-clipboard']) {
135          let cbOut = '';
136          function pushLine (v, i)
137            cbOut += '  ' + i + '.' + v + '\n';
138          cbOut += 'Appended';
139          gs.appended.forEach(pushLine);
140          cbOut += 'Removed';
141          gs.removed.forEach(pushLine);
142          util.copyToClipboard(cbOut);
143        }
144        liberator.echo(output);
145      }
146    },
147    {
148      bang: true,
149      options: [ [['-clipboard', '-c'], commands.OPTION_NOARG] ]
150    },
151    true
152  );
153
154})();
155
156// vim:sw=2 ts=2 et si fdm=marker:
Note: See TracBrowser for help on using the browser.