diff options
author | anekos | 2008-09-09 20:22:52 +0000 |
---|---|---|
committer | anekos | 2008-09-09 20:22:52 +0000 |
commit | 29059896232afabe1d3b1c0bac8f957f56c39fe7 (patch) | |
tree | 9d4a5c5f44278bc734a3440a5d4483c86f9a00c0 | |
parent | decf230b3552c732140b311357c8fb8546832872 (diff) | |
download | vimperator-plugins-29059896232afabe1d3b1c0bac8f957f56c39fe7.tar.bz2 |
initial release: ニコニコ動画のオススメ動画に飛ぶためのプラグイン
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@19104 d0d07461-0603-4401-acd4-de1884942a52
-rw-r--r-- | nico_related_videos.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/nico_related_videos.js b/nico_related_videos.js new file mode 100644 index 0000000..7ce3668 --- /dev/null +++ b/nico_related_videos.js @@ -0,0 +1,94 @@ +// ==VimperatorPlugin== +// @name Nico Related Videos +// @description-ja ニコニコ動画のオススメ動画のリスト +// @license Creative Commons 2.1 (Attribution + Share Alike) +// @version 1.0.0 +// ==/VimperatorPlugin== +// +// Author: +// anekos +// +// Usage: +// ニコニコ動画のオススメ動画のリストを補完で表示します。 +// +// コマンドにURL以外を指定したときの動作: +// 空 => ニコニコ動画のトップページに移動 +// 動画ID(sm.+) => 動画に移動 +// その他文字列 => ニコニコ動画でそれを検索 +// +// Link: +// http://d.hatena.ne.jp/nokturnalmortum/20080910#1220991278 + + +(function () { + + function getVideoId () { + let m = liberator.buffer.URL.match(/^http:\/\/www\.nicovideo\.jp\/watch\/([a-z0-9]+)/); + return m && m[1]; + } + + function httpRequest (uri, onComplete) { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function(){ + if(xhr.readyState == 4){ + if(xhr.status == 200) + onComplete(xhr.responseXML); + else + throw new Error(xhr.statusText) + } + }; + xhr.open("GET", uri, true); + xhr.send(null); + } + + function getRelatedVideos () { + let videoId = getVideoId(); + if (!videoId) + return []; + let videos = []; + let uri = 'http://www.nicovideo.jp/api/getrelation?sort=p&order=d&video=' + videoId; + let xhr = new XMLHttpRequest(); + xhr.open("GET", uri, false); + xhr.send(null); + let xml = xhr.responseXML; + let v, vs = xml.evaluate('//video', xml, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE , null); + while (v = vs.iterateNext()) { + let [cs, video] = [v.childNodes, {}]; + for each (let c in cs) + if (c.nodeName != '#text') + video[c.nodeName] = c.textContent; + videos.push(video); + } + return videos; + } + + + let last = {url: null, videos: []}; + let nothing = 'No related videos'; + + liberator.commands.addUserCommand( + ['nicorelated'], + 'niconico related videos', + function (url) { + url = (function () { + if (url == nothing) + return 'http://www.nicovideo.jp/'; + if (url.match(/^[a-z]{2}\d+$/)) + return 'http://www.nicovideo.jp/watch/' + url; + if (!url.match(/http:\/\//)) + return 'http://www.nicovideo.jp/search/' + encodeURIComponent(url); + })() || url; + liberator.open(url); + }, + { + completer: function (args) { + if (liberator.buffer.URL != last.url) { + last.videos = [[v.url, v.title] for each (v in getRelatedVideos())]; + last.url = liberator.buffer.URL; + } + return [0, last.videos.length ? last.videos : [[nothing, nothing]]]; + } + } + ); + +})(); |