// // pino.js - Open livedoor Reader (and clone server) pinned items - // // LICENSE: {{{ // // This software distributable under the terms of an MIT-style license. // // Copyright (c) 2009 snaka // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // // OSI page : http://opensource.org/licenses/mit-license.php // Japanese : http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license // // }}} // PLUGIN INFO: {{{ var PLUGIN_INFO = {NAME} Open livedoor Reader pinned items livedoor Reader でピンを立てたページを開く 3.0 https://github.com/vimpr/vimperator-plugins/raw/master/pino.js _libly.js snaka MIT style license 1.4.1 || { created_on : (create date), link : (url), title : (page title) } ||< plugins.pino.shift(): Return first item and remove pin. plugins.pino.remove(link): Remove pin from item that matched by 'link'. ]]> で先頭のn件(デフォルト5件、グローバル変数で調整可能) をバックグラウンドのタブで開きます。 で補完候補の一覧にピンを立てた記事の一覧から選択することもできます。 count を指定すると、その件数だけ開きます。 以下のオプションが指定可能です。 -list: ピンの一覧を表示します。 == グローバル変数 == g:pinoOpenItemsCount: 一度に開くピンの数 default: 5 g:pinoOpenBehavior: ピンを開くときの挙動、liberator.open()の第2引数として使用する値 参考)http://wiki.livedoor.jp/shin_yan/d/liberator%282%2e0%29#content_34 default: liberator.NEW_BACKGROUND_TAB g:pinoAscendingOrder: ピンの一覧の表示順を昇順(古い順)とするかどうか default: "false" (新しい順) g:pinoBaseURL: fastladder を使う場合は、この変数を "http://fastladder.com" とする。 default: "http://reader.livedoor.com" g:pinoOpenInterval: タブを開く間隔(ミリ秒) == API == plugins.pino.items(): ピンの一覧を配列で取得する。 ピンのデータ構造は以下のとおりとなっている。 >|| { created_on : (create date), link : (url), title : (page title) } ||< plugins.pino.shift(): 先頭のピンを取得して、そのピンを一覧から削除する。 plugins.pino.remove(link): linkに該当するピンを一覧から削除する。 ]]> ; // }}} let self = liberator.plugins.pino = (function() { // COMMAND /////////////////////////////////////////////////////// {{{ commands.addUserCommand( ["pinneditemopen", "pino"], "Open livedoor Reader(and clone server) pinned item", function(args) { let pins = new Pins(); let items = pins.items(); if (!items || items.length == 0) { liberator.echo("Pinned item doesn't exists."); return; } if (args["-list"]) { //let items = pins.items(); let list =
{items.length} items.
; liberator.echo(list, commandline.FORCE_MULTILINE); return; } if (args.string == "") { let pin; let max = (args.count >= 1) ? args.count : openItemsCount(); for(let i = 0; i < max; i++) { if (!(pin = pins.shift())) break; setTimeout(function(link) liberator.open(link, openBehavior()), openInterval() * i, pin.link); } } else { liberator.open(args.string, openBehavior()); pins.remove(args.string); } }, { literal: 0, count: true, completer: function(context) { var pins = new Pins(); context.title = ["url", "title"]; context.filters = [CompletionContext.Filter.textDescription]; context.anchored = false; context.completions = [ [i.link, i.title] for each (i in pins.items()) ]; }, options: [ [["-list", "-l"], commands.OPTION_NOARG] ] }, true // for Debug ); // }}} // GLOBAL VARIABLES ////////////////////////////////////////////// {{{ var gv = liberator.globalVariables; function openItemsCount() gv.pinoOpenItemsCount || 5; function ascending() window.eval(gv.pinoAscendingOrder) == true; // default: false function openBehavior() window.eval(gv.pinoOpenBehavior) || liberator.NEW_BACKGROUND_TAB; function baseURL() gv.pinoBaseURL || "http://reader.livedoor.com"; function openInterval() gv.pinoOpenInterval || 200; // }}} // CLASS ///////////////////////////////////////////////////////// {{{ function Pins() { this.cache = null; this.apiKey = getLDRApiKey(); this.sortOrder = ascending() ? function(a, b) (a.created_on < b.created_on ? -1 : 1) : function(a, b) (a.created_on > b.created_on ? -1 : 1); } Pins.prototype = { items : function() { let result = this.cache ? this.cache : this.cache = this._getPinnedItems(); return (result || []).sort(this.sortOrder); }, shift : function() { if (this.items().length == 0) return null; var pin = this.items().shift(); this.remove(pin.link); return pin; }, remove : function(link) { var unescapedLink = unescapeHTML(link); var request = new libly.Request( baseURL() + "/api/pin/remove", { //Cookie: "reader_sid=" + this.apiKey, //Referer: "http://reader.livedoor.com/reader/" }, { postBody: toQuery({link: unescapedLink, ApiKey: this.apiKey}) } ); request.addEventListener("onSuccess", function(data) { liberator.log("Removed pin from '" + link + "' was succeeded."); }); request.addEventListener("onFailure", function(data) { liberator.echoerr("Cannot remove pin"); }); request.post(); }, _getPinnedItems : function() { var result = null; var request = new libly.Request( baseURL() + "/api/pin/all", null, { asynchronous: false, postBody: toQuery({ApiKey: this.apiKey}) } ); request.addEventListener("onSuccess", function(data) { if (isLoginPage(data)) { liberator.echoerr("Can't get pinned list. Maybe you should login to livedoor."); return; } result = unentifyObjectValues(liberator.eval(data.responseText)); }); request.addEventListener("onFailure", function(data) { liberator.echoerr("Can't get pinned list!!!"); }); request.post(); return result; }, } // }}} // FUNCTIONS ///////////////////////////////////////////////////// {{{ var libly = plugins.libly; function getLDRApiKey() { var ioService = Cc["@mozilla.org/network/io-service;1"] .getService(Ci.nsIIOService); var uri = ioService.newURI(baseURL(), null, null); var channel = ioService.newChannelFromURI(uri); var cookie = Cc["@mozilla.org/cookieService;1"] .getService(Ci.nsICookieService) .getCookieString(uri, channel); var apiKey = cookie.match(/reader_sid=([^;]+)/); return apiKey ? apiKey[1]: null; } function unescapeHTML(source) { var result = source; [ [/</g, "<"], [/>/g, ">"], [/&/g, "&"] ].forEach( function(rule) { result = result.replace(rule[0], rule[1]); }); return result; } function toQuery(source) [encodeURIComponent(i) + "=" + encodeURIComponent(source[i]) for (i in source) ].join('&'); function isLoginPage(response) response.responseText.substr(0, 5) == ' 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>X-Hint</name> <name lang="ja">X-Hint</name> <description>Show the hints with given XPath.</description> <description lang="ja">指定のXPathでヒントを表示する</description> <version>1.1.2</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>https://github.com/vimpr/vimperator-plugins/raw/master/x-hint.js</updateURL> <minVersion>2.3</minVersion> <maxVersion>2.3</maxVersion> <detail><![CDATA[ :xh[int] <Hint-Mode> <XPath>: Show the <Hint-Mode> hints with <XPath> ]]></detail> <detail lang="ja"><![CDATA[ :xh[int] <Hint-Mode> <XPath>: <XPath> <Hint-Mode> ヒントを表示 ]]></detail> </VimperatorPlugin>; // }}} // INFO {{{ let INFO = <> <plugin name="X-Hint" version="1.1.3" href="http://github.com/vimpr/vimperator-plugins/blob/master/x-hint.js" summary="Show the hints with given XPath." lang="en-US" xmlns="http://vimperator.org/namespaces/liberator"> <author email="anekos@snca.net">anekos</author> <license>New BSD License</license> <project name="Vimperator" minVersion="2.3"/> <p>Show the hints with given XPath.</p> <item> <tags>:xhint</tags> <tags>:xh</tags> <spec>:xh<oa>int</oa> <a>HintMode</a> <a>XPath</a></spec> <description> <p> Show the <a>HintMode</a> hints with <a>XPath</a> </p> </description> </item> <item> <tags>:xhintdo</tags> <tags>:xhdo</tags> <spec>:xhintdo <a>XPath</a> <a>javascript</a></spec> <description> <p> Show the hints with <a>XPath</a>. And do <a>javascript</a> code. This command gives the variable "elem" to the context of <a>javascript</a>. </p> </description> </item> </plugin> <plugin name="X-Hint" version="1.1.3" href="http://github.com/vimpr/vimperator-plugins/blob/master/x-hint.js" summary="Show the hints with given XPath." lang="ja" xmlns="http://vimperator.org/namespaces/liberator"> <author email="anekos@snca.net">anekos</author> <license>New BSD License</license> <project name="Vimperator" minVersion="2.3"/> <p>Show the hints with given XPath.</p> <item> <tags>:xhint</tags> <tags>:xh</tags> <spec>:xh<oa>int</oa> <a>HintMode</a> <a>XPath</a></spec> <description> <p> <a>XPath</a> で <a>HintMode</a> ヒントを表示する。 </p> </description> </item> <item> <tags>:xhintdo</tags> <tags>:xhdo</tags> <spec>:xhintdo <a>XPath</a> <a>javascript</a></spec> <description> <p> <a>XPath</a> でヒントを出し、<a>javascript</a> コードを実行します。 <a>javascript</a> の context には 変数 "elem" が与えられます。 </p> </description> </item> </plugin> </>; // }}} (function () { const description = 'Show the hint with given xpath.'; let last = {}; function xpath () (last.xpath || '//a') function restore () { if (last.hintMode) last.hintMode.tags = last.hintTags; last = {}; } plugins.libly.$U.around( hints, 'show', function (next, [minor, filter, win]) { if (last.xpath) { // save last.hintMode = this._hintModes[minor]; last.hintTags = last.hintMode.tags; // override last.hintMode.tags = xpath; } try { return next(); } catch (e) { restore(); liberator.log('x-hint: restore tags for error'); liberator.log(e); } }, true ); plugins.libly.$U.around( hints, 'hide', function (next, [minor, filter, win]) { restore(); return next(); }, true ); function showHintsWith (mode, xpath) { last.xpath = xpath; hints.show(mode); } __context__.show = showHintsWith; commands.addUserCommand( ['xh[int]'], description + '(:xhint <mode> <xpath>)', function (args) { showHintsWith(args[0], args.literalArg); }, { literal: 1 }, true ); let (hintModeText = 'x-hint-do', js = null) { hints.addMode( hintModeText, 'X-Hint DO', function (elem) { let context = {__proto__: modules.userContext, elem: elem}; try { liberator.eval(js, context); } catch (e) { liberator.echoerr(e); } } ); commands.addUserCommand( ['xhintdo', 'xhdo'], 'Run js-code with X-Hint. (:xhdo <xpath> <javascript>)', function (args) { js = args.literalArg; showHintsWith(hintModeText, args[0]); }, { literal: 1, completer: function (context, args) { if (args.completeArg == 1) completion.javascript(context); } }, true ); } })(); // vim:sw=2 ts=2 et si fdm=marker: