diff options
Diffstat (limited to 'nico_related_videos.js')
-rw-r--r-- | nico_related_videos.js | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/nico_related_videos.js b/nico_related_videos.js index 197a67e..7a4fb3d 100644 --- a/nico_related_videos.js +++ b/nico_related_videos.js @@ -2,7 +2,7 @@ // @name Nico Related Videos // @description-ja ニコニコ動画のオススメ動画のリスト // @license Creative Commons 2.1 (Attribution + Share Alike) -// @version 1.0.0 +// @version 1.1.0 // ==/VimperatorPlugin== // // Author: @@ -14,6 +14,7 @@ // コマンドにURL以外を指定したときの動作: // 空 => ニコニコ動画のトップページに移動 // 動画ID(sm.+) => 動画に移動 +// ":" タグ名 => タグ検索 // その他文字列 => ニコニコ動画でそれを検索 // // Link: @@ -23,7 +24,7 @@ (function () { function getVideoId () { - let m = liberator.buffer.URL.match(/^http:\/\/www\.nicovideo\.jp\/watch\/([a-z0-9]+)/); + let m = buffer.URL.match(/^http:\/\/www\.nicovideo\.jp\/watch\/([a-z0-9]+)/); return m && m[1]; } @@ -62,19 +63,28 @@ return videos; } + function getRelatedTags () { + let doc = content.document; + let nodes = doc.getElementsByClassName('nicopedia'); + return [it.textContent for each (it in nodes) if (it.rel == 'tag')]; + } + - let last = {url: null, videos: []}; + let last = {url: null, completions: []}; let nothing = 'No related videos'; - liberator.commands.addUserCommand( + commands.addUserCommand( ['nicorelated'], 'niconico related videos', function (url) { + (url === undefined) || (url = url.string); 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(/^[:\uff1a]/)) + return 'http://www.nicovideo.jp/tag/' + encodeURIComponent(url.substr(1)); if (url.indexOf('http://') == -1) return 'http://www.nicovideo.jp/search/' + encodeURIComponent(url); })() || url; @@ -82,11 +92,13 @@ }, { 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; + if (buffer.URL != last.url) { + last.completions = []; + getRelatedVideos().forEach(function (it) last.completions.push([it.url, it.title])); + getRelatedTags().forEach(function (it) last.completions.push([":" + it, "tag"])); + last.url = buffer.URL; } - return [0, last.videos.length ? last.videos : [[nothing, nothing]]]; + return [0, last.completions.length ? last.completions : [[nothing, nothing]]]; } } ); |