OOGIRI+ Time left adjustment

残り時間を割り込まれた処理などで遅れないものにします

当前为 2017-06-15 提交的版本,查看 最新版本

// ==UserScript==
// @name        OOGIRI+ Time left adjustment
// @name:ja     大喜利プラス ズレない残り時間
// @namespace   https://greasyfork.org/users/19523
// @description 残り時間を割り込まれた処理などで遅れないものにします
// @include     http://oogiri.biz/
// @version     0.1
// @grant       none
// ==/UserScript==


(function () {
  switch ($('#header-info .title').eq(0).text().split(' ')[1]) {
    case 'ボケ受付中':
      var url = 'http://oogiri.biz/ajax/get_boke_limit';
      break;
    case '投票中':
      var url = 'http://oogiri.biz/ajax/get_vote_limit';
      break;
  }

  function timer(data) {
    var end = (data * 1000) + Date.now();
    return function ($timer, observer) {
      var limit = end - Date.now();
      if (limit >= 0) {
        var min = Math.floor(limit / (1000 * 60));
        var sec = Math.floor(limit / 1000) % 60;
        $timer.text(`${min}分${sec}秒`);
      } else {
        observer.disconnect();
        $timer.text(`終了`);
        location.replace(location.href.replace(location.hash, ''));
      }
    };
  }

  $.ajax({
    url: url,
    type: 'GET',
    dataType: 'json'
  })
  .done(init);

  function init(data) {
    if (data <= 0)
      return;

    $info = $('.info .limit .time');
    $info.css('display', 'none');
    var $timer = $('<span class="usertime"></span>');
    $info.parent().append($timer);

    var timerON = timer(data);
    var observer = new MutationObserver(function (mutations) {
      timerON($timer, observer);
    });
    observer.observe($info[0], {childList: true});
  }
})();