Greasy Fork is available in English.

HistogramHeatGraph_html5.user.js

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

اعتبارا من 04-11-2017. شاهد أحدث إصدار.

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