HistogramHeatGraph.user.js

Visualize comments upsurge for Nicovideo

Fra 09.08.2014. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name           HistogramHeatGraph.user.js
// @version        0.20140627
// @description    Visualize comments upsurge for Nicovideo
// @match          http://www.nicovideo.jp/watch/*
// @include        http://www.nicovideo.jp/watch/*
// @grant          none
// @namespace https://gist.github.com/ishitsuka/
// ==/UserScript==

(function() {

  var style = document.createElement('style'),
      styleText = function() {/*

    #playerContainer {
      text-align: left;
    }
    .size_small #playerTabWrapper ~ *,
    #comment-list:empty {
      display: none;
    }
    .size_normal #playerTabWrapper + * {
      -webkit-transform-origin: 0;
      transform-origin: 0;
      -webkit-transform: scaleX(1.336);
      transform: scaleX(1.336);
    }
    #playerTabWrapper + * {
      background: repeating-linear-gradient(to top, #000, #222 10px);
      border: 1px solid #000;
      border-top: 0;
      float: left;
      font-size: 0;
      white-space: nowrap;
      width: 670px;
    }
    #playerTabWrapper + * * {
      display: inline-block;
    }
    #playerTabWrapper + * *:hover {
      background-color: rgba(255, 255, 255, .1);
    }
    .size_normal #playerContainer > a {
      margin-left: 236px;
    }
    #playerContainer > a {
      cursor: pointer;
      margin-left: 10px;
    }
    #comment-list {
      background: #000;
      color: #fff;
      font-size: 12px;
      line-height: 1.25;
      padding: 4px 4px 0;
      pointer-events: none;
      position: absolute;
      z-index: 9999;
    }

  */};
  styleText = styleText.toString().match(/\/\*([^]*)\*\//)[1];
  style.appendChild(document.createTextNode(styleText));
  document.body.appendChild(style);

  var setScript = function() {

    var setElement = function() {
      $('#playerContainer')
          .append('<div>')
          .append('<a>');
      $('<div>')
          .attr('id', 'comment-list')
          .appendTo('body');
    };

    var setContent = function(barTimeInterval) {

      var cpvc = WatchApp.ns.init.PlayerInitializer.commentPanelViewController,
          comments = [],
          videoTotalTime = $('#external_nicoplayer')[0].ext_getTotalTime(),
          barIndex = Math.ceil(videoTotalTime / barTimeInterval),
          listMessages = [],
          listCounts = [],
          barTimePoint = 0,
          listTimes = [],
          lastBarTimeIntervalGap = barTimeInterval - videoTotalTime % barTimeInterval | 0;

      cpvc.commentLists.some(function(e) {
        if (e.listName === cpvc.commentListModel.getListName()) {
          comments = e.comments;
          return true;
        }
      });

      comments.some(function(e) {
        if (e.vpos / 1000 > videoTotalTime + 1) {
          videoTotalTime += 10;
          return true;
        }
      });

      for (var i = 0; i < barIndex; i++) {

        listMessages[i] = [];
        listCounts[i] = 0;

        comments.forEach(function(e) {
          var thisTimePoint = e.vpos / 1000;
          if (barTimePoint <= thisTimePoint && thisTimePoint < barTimeInterval + barTimePoint) {
            listMessages[i].push(e);
            listCounts[i]++;
          }
        });

        listMessages[i].sort(function(a, b) { return a.vpos - b.vpos; }).
            forEach(function(e, j) {
              listMessages[i][j] = e.message.replace(/"|<|&lt;/g, ' ').replace(/\n/g, '<br>');
            });
        listMessages[i] = listMessages[i].join('<br>');

        var min = barTimePoint / 60 | 0,
            sec = barTimePoint - min * 60;
        if (sec < 10) sec = '0' + sec;
        listTimes[i] = min + ':' + sec + '-';
        if (i > barIndex - 2) barTimePoint -= lastBarTimeIntervalGap;
        min = (barTimeInterval + barTimePoint) / 60 | 0;
        sec = barTimeInterval + barTimePoint - min * 60;
        if (sec < 10) sec = '0' + sec;
        listTimes[i] += min + ':' + sec;

        barTimePoint += barTimeInterval;

      }

      var listCountMax = Math.max.apply(null, listCounts),
          graphHeight = listCountMax > 10 ? listCountMax : 10,
          playerWidth = 670,
          barColors = [
              '126da2', '1271a8', '1275ae', '1279b4', '137dba',
              '1381c0', '1385c6', '1489cc', '148dd2', '1491d8'
          ],
          barColorRatio = (barColors.length - 1) / listCountMax,
          lastBarWidthRatio = videoTotalTime % barTimeInterval / barTimeInterval,
          barWidth = playerWidth / (lastBarWidthRatio + barIndex - 1),
          elmGraph = $('#playerTabWrapper').next(),
          elmList = $('#comment-list');

      $('#playerContainerWrapper').css('padding-bottom', graphHeight + 11);
      $('.leftPanelAdCloseButton').trigger('click');
      elmGraph.empty();

      for (i = 0; i < barIndex; i++) {

        var barTitle = listCounts[i] ?
            listMessages[i] + '<br><br>' + listTimes[i] + ' コメ ' + listCounts[i] : '',
            barColor = barColors[listCounts[i] * barColorRatio | 0],
            barBackground = 'linear-gradient(to top,' +
                '#' + barColor + ',' +
                '#' + barColor + ' ' + listCounts[i] + 'px,' +
                'transparent ' + listCounts[i] + 'px,' +
                'transparent)';

        if (i > barIndex - 2) barWidth *= lastBarWidthRatio;

        $('<div>')
            .attr('title', barTitle)
            .css('background-image', barBackground)
            .height(graphHeight)
            .width(barWidth)
            .appendTo(elmGraph);

      }

      elmGraph.children().on({
        'mouseenter': function(e) {
          barTitle = $(this).attr('title');
          barBackground = $(this).css('background-image');
          $(this)
              .removeAttr('title')
              .css('background-image', barBackground.replace(/rg.+?\)/g, '#f60'));
          elmList
              .css({
                'left': e.pageX,
                'top': elmGraph.offset().top - elmList.height() - 10
              })
              .html(barTitle);
        },
        'mousemove': function(e) {
          elmList.offset({
            'left': e.pageX,
            'top': elmGraph.offset().top - elmList.height() - 10
          });
        },
        'mouseleave': function() {
          $(this)
              .attr('title', barTitle)
              .css('background-image', barBackground);
          elmList.empty();
        }
      });

      var elmLastBar = elmGraph.children().last(),
          aaa = elmGraph.offset().left - elmLastBar.offset().left,
          lastBarWidth = $('#playerNicoplayer').width() < 898 ? aaa + 671 : (aaa + 897) * 672 / 898;
      elmLastBar.width(lastBarWidth > 1 ? lastBarWidth : 1);

      elmGraph.next()
          .on('click', function(e) { setContent(e.ctrlKey ? 60 : 10); })
          .text('コメ ' + comments.length);

      if (navigator.userAgent.match(/Chrome/)) elmList.css('background', 'rgba(0, 0, 0, .5)');

    };

    if (window.PlayerApp) {
      WatchApp.ns.init.PlayerInitializer.playerAreaConnector.addEventListener(
          'onCommentListInitialized', function() {
            if (!$('#comment-list')[0]) setElement();
            setTimeout(setContent, 1000, 10);
          });
    }

  };

  var script = document.createElement('script');
  script.appendChild(document.createTextNode('(' + setScript + ')()'));
  document.body.appendChild(script);

})();