root/lang/javascript/vimperator-plugins/branches/2.0/subscldr.js

Revision 31160, 7.4 kB (checked in by snaka, 18 months ago)

Modify PLUGIN_INFO and some messages.

Line 
1//
2// subscldr.js
3//
4// LICENSE: {{{
5//   Copyright (c) 2009 snaka<snaka.gml@gmail.com>
6//
7//     Distributable under the terms of an MIT-style license.
8//     http://www.opensource.jp/licenses/mit-license.html
9// }}}
10//
11// PLUGIN INFO: {{{
12var PLUGIN_INFO =
13<VimperatorPlugin>
14  <name>{NAME}</name>
15  <description>Adds subscriptions to livedoor Reader/Fastladder in place.</description>
16  <description lang="ja">ページ遷移なしでlivedoor ReaderやFastladderにフィードを登録します。</description>
17  <minVersion>2.0pre</minVersion>
18  <maxVersion>2.0</maxVersion>
19  <require type="plugin">_libly.js</require>
20  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/subscldr.js</updateURL>
21  <author mail="snaka.gml@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/snaka72/">snaka</author>
22  <license>MIT style license</license>
23  <version>0.2</version>
24  <detail><![CDATA[
25    == Subject ==
26    Adds subscriptions to livedoor Reader/Fastladder in place.
27
28    == Commands ==
29    >||
30    :subscldr
31    :subscfl
32    ||<
33
34  ]]></detail>
35
36  <detail lang="ja"><![CDATA[
37    == 概要 ==
38    ページ遷移すること無しにlivedoor ReaderやFastladderへのフィードの登録を行います。
39
40    == コマンド ==
41    >||
42    :subscldr
43    :subscfl
44    ||<
45
46  ]]></detail>
47</VimperatorPlugin>;
48// }}}
49
50liberator.plugins.subscldr = (function() {
51  // PUBLIC {{{
52  var PUBLICS = {
53    // TODO:Provide API function.
54
55    // for DEBUG {{{
56    //getSubscription: getSubscription,
57    //postSubscription: postSubscription,
58    //selectFeed: selectFeed
59    // }}}
60  };
61
62  // }}}
63  // COMMAND {{{
64  addCommand(
65    ["subscldr", "subscrldr"],
66    "livedoor Reader",
67    "http://reader.livedoor.com/subscribe/"
68  );
69
70  addCommand(
71    ["subscfl", "subscrfl"],
72    "Fastladder",
73    "http://fastladder.com/subscribe/"
74  );
75  // }}}
76  // PRIVATE {{{
77  function addCommand (command, servicename, endpoint) {
78
79    function handleFeedRequest(opts, redirectUrl, force) {
80        var subscribeInfo = getSubscription(redirectUrl);
81        var availableLinks = subscribeInfo.feedlinks.filter(function(info) info[1]);
82        var alreadySubscribed = availableLinks.length != subscribeInfo.feedlinks.length;
83
84        if (alreadySubscribed && !force) {
85          liberator.echo("This site has already been subscribed. Are you sure to want to add subscription?");
86          commandline.input("Add? [y/N]:",
87            function(ans) {
88              if (ans.toLowerCase().indexOf("y") == 0) // /^y(?:es)?$/.test(ans.toLowerCase())
89                handleFeedRequest(opts, null, true);
90              else
91                liberator.echo("Canceled.");
92              commandline.close();
93            }
94          );
95          return;
96        }
97
98        switch (availableLinks.length) {
99        case 0:
100          if (alreadySubscribed)
101            liberator.echo("The feed of this site has already been subscribed.");
102          else
103            // Maybe never reach here.
104            liberator.echoerr("SITE FEED NOT AVAILABLE!!!");
105          break;
106        case 1:
107          liberator.log("FEED ONLY ONE!!");
108          subscribeInfo.feedlinks = [availableLinks[0][0], true];
109          postSubscription(subscribeInfo, opts);
110          break;
111        default:
112          liberator.log("SOME FEED AVAILABLE");
113          selectFeed( availableLinks.map(function(i) [i[0], i[2]]),
114            function(sel) {
115              liberator.log("SELECTED FEED:" + sel);
116              liberator.echo("Redirected ...");
117              var redirectUrl = endpoint + "?url=" + encodeURIComponent(sel);
118              handleFeedRequest(opts, redirectUrl);
119            }
120          );
121        }
122    }
123
124    function getSubscription(target) {
125      liberator.echo("Please wait ...");
126      var subscribeInfo;
127
128      var uri = target || endpoint + buffer.URL;
129
130      var req = new libly.Request(uri, null, {asynchronous: false});
131      req.addEventListener("onSuccess", function(res) {
132        liberator.log(res.responseText);
133        res.getHTMLDocument();
134        subscribeInfo = getSubscribeInfo(res.doc);
135        liberator.log(subscribeInfo.toSource());
136      });
137      req.get();
138
139      return subscribeInfo;
140    }
141
142    function postSubscription(info, opts) {
143      liberator.log("subscribe:" + info.toSource());
144
145      var postBody= "url=" + encodeURIComponent(info.target_url) +
146                    "&folder_id=0" +
147                    "&rate=" + (opts.rate || "0") +
148                    "&register=1" +
149                    "&feedlink=" + encodeURIComponent(info.feedlinks[0]) +
150                    "&public=1" +
151                    "&ApiKey=" + info.apiKey;
152
153      liberator.log("POST DATA:" + postBody);
154      var req = new libly.Request(
155        endpoint,
156        null,
157        {
158          asyncronus: false,
159          postBody: postBody
160        }
161      );
162      req.addEventListener("onSuccess", function(data) {
163        liberator.log("Post status: " + data.responseText);
164        liberator.echo("Subscribe feed succeed.");
165      });
166      req.addEventListener("onFailure", function(data) {
167        liberator.log("POST FAILURE: " + data.responseText);
168        liberator.echoerr("POST FAILURE: " + data.statusText);
169      });
170
171      req.post();
172    }
173
174    commands.addUserCommand(
175      command,
176      "Register feed subscriptions to " + servicename + ".",
177      function(args) {
178        handleFeedRequest({rate: args["-rate"]});
179      },
180      {
181        options: [
182          [["-rate", "-r"], commands.OPTION_INT]
183        ]
184      },
185      true  // Use in DEVELOP
186    );
187
188  }
189
190  function getSubscribeInfo(htmldoc) {
191    var subscribeInfo = {
192       target_url: null,
193       register: 1,
194       apiKey: null,
195       feedlinks: []
196    };
197
198    $LXs('id("feed_candidates")/li', htmldoc).forEach( function(item) {
199      var feedlink = $LX('./a[@class="feedlink"]', item);
200      var title = $LX('./a[@class="subscribe_list"]', item);
201      var users = $LX('./span[@class="subscriber_count"]/a', item);
202      var yet = $LX('./input[@name="feedlink"]', item);
203      liberator.log("input:" + feedlink.href);
204      subscribeInfo.feedlinks.push([feedlink.href, (yet != null), (title ? title.textContent : '' ) + ' / ' + (users ? users.textContent :  '0 user')]);
205    });
206
207    var target_url = $LX('id("target_url")', htmldoc);
208    if (!target_url) throw "Cannot find subscribe info about this page!";
209    subscribeInfo.target_url = target_url.value;
210    liberator.log("target_url:" + subscribeInfo.target_url);
211
212    subscribeInfo.apiKey = $LX('//*[@name="ApiKey"]', htmldoc).value;
213    if (!subscribeInfo.apiKey) throw "Can't get API key for subscription!";
214    return subscribeInfo;
215  }
216
217  function selectFeed(links, next) {
218    liberator.log(links.toSource());
219    liberator.echo("Following feeds were found on this site. Which are you subscribe?");
220    commandline.input("Select or input feed URL ", function(selected) {
221      liberator.echo("You select " + selected + ".");
222      commandline.close();
223      if (next && typeof next == "function")
224        next(selected);
225      else
226        liberator.echoerr("Your selected no is invalid.");
227    },{
228      completer: function(context) {
229        context.title = ["Available feeds", "Title / users"];
230        context.completions = links;
231      }
232    });
233    // Open candidates list forcibly
234    events.feedkeys("<TAB>");
235  }
236
237  // For convenience
238  function $LXs(a,b) libly.$U.getNodesFromXPath(a,b);
239  function $LX(a,b)  libly.$U.getFirstNodeFromXPath(a,b);
240
241  // }}}
242  return PUBLICS;
243})();
244
245// vim:sw=2 ts=2 et si fdm=marker:
Note: See TracBrowser for help on using the browser.