aboutsummaryrefslogtreecommitdiffstats
path: root/caret-hint.js
blob: 4f2a20dd94dd1294b1edcfc01903cd2706196709 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* NEW BSD LICENSE {{{
Copyright (c) 2009-2010, anekos.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. The names of the authors may not be used to endorse or promote products
       derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.


###################################################################################
# http://sourceforge.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license       #
# に参考になる日本語訳がありますが、有効なのは上記英文となります。                #
###################################################################################

}}} */

// PLUGIN_INFO {{{
let PLUGIN_INFO =
<VimperatorPlugin>
  <name>Caret Hint</name>
  <description>Move caret position by hint</description>
  <description lang="ja">Hint を使ってキャレット位置を移動</description>
  <version>1.3.1</version>
  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
  <license>new BSD License (Please read the source code comments of this plugin)</license>
  <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/caret-hint.js</updateURL>
  <minVersion>2.0pre</minVersion>
  <maxVersion>2.3</maxVersion>
  <detail><![CDATA[
    Move caret position by hint.
    == Global Variables ==
      let g:caret_hint_key = 'c':
        Hint mode key.
        Move caret position to the head of selected element.
      let g:caret_hint_tail_key = 'C':
        Hint mode key.
        Move caret position to the tail of selected element.
      let g:caret_hint_select_key = '' (default: disabled):
        Hint mode key.
        Move caret position to the head of selected element, and select.
      let g:caret_hint_select_tail_key = 'S':
        Hint mode key.
        Move caret position to the tail of selected element, and select.
      let g:caret_hint_swap_key = 's':
        The key mapping for Visual-mode.
        Swap caret position head to tail.
      If apply empty string ('') to these variables, these mapping or mode are not enabled.
    == Global Variables 2 ==
      let g:caret_hint_xpath = '//*':
        The XPath for hint-mode selection.
  ]]></detail>
  <detail lang="ja"><![CDATA[
    Hint を使ってキャレット位置を移動
    == Global Variables 1 ==
      let g:caret_hint_key = 'c':
        Hint モードのキー
        選択した要素の先頭にキャレットを移動する
      let g:caret_hint_tail_key = 'C':
        Hint モードのキー
        選択した要素の後尾にキャレットを移動する
      let g:caret_hint_select_key = '' (デフォルト: 無効):
        Hint モードのキー
        選択した要素の先頭にキャレットを移動し要素を選択する
      let g:caret_hint_select_tail_key = 'S':
        Hint モードのキー
        選択した要素の後尾にキャレットを移動し要素を選択する
      let g:caret_hint_swap_key = 's':
        VISUAL モード用のキーマッピング
        キャレットの位置を交換する(先頭 <=> 後尾)
      これらの値に空文字列を与えればマッピングやモードは有効にされません
    == Global Variables 2 ==
      let g:caret_hint_xpath = '//*':
        ヒント対象要素を選択するための XPath
  ]]></detail>
</VimperatorPlugin>;
// }}}

/*       _\|/_
         (o o)
 +----oOO-{_}-OOo------------+
 |TODO count@action の使い道 |
 |     要素 A-B 間を選択     |
 +---------------------------*/


(function () {

  // XXX 空白も有効
  let headMode = gval('caret_hint_key', 'c');
  let tailMode = gval('caret_hint_tail_key', 'C');
  let selectHeadMode = gval('caret_hint_select_key', '');
  let selectTailMode = gval('caret_hint_select_tail_key', 'S');
  let swapKey = gval('caret_hint_swap_key', 's');
  let extendLeader = gval('extend_leader', 'c');
  let hintXPath = liberator.globalVariables.caret_hint_xpath || '//*';

  let extendMode = false;

  [headMode, tailMode, selectHeadMode, selectTailMode].forEach(
    function(mode) {
      let map = extendLeader + ';' + mode;
      if (!mode)
        return;
      mappings.remove(modes.NORMAL, map); // for debug
      mappings.remove(modes.VISUAL, map); // for debug
      mappings.addUserMap(
        [modes.NORMAL, modes.VISUAL],
        [map],
        'desc',
        function () {
          extendMode = true;
          hints.show(mode);
        },
        {
        }
      );
    }
  );

  [
    [[true,  false], headMode],
    [[false, false], tailMode],
    [[true,  true ], selectHeadMode],
    [[false, true ], selectTailMode],
  ].forEach(function ([[h, s], m, d]) {
    if (!m)
      return;
    hints.addMode(
      m,
      'Move caret position to ' + (h ? 'head' : 'tail') + (s ? ' and Select' : ''),
      function (elem, loc, count) {
        moveCaret(elem, h, s);
        extendMode = false;
      },
      function () hintXPath
    );
  });

  if (swapKey) {
    mappings.addUserMap(
      [modes.VISUAL],
      [swapKey],
      'Swap caret position head to tail',
      swapCaret,
      {}
    );
  }


  function gval (name, def) {
    let v = liberator.globalVariables[name];
    return (v === undefined) ? def : v;
  }

  function swapCaret () {
    let win = new XPCNativeWrapper(window.content.window);
    let s = win.getSelection();

    if (s.rangeCount <= 0)
      return false;

    // 位置交換時に元の情報が失われるので保存しておく
    let [a, f] = [[s.anchorNode, s.anchorOffset], [s.focusNode, s.focusOffset]];
    s.collapse.apply(s, f);
    s.extend.apply(s, a);
  }

  function moveCaret (elem, head, select) {
    let doc = elem.ownerDocument;
    let win = new XPCNativeWrapper(window.content.window);
    let sel =  win.getSelection();
    let r = doc.createRange();

    sel.removeAllRanges();
    r.selectNodeContents(elem);

    if (select) {
      mappings.getDefault(modes.NORMAL, 'i').action();
      mappings.getDefault(modes.CARET, 'v').action();
    } else {
      if (head) {
        r.setEnd(r.startContainer, r.startOffset);
      } else {
        r.setStart(r.endContainer, r.endOffset);
      }
      mappings.getDefault(modes.NORMAL, 'i').action();
    }

    if (extendMode) {
      let a = sel.getRangeAt(0);
      if (r.compareBoundaryPoints(Range.END_TO_START, a) < 0) {
        r.setEnd(a.endContainer, a.endOffset);
      } else {
        r.setStart(a.startContainer, a.startOffset);
      }
    }

    sel.addRange(r);

    if (select && head)
      swapCaret();

  }
})();

// vim:sw=2 ts=2 et si fdm=marker:
pan class="p">, opts.url, true, opts.user || null, opts.password || null); if (opts.headers) { for (var k in opts.headers) if (opts.headers.hasOwnProperty(k)) { req.setRequestHeader(k, opts.headers[k]); } } req.onreadystatechange = function () { if (req.readyState == 4) d.call(req); }; req.send(opts.data || null); d.xhr = req; return d; } http.get = function (url) http({method:"get", url:url}); http.post = function (url, data) http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}); Deferred.Deferred = Deferred; Deferred.http = http; function WSSEUtils(aUserName, aPassword){ this._init(aUserName, aPassword); } WSSEUtils.prototype = { get userName() this._userName, get noce() this._nonce, get created() this._created, get passwordDigest() this._passwordDigest, getWSSEHeader: function(){ var result = [ 'UsernameToken Username="' + this._userName + '", ', 'PasswordDigest="' + this._passwordDigest + '=", ', 'Nonce="' + this._nonce + '", ', 'Created="' + this._created + '"' ].join(""); return result; }, _init: function(aUserName, aPassword){ var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator); var seed = (new Date()).toUTCString() + uuidGenerator.generateUUID().toString(); this._userName = aUserName; this._nonce = this._getSha1Digest(seed, true); this._created = this._getISO8601String((new Date())); this._passwordDigest = this._getSha1Digest(this._getSha1Digest(seed, false) + this._created + aPassword, true); }, _getSha1Digest: function(aString, aBase64){ var cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash); cryptoHash.init(Ci.nsICryptoHash.SHA1); var inputStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream); inputStream.setData(aString, aString.length); cryptoHash.updateFromStream(inputStream, -1); return cryptoHash.finish(aBase64); }, _getISO8601String: function(aDate){ var result = [ zeropad(aDate.getUTCFullYear(), 4), "-", zeropad(aDate.getUTCMonth() + 1, 2), "-", zeropad(aDate.getUTCDate(), 2), "T", zeropad(aDate.getUTCHours(), 2), ":", zeropad(aDate.getUTCMinutes(), 2), ":", zeropad(aDate.getUTCSeconds(), 2), "Z" ].join(""); return result; function zeropad(s, l){ s = String(s); while(s.length < l){ s = "0" + s; } return s; } } }; // copied from AutoPagerize (c) id:swdyh function getElementsByXPath(xpath, node){ node = node || document; var nodesSnapshot = (node.ownerDocument || node).evaluate(xpath, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); var data = []; for(var i = 0, l = nodesSnapshot.snapshotLength; i < l; data.push(nodesSnapshot.snapshotItem(i++))); return (data.length > 0) ? data : null; } function getFirstElementByXPath(xpath, node){ node = node || document; var result = (node.ownerDocument || node).evaluate(xpath, node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); return result.singleNodeValue ? result.singleNodeValue : null; } // copied from Pagerization (c) id:ofk function parseHTML(str, ignoreTags) { var exp = "^[\\s\\S]*?<html(?:\\s[^>]*)?>|</html\\s*>[\\S\\s]*$"; if (ignoreTags) { if (typeof ignoreTags == "string") ignoreTags = [ignoreTags]; var stripTags = []; ignoreTags = ignoreTags.filter(function(tag) tag[tag.length - 1] == "/" || !stripTags.push(tag)) .map(function(tag) tag.replace(/\/$/, "")); if (stripTags.length > 0) { stripTags = stripTags.length > 1 ? "(?:" + stripTags.join("|") + ")" : String(stripTags); exp += "|<" + stripTags + "(?:\\s[^>]*|/)?>|</" + stripTags + "\\s*>"; } } str = str.replace(new RegExp(exp, "ig"), ""); var res = document.implementation.createDocument(null, "html", null); var range = document.createRange(); range.setStartAfter(window.content.document.body); res.documentElement.appendChild(res.importNode(range.createContextualFragment(str), true)); if (ignoreTags) ignoreTags.forEach(function(tag) { var elements = res.getElementsByTagName(tag); for (var i = elements.length, el; el = elements.item(--i); el.parentNode.removeChild(el)); }); return res; } // // // // function getNormalizedPermalink(url){ var xhr = new XMLHttpRequest(); xhr.open("GET","http://api.pathtraq.com/normalize_url?url=" + url,false); xhr.send(null); if(xhr.status != 200){ liberator.echoerr("Pathtraq: FAILED to normalize URL!!"); return undefined; } return xhr.responseText; } function getUserAccount(form,post,arg){ var user, password; try{ var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); var logins = passwordManager.findLogins({}, form, post, arg); if(logins.length > 0){ [user, password] = [logins[0].username, logins[0].password]; } else { var promptUser = { value : this.loginPrompt.user }, promptPass = { value : this.loginPrompt.password }; var promptSvc = Cc["@mozilla.org/embedcomp/prompt-service;1"] .getService(Ci.nsIPromptService); var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init"); var ret = promptSvc.promptUsernameAndPassword( window, form, this.loginPrompt.description, promptUser, promptPass, null, {} ); if(ret){ [user, password] = [promptUser.value, promptPass.value]; var formLoginInfo = new nsLoginInfo(form, post, null, user, password, '', ''); passwordManager.addLogin(formLoginInfo); } else { liberator.echoerr("Direct Social Bookmark: account not found - " + form); } } } catch(ex){ liberator.echoerr("Direct Social Bookmark: handled exception during tag extracting"); liberator.log(ex); } return [user, password]; } // // // // var services = { 'h': { description:'Hatena bookmark', account:['https://www.hatena.ne.jp', 'https://www.hatena.ne.jp', null], loginPrompt:{ user:'', password:'', description:'Enter username and password.' }, entryPage:'http://b.hatena.ne.jp/entry/%URL%', poster:function(user,password,url,title,comment,tags){ var tagString = tags.length > 0 ? '[' + tags.join('][') + ']' : ""; var request = <entry xmlns="http://purl.org/atom/ns#"> <title>dummy</title> <link rel="related" type="text/html" href={url}/> <summary type="text/plain">{tagString + comment}</summary> </entry>; var wsse = new WSSEUtils(user,password); return Deferred.http({ method: "post", url: "http://b.hatena.ne.jp/atom/post", data: request.toString(), headers: { "X-WSSE": wsse.getWSSEHeader(), "Content-Type": "application/atom+xml", }, }).next(function(xhr){ if(xhr.status != 201) throw "Hatena Bookmark: faild"; }); }, tags:function(user,password){ var xhr = new XMLHttpRequest(); var hatena_tags = []; //xhr.open("GET","http://b.hatena.ne.jp/my",false); xhr.open("GET","http://b.hatena.ne.jp/"+user,false); xhr.send(null); var mypage_html = parseHTML(xhr.responseText, ['img', 'script']); //var tags = getElementsByXPath("//ul[@id=\"taglist\"]/li/a",mypage_html); var tags = getElementsByXPath('id("tags")/li/a', mypage_html); tags.forEach(function(tag){ hatena_tags.push(tag.innerHTML); }); liberator.echo("Hatena Bookmark: Tag parsing is finished. Taglist length: " + tags.length); return hatena_tags; }, icon:function(url){ return '<img src="http://b.hatena.ne.jp/entry/image/' + url + '" style="vertical-align: middle;" />'; }, }, 'd': { description:'del.icio.us', account:['https://secure.delicious.com', 'https://secure.delicious.com', null], loginPrompt:{ user:'', password:'', description:'Enter username and password.' }, entryPage:'http://del.icio.us/url/%URL::MD5%', poster:function(user,password,url,title,comment,tags){ var request_url = 'https://api.del.icio.us/v1/posts/add?' + [ ['url', url], ['description', title], ['extended', comment], ['tags', tags.join(' ')] ].map(function(p) p[0] + '=' + encodeURIComponent(p[1])).join('&'); return Deferred.http({ method: "get", url: request_url, user: user, password: password, }).next(function(xhr){ if(xhr.status != 200) throw "del.icio.us: faild"; }); }, tags:function(user,password){ const feed_url = 'http://feeds.delicious.com/feeds/json/tags/'; var returnValue = []; var xhr = new XMLHttpRequest(); xhr.open("GET", feed_url + user + "?raw", false, user, password); xhr.send(null); var tags = evalFunc("(" + xhr.responseText + ")"); for(var tag in tags) returnValue.push(tag); liberator.echo("del.icio.us: Tag parsing is finished. Taglist length: " + returnValue.length); return returnValue; }, icon:function(url){ var url = liberator.modules.buffer.URL; var cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash); cryptoHash.init(Ci.nsICryptoHash.MD5); var inputStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream); inputStream.setData(url, url.length); cryptoHash.updateFromStream(inputStream, -1); var hash = cryptoHash.finish(false), ascii = []; const hexchars = '0123456789ABCDEF'; var hexrep = new Array(hash.length * 2); for(var i = 0; i < hash.length; i++) { ascii[i * 2] = hexchars.charAt((hash.charCodeAt(i) >> 4) & 0xF); ascii[i * 2 + 1] = hexchars.charAt(hash.charCodeAt(i) & 0xF); } return '<img src="http://del.icio.us/feeds/img/savedcount/' + ascii.join('').toLowerCase() + '?aggregate" style="vertical-align: middle;" />'; }, }, 'l': { description:'livedoor clip', account:['http://api.clip.livedoor.com', 'http://api.clip.livedoor.com', null], loginPrompt:{ user:'', password:'apikey', description:'Enter username and apikey.\nyou can get "api-key" from\n\thttp://clip.livedoor.com/config/api' }, entryPage:'http://clip.livedoor.com/page/%URL%', poster:function(user,password,url,title,comment,tags){ var rate=0; var starFullRate=5; if(comment.match(/\*+$/)){ comment = RegExp.leftContext; rate = (RegExp.lastMatch.length > starFullRate)? starFullRate : RegExp.lastMatch.length; } var request_url = 'http://api.clip.livedoor.com/v1/posts/add?' + [ ['url', url], ['description', title], ['extended', comment], ['rate', rate], ['tags', tags.join(' ')] ].map(function(p) p[0] + '=' + encodeURIComponent(p[1])).join('&'); return Deferred.http({ method: "get", url: request_url, user: user, password: password, }).next(function(xhr){ if(xhr.status != 200) throw "livedoor clip: faild"; }); }, tags:function(user,password){ var xhr = new XMLHttpRequest(); var ldc_tags = []; xhr.open("GET","http://clip.livedoor.com/clip/add?link=http://example.example/",false); xhr.send(null); var mypage_html = parseHTML(xhr.responseText, ['img', 'script']); var tags = getElementsByXPath("id(\"tag_list\")/span",mypage_html); tags.forEach(function(tag){ ldc_tags.push(tag.textContent); }); liberator.echo("livedoor clip: Tag parsing is finished. Taglist length: " + tags.length); return ldc_tags; }, icon:function(url){ return '<img src="http://image.clip.livedoor.com/counter/' + url + '" style="vertical-align: middle;" />'; }, }, 'g': { description:'Google Bookmarks', account:null, loginPrompt:null, entryPage:'%URL%', poster:function(user,password,url,title,comment,tags){ var request_url = 'http://www.google.com/bookmarks/mark'; var params = [ ['bkmk', url], ['title', title], ['labels', tags.join(',')] ].map(function(p) p[0] + '=' + encodeURIComponent(p[1])).join('&'); return Deferred.http({ method: "post", url: request_url, data: params, headers: { "User-Agent": navigator.userAgent + " GoogleToolbarFF 3.0.20070525", }, }).next(function(xhr){ if(xhr.status != 200) throw "Google Bookmarks: faild"; }); }, tags:function(user,password) [], }, 'f': { description:'foves', account:['https://secure.faves.com', 'https://secure.faves.com', null], loginPrompt:{ user:'', password:'', description:'Enter username and password.' }, entryPage:'%URL%', poster:function(user,password,url,title,comment,tags){ var request_url = 'https://secure.faves.com/v1/posts/add?' + [ ['url', url], ['description', title], ['extended', comment], ['tags', tags.join(' ')] ].map(function(p) p[0] + '=' + encodeURIComponent(p[1])).join('&'); return Deferred.http({ method: "get", url: request_url, user: user, password: password, }).next(function(xhr){ if(xhr.status != 200) throw "foves: faild"; }); }, tags:function(user,password){ const feed_url = 'https://secure.faves.com/v1/tags/get'; var returnValue = []; var xhr = new XMLHttpRequest(); xhr.open("GET", feed_url, false, user, password); xhr.send(null); var tags = xhr.responseXML.getElementsByTagName('tag'); for(var n = 0; n < tags.length; n++) returnValue.push(tags[n].getAttribute('tag')); liberator.echo("foves: Tag parsing is finished. Taglist length: " + returnValue.length); return returnValue; }, }, 'p': { description:'Places', account:null, loginPrompt:null, entryPage:'%URL%', poster:function(user,password,url,title,comment,tags){ const taggingService = Cc["@mozilla.org/browser/tagging-service;1"].getService(Ci.nsITaggingService); var nsUrl = Cc["@mozilla.org/network/standard-url;1"].createInstance(Ci.nsIURL); nsUrl.spec = url; taggingService.tagURI(nsUrl,tags); try{ Application.bookmarks.tags.addBookmark(title, nsUrl); }catch(e){ throw "Places: faild"; } }, tags:function(user,password) Application.bookmarks.tags.children.map(function(x) x.title), }, }; liberator.plugins.direct_bookmark = { services: services, tags: [] }; function getTags(arg){ var d,first; d = first = Deferred(); useServicesByTag.split(/\s*/).forEach(function(service){ var user, password, currentService = services[service] || null; [user,password] = currentService.account ? getUserAccount.apply(currentService,currentService.account) : ["", ""]; d = d.next(function(t) t.concat(currentService.tags(user,password))); }); d.next(function(tags){liberator.plugins.direct_bookmark.tags = tags.filter(function(e,i,a) a.indexOf(e) == i).sort()}) .error(function(e){liberator.echoerr("direct_bookmark.js: Exception throwed! " + e)}); return first; } liberator.modules.commands.addUserCommand(['btags'],"Update Social Bookmark Tags", function(arg){setTimeout(function(){getTags().call([])},0)}, {}); liberator.modules.commands.addUserCommand(['bentry'],"Goto Bookmark Entry Page", function(service, special){ service = service.string || useServicesByPost.split(/\s*/)[0]; var currentService = services[service] || null; if(!currentService || !currentService.entryPage) { return; } liberator.open(currentService.entryPage .replace(/%URL(?:::(ESC|MD5))?%/g, function(x, t){ if(!t) return liberator.modules.buffer.URL.replace(/#/, '%23'); if(t == "ESC") return encodeURIComponent(liberator.modules.buffer.URL); if(t == "MD5"){ var url = liberator.modules.buffer.URL; var cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash); cryptoHash.init(Ci.nsICryptoHash.MD5); var inputStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream); inputStream.setData(url, url.length); cryptoHash.updateFromStream(inputStream, -1); var hash = cryptoHash.finish(false), ascii = []; const hexchars = '0123456789ABCDEF'; var hexrep = new Array(hash.length * 2); for(var i = 0; i < hash.length; i++) { ascii[i * 2] = hexchars.charAt((hash.charCodeAt(i) >> 4) & 0xF); ascii[i * 2 + 1] = hexchars.charAt(hash.charCodeAt(i) & 0xF); } return ascii.join('').toLowerCase(); } }), special ? liberator.NEW_TAB : liberator.CURRENT_TAB); },{ completer: function(filter) [0, useServicesByPost.split(/\s*/).map(function(p) [p, services[p].description])] } ); liberator.modules.commands.addUserCommand(['bicon'],"Show Bookmark Count as Icon", function(arg){ var url = getNormalizedPermalink(liberator.modules.buffer.URL); var html = useServicesByTag.split(/\s*/).map(function(service){ var currentService = services[service] || null; return (currentService && typeof currentService.icon === 'function') ? (currentService.description + ': ' + currentService.icon(url)) : null; }).join('<br />'); liberator.echo(html, true); }, {}); liberator.modules.commands.addUserCommand(['sbm'],"Post to Social Bookmark", function(arg){ var comment = ""; var targetServices = useServicesByPost; if (arg["-s"]) targetServices = arg["-s"]; if (arg.length > 0) comment = arg.join(" "); var tags = []; var re = /\[([^\]]+)\]([^\[].*)?/g; var d = new Deferred(); var first = d; if(/^\[[^\]]+\]/.test(comment)){ var tag, text; while((tag = re.exec(comment))){ [, tag, text] = tag; tags.push(tag); } comment = text || ''; } var url = liberator.modules.buffer.URL; var title = liberator.modules.buffer.title; targetServices.split(/\s*/).forEach(function(service){ var user, password, currentService = services[service] || null; [user,password] = currentService.account ? getUserAccount.apply(currentService,currentService.account) : ["", ""]; d = d.next(function() currentService.poster( user,password, isNormalize ? getNormalizedPermalink(url) : url,title, comment,tags //)); )).next(function(){ liberator.echo("[" + services[service].description + "] post completed."); }); }); d.error(function(e){liberator.echoerr("direct_bookmark.js: Exception throwed! " + e);liberator.log(e);}); setTimeout(function(){first.call();},0); },{ completer: function(context, arg, special){ let filter = context.filter; var match_result = filter.match(/((?:\[[^\]]*\])*)\[?(.*)/); //[all, commited, now inputting] var m = new RegExp(XMigemoCore && isUseMigemo ? "^(" + XMigemoCore.getRegExp(match_result[2]) + ")" : "^" + match_result[2],'i'); var completionList = []; if(liberator.plugins.direct_bookmark.tags.length == 0) getTags().call([]); context.title = ['Tag','Description']; context.advance( match_result[1].length ); context.completions = [["[" + tag + "]","Tag"] for each (tag in liberator.plugins.direct_bookmark.tags) if (m.test(tag) && match_result[1].indexOf('[' + tag + ']') < 0)]; }, options: [ [['-s','-service'], liberator.modules.commands.OPTION_STRING], ] } ); })(); // vim:sw=4 ts=4 et: