HistogramHeatGraph_html5.user.js

ニコニコ動画でコメントの盛り上がりをグラフで表示(html5版)

As of 2017-11-18. See the latest version.

  1. // ==UserScript==
  2. // @name HistogramHeatGraph_html5.user.js
  3. // @namespace sotoba
  4. // @version 1.0.1.20171119
  5. // @description ニコニコ動画でコメントの盛り上がりをグラフで表示(html5版)
  6. // @match http://www.nicovideo.jp/watch/*
  7. // @include http://www.nicovideo.jp/watch/*
  8. // @require https://code.jquery.com/jquery-3.2.1.min.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14. // default settings
  15. var NicoHeatGraph = function(){
  16. this.MINIMUMBARNUM=50;
  17. this.DEFAULTINTERBAL=10;
  18. this.MAXCOMMENTNUM=30;
  19. this.GRAPHHEIGHT = 30;
  20. this.GRAPHDEFWIDTH=1368;
  21. this.barIndexNum=0;
  22. this.$commentgraph=$('<div>').attr('id', 'comment-graph');
  23. this.$commentlist=$('<div>').attr('id', 'comment-list');
  24. };
  25. //draw background of graph
  26. NicoHeatGraph.prototype.drawCoordinate = function(){
  27. const $commentgraph = this.$commentgraph;
  28. const $commentlist = this.$commentlist;
  29. let $canvas=$("#CommentRenderer").children('canvas').eq(0);
  30. $('.PlayerContainer').eq(0).append($commentgraph);
  31. $('.MainContainer').eq(0).append($commentlist);
  32. const styleString = `
  33. #comment-graph :hover{
  34. -webkit-filter: hue-rotate(180deg);
  35. filter: hue-rotate(180deg);
  36. }
  37. #comment-list:empty {
  38. display: none;
  39. }
  40. `;
  41. const style = document.createElement('style');
  42. style.appendChild(document.createTextNode(styleString));
  43. document.body.appendChild(style);
  44. var playerWidth =parseFloat($canvas.css("width"))|this.GRAPHDEFWIDTH;
  45. $commentgraph.height(this.GRAPHHEIGHT);
  46. $commentgraph.width( playerWidth );
  47. $commentgraph.css({
  48. background:'repeating-linear-gradient(to top, #000, #111 5px)',
  49. border: '1px solid #000',
  50. borderTo: 0,
  51. float: 'left',
  52. fontSize: 0,
  53. whiteSpace: 'nowrap',
  54. });
  55. $commentlist.css({
  56. background: '#000',
  57. color: '#fff',
  58. fontSize: '12px',
  59. lineHeight: 1.25,
  60. padding: '4px 4px 0',
  61. pointerEvents:' none',
  62. position: 'absolute',
  63. zIndex: 9999,
  64. });
  65. };
  66.  
  67. function getCommentData() {
  68. var ApiJsonData=JSON.parse(document.getElementById('js-initial-watch-data').getAttribute('data-api-data'));
  69. var thread_id;
  70. var video_id;
  71. var user_id;
  72. if(ApiJsonData.video.dmcInfo !== null){
  73. thread_id=ApiJsonData.video.dmcInfo.thread.thread_id;
  74. video_id=ApiJsonData.video.dmcInfo.video.video_id;
  75. user_id=ApiJsonData.video.dmcInfo.user.user_id;
  76. }else{
  77. thread_id=ApiJsonData.thread.ids.default;
  78. video_id=ApiJsonData.video.id;
  79. user_id=ApiJsonData.viewer.id;
  80. }
  81.  
  82. if(video_id.startsWith('sm')||video_id.startsWith('nm')){
  83. return $.ajax({
  84. url:'http://nmsg.nicovideo.jp/api/thread?thread='+thread_id+'&version=20061206&res_from=-1000&scores=1',
  85. type:'GET',
  86. dataType:'xml'
  87. });
  88. }else{
  89. return $.ajax({
  90. url:'http://flapi.nicovideo.jp/api/getthreadkey?thread='+thread_id,
  91. type:'GET',
  92. }).then(function(response){
  93. return $.ajax({
  94. url:'http://nmsg.nicovideo.jp/api/thread?thread='+thread_id+'&version=20061206&res_from=-1000&scores=1&user='+user_id+'&'+response,
  95. type:'GET',
  96. dataType:'xml'
  97. });
  98. });
  99. }
  100. }
  101. //draw bars make comment list
  102. NicoHeatGraph.prototype.drowgraph = function(commentData,$canvas){
  103. const $commentgraph = $('#comment-graph');
  104. const $commentlist = $('#comment-list');
  105. var ApiJsonData=JSON.parse(document.getElementById('js-initial-watch-data').getAttribute('data-api-data'));
  106. var playerWidth =parseFloat($canvas.css("width"));
  107. var videoTotalTime = ApiJsonData.video.dmcInfo !== null ? ApiJsonData.video.dmcInfo.video.length_seconds : ApiJsonData.video.duration;
  108. var barTimeInterval;
  109.  
  110. //TODO 非常に長い(2,3時間以上)動画の処理
  111. //長い動画
  112. if(videoTotalTime > this.MINIMUMBARNUM*this.DEFAULTINTERBAL){
  113. barTimeInterval=this.DEFAULTINTERBAL;
  114. this.barIndexNum=Math.ceil(videoTotalTime / barTimeInterval);
  115. //普通の動画
  116. }else if(videoTotalTime>this.MINIMUMBARNUM){
  117. this.barIndexNum=this.MINIMUMBARNUM;
  118. barTimeInterval=videoTotalTime/this.MINIMUMBARNUM;
  119. }else{
  120. //MINIMUMBARNUM秒以下の短い動画
  121. this.barIndexNum=Math.floor(videoTotalTime);
  122. barTimeInterval=1;
  123. }
  124.  
  125. $commentgraph.width( playerWidth );
  126. const barColors = [
  127. '003165', '00458f', '0058b5','005fc4', '006adb',
  128. '0072ec', '007cff', '55a7ff','3d9bff'
  129. ];
  130. var listCounts = (new Array(this.barIndexNum+1)).fill(0);
  131. var listMessages = (new Array(this.barIndexNum+1)).fill("");
  132. var listTimes = (new Array(this.barIndexNum+1)).fill("");
  133. var lastBarTimeIntervalGap = Math.floor(videoTotalTime- (this.barIndexNum * barTimeInterval));
  134. var barWidth = playerWidth / this.barIndexNum;
  135. var barTimePoint = 0;
  136.  
  137. const MAXCOMMENTNUM=this.MAXCOMMENTNUM;
  138.  
  139. $(commentData).find('chat').each(function(index){
  140. let vpos = $(this).attr('vpos')/100;
  141. //動画長を超えた時間のpostがある
  142. if (videoTotalTime<=vpos){
  143. vpos=videoTotalTime;
  144. }
  145. let section=Math.floor(vpos/barTimeInterval);
  146. listCounts[section]++;
  147. if(listCounts[section]<= MAXCOMMENTNUM){
  148. let comment=$(this).text().replace(/"|<|&lt;/g, ' ').replace(/\n/g, '<br>');
  149. listMessages[section]+=comment+'<br>';
  150. }
  151. });
  152.  
  153. let starttime=0;
  154. let nexttime=0;
  155. for (var i = 0; i < this.barIndexNum; i++) {
  156. starttime=nexttime;
  157. nexttime+=barTimeInterval;
  158. if(i==this.barIndexNum-1){
  159. nexttime+=lastBarTimeIntervalGap;
  160. }
  161. let startmin=Math.floor(starttime/60);
  162. let startsec=Math.floor(starttime-startmin*60);
  163. let endmin=Math.floor(nexttime/60);
  164. let endsec=Math.ceil(nexttime-endmin*60);
  165. if(59 < endsec){
  166. endmin+=1;
  167. endsec-=60;
  168. }
  169. listTimes[i] += `${("0"+startmin).slice(-2)}:${("0"+startsec).slice(-2)}-${("0"+endmin).slice(-2)}:${("0"+endsec).slice(-2)}`;
  170. }
  171.  
  172. // TODO なぜかthis.barIndexNum以上の配列ができる
  173. listCounts=listCounts.slice(0, this.barIndexNum);
  174. var listCountMax = Math.max.apply(null,listCounts);
  175. const barColorRatio = (barColors.length - 1) / listCountMax;
  176.  
  177. $commentgraph.empty();
  178. $commentgraph.height(this.GRAPHHEIGHT);
  179. var barColor;
  180. var barBackground;
  181. for (i = 0; i < this.barIndexNum; i++) {
  182. barColor = barColors[Math.floor(listCounts[i] * barColorRatio)];
  183. barBackground = `linear-gradient(to top, #${barColor}, #${barColor} ` +
  184. `${listCounts[i]}px, transparent ${listCounts[i]}px, transparent)`;
  185. var barText = listCounts[i] ?
  186. `${listMessages[i]}<br><br>${listTimes[i]} コメ ${listCounts[i]}` : '';
  187. $('<div>')
  188. .css('background-image', barBackground)
  189. .css('float','left')
  190. .data('text', barText)
  191. .height(this.GRAPHHEIGHT)
  192. .width(barWidth)
  193. .addClass("commentbar")
  194. .appendTo($commentgraph);
  195. }
  196. };
  197. // set mouse functions
  198. NicoHeatGraph.prototype.addMousefunc = function($canvas){
  199. let $commentgraph = this.$commentgraph;
  200. let $commentlist = this.$commentlist;
  201. function mouseOverFunc() {
  202. $commentlist.css({
  203. 'left': $(this).offset().left,
  204. 'top': $commentgraph.offset().top - $commentlist.height() - 10
  205. })
  206. .html($(this).data('text'));
  207. }
  208. function mouseOutFunc() {
  209. $commentlist.empty();
  210. }
  211.  
  212. $commentgraph.children().on({
  213. 'mouseenter': function(val) {
  214. $commentlist.css({
  215. 'left': $(this).offset().left,
  216. 'top': $commentgraph.offset().top - $commentlist.height() - 10
  217. })
  218. .html($(this).data('text'));
  219. },
  220. 'mousemove': function(val) {
  221. $commentlist.offset({
  222. 'left': $(this).offset().left,
  223. 'top': $commentgraph.offset().top - $commentlist.height() - 10
  224. });
  225. },
  226. 'mouseleave': function() {
  227. $commentlist.empty();
  228. }
  229. });
  230.  
  231. /* 1 Dom Style Watcher本体 監視する側*/
  232. var domStyleWatcher = {
  233. Start: function(tgt, styleobj){
  234. function eventHappen(data1, data2){
  235. var throwval = tgt.css(styleobj);
  236. tgt.trigger('domStyleChange', [throwval]);
  237. }
  238. var tge = tgt[0];
  239. var filter = ['style'];
  240. var options = {
  241. attributes: true,
  242. attributeFilter: filter
  243. };
  244. var mutOb = new MutationObserver(eventHappen);
  245. mutOb.observe(tge, options);
  246. return mutOb;
  247. },
  248. Stop: function(mo){
  249. mo.disconnect();
  250. }
  251. };
  252. function catchEvent(event, value){
  253. var playerWidth=parseFloat(value);
  254. var barIndexNum=$('.commentbar').length;
  255. $commentgraph.width(playerWidth);
  256. $('.commentbar').width(playerWidth /barIndexNum);
  257. }
  258. var target = $canvas;
  259. var styleobj = 'width';
  260. target.on('domStyleChange', catchEvent);//イベントを登録
  261. var dsw = domStyleWatcher.Start(target, styleobj);//監視開始
  262. //domStyleWatcher.Stop(dsw);//監視終了
  263. };
  264.  
  265. // Main
  266. var heatgraph = new NicoHeatGraph();
  267. heatgraph.drawCoordinate();
  268. getCommentData().done(function(data, textStatus, jqXHR){
  269. let canvas=$("#CommentRenderer").children('canvas').eq(0);
  270. heatgraph.drowgraph(data,canvas);
  271. heatgraph.addMousefunc(canvas);
  272. }).fail(function(jqXHR, textStatus, errorThrown){
  273. //TODO
  274. console.log("failed");
  275. });
  276. })();