aboutsummaryrefslogtreecommitdiffstats
path: root/nnp_cooperation.js
diff options
context:
space:
mode:
authorjanus_wel2008-11-04 21:02:16 +0000
committerjanus_wel2008-11-04 21:02:16 +0000
commit56cb882fc6f122c75608f8eb6142a607d3dc855f (patch)
treebf0f11c61e6d3455ddb32f538c7d9c8e5bb691d0 /nnp_cooperation.js
parent0748c10d3679e1f97a53641f71390307d1c5abbb (diff)
downloadvimperator-plugins-56cb882fc6f122c75608f8eb6142a607d3dc855f.tar.bz2
for vimperator 1.2
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/branches/1.2@22732 d0d07461-0603-4401-acd4-de1884942a52
Diffstat (limited to 'nnp_cooperation.js')
-rw-r--r--nnp_cooperation.js265
1 files changed, 265 insertions, 0 deletions
diff --git a/nnp_cooperation.js b/nnp_cooperation.js
new file mode 100644
index 0000000..71c2d1f
--- /dev/null
+++ b/nnp_cooperation.js
@@ -0,0 +1,265 @@
+/*
+ * ==VimperatorPlugin==
+ * @name niconicoplaylist_cooperation.js
+ * @description this script give you keyboard opration for NicoNicoPlaylist.
+ * @description-ja NicoNicoPlaylist をキーボードで操作できるようにする。
+ * @author janus_wel <janus_wel@fb3.so-net.ne.jp>
+ * @version 0.32
+ * @minversion 2.0pre 2008/10/16
+ * ==/VimperatorPlugin==
+ *
+ * CONSTRAINT
+ * need NicoNicoPlaylist version 1.12 or above
+ *
+ * LICENSE
+ * New BSD License
+ *
+ * USAGE
+ * :nnppushallvideos
+ * 現在のページ内のすべての動画を再生リストに送る。
+ * ランキングやマイリストのほか、動画ページではオススメ動画が追加される。
+ * :nnppushthisvideo
+ * 現在見ている動画を再生リストに送る。
+ * :nnpplaynext [next]
+ * 再生リストの次の動画を再生する。
+ * :nnpremove [index]
+ * index 番目の動画を再生リストから取り除く。 index は 0 から数える。
+ * 指定しない場合は一番上が取り除かれる。
+ * :nnpclear
+ * 再生リストをすべてクリアする。
+ * :nnpgetlist [numof]
+ * 再生リストの上から numof 個を表示する。指定しない場合は g:nnp_coop_numoflist が使われる。
+ * :nnprandom
+ * ランダムモードの on / off
+ * :nnploop
+ * ループモードの on / off
+ * :nnpfullscreen
+ * 全画面モードの on / off
+ *
+ * VARIABLES
+ * g:nnp_coop_numoflist
+ * :NNPGetList で表示するリストの個数を指定する。デフォルトは 10 。
+ *
+ * HISTORY
+ * 2008/07/11 ver. 0.10 - initial written.
+ * 2008/07/15 ver. 0.20 - refactoring.
+ * 2008/09/26 ver. 0.30 - change XPath expression.
+ * - correspond mode toggling (fullscreen, random, loop).
+ * - change caption: display now-playing title and mode's statuses.
+ * - mode's statuses are displayed with the following word.
+ * R: random mode is on
+ * L: loop mode is on
+ * F: fullscreen mode is on
+ * 2008/09/28 ver. 0.31 - bugfix :nnpgetlist in ranking page.
+ *
+ * */
+/*
+以下のコードを _vimperatorrc に貼り付けると幸せになれるかも。
+コマンド ( [',nn'] や [',nr'] の部分 ) は適宜変えてね。
+
+javascript <<EOM
+
+// [N],nn
+// N 番目の動画を再生する。
+// 指定なしの場合次の動画が再生される。
+liberator.mappings.addUserMap(
+ [liberator.modes.NORMAL],
+ [',nn'],
+ 'play next item in NicoNicoPlaylist',
+ function(count) {
+ if(count === -1) count = 1;
+ liberator.execute(':nnpplaynext ' + count);
+ },
+ { flags: liberator.Mappings.flags.COUNT }
+);
+
+// [N],nr
+// 上から N 個の動画を削除する。
+// 指定なしの場合一番上の動画が削除される。
+liberator.mappings.addUserMap(
+ [liberator.modes.NORMAL],
+ [',nr'],
+ 'remove item in NicoNicoPlaylist',
+ function(count) {
+ if(count === -1) count = 1;
+ for(let i=0 ; i<count ; ++i) liberator.execute(':nnpremove');
+ liberator.execute(':nnpgetlist');
+ },
+ { flags: liberator.Mappings.flags.COUNT }
+);
+
+EOM
+
+*/
+
+(function(){
+
+// thumbnail URL
+const thumbnailURL = 'http://tn-skr$HOSTNUMBER.smilevideo.jp/smile?i=$VIDEO_ID';
+
+// style
+const styles = [
+ '<style type="text/css">',
+ 'table.nnp_coop .index { text-align:right; width:2em; }',
+ 'table.nnp_coop .thumbnail { text-align:center; }',
+ 'table.nnp_coop caption { color:green; }',
+ 'table.nnp_coop thead { text-align:center; }',
+ '</style>',
+].join('');
+
+// table
+const tableTemplate = [
+ '<table class="nnp_coop">',
+ '$CAPTION',
+ '$THEAD',
+ '<tbody>$ITEMS</tbody>',
+ '</table>',
+].join('');
+
+// table caption
+const captionTemplate = '<caption>now playing: $PLAYTITLE (display $NUMOFDISPLAY / $NUMOFTOTAL$STATUSES)</caption>';
+
+// table head
+const thead = [
+ '<thead>',
+ '<tr>',
+ '<td> </td>',
+ '<td>thumbnail</td>',
+ '<td>title</td>',
+ '<td>url</td>',
+ '</tr>',
+ '</thead>',
+].join('');
+
+// item
+const itemHTML = [
+ '<tr>',
+ '<td class="index">$INDEX:</td>',
+ '<td class="thumbnail"><img src="$THUMBNAILURL" width="33" height="25" /></td>',
+ '<td>$TITLE</td>',
+ '<td>$URL</td>',
+ '</tr>',
+].join('');
+
+
+// scrape from div element that inserted by NicoNicoPlaylist
+liberator.commands.addUserCommand(['nnpgetlist'], 'get NicoNicoPlaylist',
+ function (arg) {
+ // check existence of NicoNicoPlaylist
+ var playlist = $f('//div[contains(@id, "playlistcontroller_")]');
+ if(!playlist) {
+ liberator.echoerr('NicoNicoPlaylist is not found.');
+ return;
+ }
+
+ var titleNode = $f('//h1') || $f('./html/head/title');
+ var playTitle = titleNode.textContent;
+ var statuses = '';
+ if($f('.//input[contains(@id, "-checkbox-random")]', playlist).checked) statuses += 'R';
+ if($f('.//input[contains(@id, "-checkbox-loop")]', playlist).checked) statuses += 'L';
+ if($f('.//input[contains(@id, "-checkbox-full")]', playlist).checked) statuses += 'F';
+ if(statuses) statuses = ' ' + statuses;
+
+ // check existence of items in NicoNicoPlaylist
+ var nodes = $s('./div[contains(concat(" ", @class, " "), " playlist-list-outer ")]/ul/li/a', playlist);
+ var nodesLength = nodes.length
+ if(nodesLength === 0) {
+ liberator.echoerr('no items in NicoNicoPlaylist.');
+ return;
+ }
+
+ // get number of displayed items
+ var numofList = arg.match(/^\d+$/)
+ ? arg
+ : (liberator.globalVariables.nnp_coop_numoflist || 10);
+
+ // struct display string
+ // generate data
+ var items = new Array;
+ for(let i=0 ; i<nodesLength && i<numofList ; ++i ) {
+ // get video id
+ let id = nodes[i].href.match(/\d+$/);
+ // build thumnail's URL
+ // refer: http://d.hatena.ne.jp/ZIGOROu/20081014/1223991205
+ let thumbnail = thumbnailURL.replace(/\$HOSTNUMBER/g, id % 2 + 1)
+ .replace(/\$VIDEO_ID/g, id);
+ // evaluate variables and push to list
+ items.push(
+ itemHTML.replace(/\$INDEX/g, i + 1)
+ .replace(/\$THUMBNAILURL/g, thumbnail)
+ .replace(/\$TITLE/g, nodes[i].textContent)
+ .replace(/\$URL/g, nodes[i].href)
+ );
+ }
+
+ // evaluate variables
+ var caption = captionTemplate
+ .replace(/\$NUMOFDISPLAY/g, (nodesLength < numofList) ? nodesLength : numofList)
+ .replace(/\$NUMOFTOTAL/g, nodesLength)
+ .replace(/\$PLAYTITLE/g, playTitle)
+ .replace(/\$STATUSES/g, statuses);
+
+ // final processing
+ var str = styles + tableTemplate.replace(/\$CAPTION/g, caption)
+ .replace(/\$THEAD/g, thead)
+ .replace(/\$ITEMS/g, items.join(''));
+
+ liberator.echo(str, liberator.commandline.FORCE_MULTILINE);
+ },{}
+);
+
+// stuff functions
+// return first node
+function $f(query, node) {
+ node = node || window.content.document;
+ var result = (node.ownerDocument || node).evaluate(
+ query,
+ node,
+ null,
+ XPathResult.FIRST_ORDERED_NODE_TYPE,
+ null
+ );
+ return result.singleNodeValue ? result.singleNodeValue : null;
+}
+
+// return snapshot nodes list
+function $s(query, node) {
+ node = node || window.content.document;
+ var result = (node.ownerDocument || node).evaluate(
+ query,
+ node,
+ null,
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
+ null
+ );
+ var nodes = [];
+ for(let i=0 ; i<result.snapshotLength ; ++i) nodes.push(result.snapshotItem(i));
+ return nodes;
+}
+
+// define other commands
+// only send CommandEvent to NicoNicoPlaylist script
+[
+ [['nnppushallvideos'], "push all videos to NicoNicoPlaylist", 'GMNNPPushAllVideos'],
+ [['nnppushthisvideo'], "push current video to NicoNicoPlaylist", 'GMNNPPushThisVideo'],
+ [['nnpplaynext'], "play next in NicoNicoPlaylist", 'GMNNPPlayNext'],
+ [['nnpremove'], "remove item in NicoNicoPlaylist", 'GMNNPRemove'],
+ [['nnpclear'], "clear all items in NicoNicoPlaylist", 'GMNNPClear'],
+ [['nnprandom'], "toggle random mode of NicoNicoPlaylist", 'GMNNPRandom'],
+ [['nnploop'], "toggle loop mode of NicoNicoPlaylist", 'GMNNPLoop'],
+ [['nnpfullscreen'], "toggle fullscreen mode of NicoNicoPlaylist", 'GMNNPFullScreen'],
+].forEach(
+ function ([command, description, eventname]){
+ liberator.commands.addUserCommand(command, description,
+ function (arg) {
+ var r = document.createEvent("CommandEvent");
+ r.initCommandEvent(eventname, true, true, arg);
+ window.content.dispatchEvent(r);
+ },{}
+ );
+ }
+);
+
+})();
+
+// vim:sw=4 ts=4 et: