Greasy Fork is available in English.

HistogramHeatGraph_html5.user.js

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

2017-11-07 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name HistogramHeatGraph_html5.user.js
  3. // @namespace sotoba
  4. // @version 0.20171107
  5. // @description ニコニコ動画でコメントの盛り上がりをグラフで表示(html5版)
  6. // @match http://www.nicovideo.jp/watch/*
  7. // @include http://www.nicovideo.jp/watch/*
  8. // @require http://code.jquery.com/jquery-latest.min.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13.  
  14. const MINIMUMBARNUM=50;
  15. const DEFAULTINTERBAL=10;
  16. const MAXCOMMENTNUM=30;
  17. const GRAPHHEIGHT = 30;
  18. function setStyle() {/*
  19. #comment-graph {
  20. background: repeating-linear-gradient(to top, #000, #222 10px);
  21. border: 1px solid #000;
  22. border-top: 0;
  23. float: left;
  24. font-size: 0;
  25. white-space: nowrap;
  26. }
  27. #comment-list {
  28. background: #000;
  29. color: #fff;
  30. font-size: 12px;
  31. line-height: 1.25;
  32. padding: 4px 4px 0;
  33. pointer-events: none;
  34. position: absolute;
  35. z-index: 9999;
  36. }
  37. #comment-list:empty {
  38. display: none;
  39. }
  40. */}
  41. const style = document.createElement('style');
  42. const styleText = setStyle.toString().match(/\/\*([^]*)\*\//)[1];
  43. style.appendChild(document.createTextNode(styleText));
  44. document.body.appendChild(style);
  45.  
  46. var ControllerBox=$('.ControllerBoxContainer').eq(0);
  47. var PlayerContainer=$('.PlayerContainer').eq(0);
  48.  
  49. PlayerContainer.append('<div id=comment-graph></div>');
  50. $('.MainContainer').eq(0).append('<div id=comment-list></div>');
  51. const $commentgraph = $('#comment-graph');
  52.  
  53. const $list = $('#comment-list');
  54. var ApiJsonData=JSON.parse(document.getElementById('js-initial-watch-data').getAttribute('data-api-data'));
  55. var thread_id=ApiJsonData.video.dmcInfo.thread.thread_id;
  56. var video_id=ApiJsonData.video.id;
  57. var user_id=ApiJsonData.video.dmcInfo.user.user_id;
  58.  
  59. if(video_id.startsWith('sm')||video_id.startsWith('nm')){
  60. $.ajax({
  61. url:'http://nmsg.nicovideo.jp/api/thread?thread='+thread_id+'&version=20061206&res_from=-1000&scores=1',
  62. type:'GET',
  63. dataType:'xml',
  64. timeout:3000,
  65. error:function() {
  66. console.log("Ajax:failed");
  67. },
  68. success:function(xml){
  69. drowgraph(xml,ApiJsonData);
  70. }
  71. });
  72. }else{
  73. $.ajax({
  74. url:'http://flapi.nicovideo.jp/api/getthreadkey?thread='+thread_id,
  75. type:'GET',
  76. timeout:3000,
  77. error:function() {
  78. console.log("Ajax:failed");
  79. },
  80. success:function(response){
  81. $.ajax({
  82. url:'http://nmsg.nicovideo.jp/api/thread?thread='+thread_id+'&version=20061206&res_from=-1000&scores=1&user='+user_id+'&'+response,
  83. type:'GET',
  84. dataType:'xml',
  85. timeout:3000,
  86. error:function() {
  87. console.log("Ajax:failed...");
  88. },
  89. success:function(xml){
  90. drowgraph(xml,ApiJsonData);
  91. }
  92. });
  93. }
  94. });
  95. }
  96.  
  97. function drowgraph(commentData,ApiJsonData){
  98. const playerWidth =parseFloat($("#CommentRenderer").children('canvas').eq(0).css("width"));
  99. var videoTotalTime =ApiJsonData.video.dmcInfo.video.length_seconds;
  100. var barTimeInterval;
  101. var barIndexNum;
  102. if(videoTotalTime > MINIMUMBARNUM*DEFAULTINTERBAL){
  103. barTimeInterval=DEFAULTINTERBAL;
  104. barIndexNum=Math.ceil(videoTotalTime / barTimeInterval);
  105. }else if(videoTotalTime>MINIMUMBARNUM){
  106. barIndexNum=MINIMUMBARNUM;
  107. barTimeInterval=Math.round(videoTotalTime/MINIMUMBARNUM);
  108. }else{
  109. barIndexNum=Math.floor(videoTotalTime);
  110. barTimeInterval=1;
  111. }
  112. $('#comment-graph').css( "width" , playerWidth );
  113. const barColors = [
  114. '126da2', '1271a8', '1275ae', '1279b4', '137dba',
  115. '1381c0', '1385c6', '1489cc', '148dd2', '1491d8'
  116. ];
  117. var listCounts = (new Array(barIndexNum)).fill(0);
  118. var listMessages = (new Array(barIndexNum)).fill("");
  119. var listTimes = (new Array(barIndexNum)).fill("");
  120. var lastBarTimeIntervalGap = Math.floor(videoTotalTime- (barIndexNum * barTimeInterval));
  121. var barWidth = playerWidth / barIndexNum;
  122. var barTimePoint = 0;
  123.  
  124. $(commentData).find('chat').each(function(index){
  125. var vpos = $(this).attr('vpos')/100;
  126. var section=Math.floor(vpos/barTimeInterval);
  127. listCounts[section]++;
  128. if(listCounts[section]<=MAXCOMMENTNUM){
  129. var comment=$(this).text().replace(/"|<|&lt;/g, ' ').replace(/\n/g, '<br>');
  130. listMessages[section]+=comment+'<br>';
  131. }
  132. });
  133. var startMin=0;
  134. var startSec=0;
  135. var min=0;
  136. var sec=0;
  137. for (var i = 0; i < barIndexNum-1; i++) {
  138. startMin=min;
  139. startSec=sec;
  140. sec+=barTimeInterval;
  141. if(59 < sec){
  142. min+=1;
  143. sec-=60;
  144. }
  145. listTimes[i] += `${("0"+startMin).slice(-2)}:${("0"+startSec).slice(-2)}-${("0"+min).slice(-2)}:${("0"+sec).slice(-2)}`;
  146. }
  147. startMin=min;
  148. startSec=sec;
  149. sec+=(barTimeInterval+lastBarTimeIntervalGap);
  150. if(59 < sec){
  151. min+=1;
  152. sec-=60;
  153. }
  154. listTimes[barIndexNum-1] += `${("0"+startMin).slice(-2)}:${("0"+startSec).slice(-2)}-${("0"+min).slice(-2)}:${("0"+sec).slice(-2)}`;
  155.  
  156. // TODO なぜかbarIndexNum以上の配列ができる
  157. listCounts=listCounts.slice(0, barIndexNum);
  158. var listCountMax = Math.max.apply(null,listCounts);
  159. const barColorRatio = (barColors.length - 1) / listCountMax;
  160.  
  161.  
  162. $commentgraph.empty();
  163. $commentgraph.height(GRAPHHEIGHT);
  164. var barColor;
  165. var barBackground;
  166. for (i = 0; i <= barIndexNum; i++) {
  167. barColor = barColors[Math.floor(listCounts[i] * barColorRatio)];
  168. barBackground = `linear-gradient(to top, #${barColor}, #${barColor} ` +
  169. `${listCounts[i]}px, transparent ${listCounts[i]}px, transparent)`;
  170. var barText = listCounts[i] ?
  171. `${listMessages[i]}<br><br>${listTimes[i]} コメ ${listCounts[i]}` : '';
  172. $('<div>')
  173. .css('background-image', barBackground)
  174. .css('float','left')
  175. .data('text', barText)
  176. .height(GRAPHHEIGHT)
  177. .width(barWidth)
  178. .appendTo($commentgraph);
  179. }
  180. $commentgraph.children().on({
  181. 'mouseenter': function(val) {
  182. $list
  183. .css({
  184. 'left': $(this).offset().left,
  185. 'top': $commentgraph.offset().top - $list.height() - 10
  186. })
  187. .html($(this).data('text'));
  188. },
  189. 'mousemove': function(val) {
  190. $list.offset({
  191. 'left': $(this).offset().left,
  192. 'top': $commentgraph.offset().top - $list.height() - 10
  193. });
  194. },
  195. 'mouseleave': function() {
  196. $list.empty();
  197. }
  198. });
  199.  
  200. }
  201. })();