OOGIRI+ Time left adjustment

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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


(function () {
  var url = $('#main > article').next('script').text().match(/http\:\/\/oogiri\.biz\/api\/get_(boke|vote)_limit_second\/\d+/)[0];

  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['second'] <= 0)
      return;

    var $oldTime = $('.limitTime .time').eq(0);
    $oldTime.css('display', 'none');
    var $timer = $('<time></time>');
    $oldTime.parent().append($timer);

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