Append a Reservation Button

on the community page.

目前為 2021-02-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Append a Reservation Button
// @name:ja        タイムシフト予約ボタン追加
// @namespace      https://greasyfork.org/users/19523
// @description    on the community page.
// @description:ja コミュニティページのニコ生配信一覧にタイムシフト予約ボタンを付加
// @include        https://com.nicovideo.jp/community/*
// @include        https://com.nicovideo.jp/live/*
// @version        0.1
// @grant          none
// @run-at         document-idle
// ==/UserScript==

if (location.pathname.match('/community')) {
  var target = document.querySelector('li.liveNoRecent').parentElement.parentElement;
  var invoke = function (mutations) {
    createReservationBtn('a.now_live_inner', 'float: right;writing-mode: vertical-rl;text-orientation: upright;');
    createReservationBtn('a.liveTitle', '');
    observer.disconnect();
  };
} else if (location.pathname.match('/live')) {
  var target = document.getElementsByClassName('area-communityLive')[0];
  var invoke = function (mutations) {
    createReservationBtn('a.liveTitle', 'float: right;');
    observer.disconnect();
  };
}

var observer = new MutationObserver(invoke);
observer.observe(target, { childList: true, subtree: true });

function createReservationBtn(selector, style) {
  var streams = document.querySelectorAll(selector);

  for (var i = 0; streams[i]; i++) {
    var btn = document.createElement('a');
    btn.innerHTML = 'TS予約';
    btn.setAttribute('style', style);
    btn.addEventListener('click', { handleEvent: reserve, videoId: streams[i].pathname.match(/\d+$/), btn: btn});

    streams[i].insertAdjacentElement('beforebegin', btn);
  }
}

function reserve (ev) {
  var post = new XMLHttpRequest();
  post.open('POST', 'https://live.nicovideo.jp/api/timeshift.reservations');
  post.responseType = 'json';
  post.withCredentials = true;

  var fd = new FormData();
  fd.set('vid', this.videoId);
  post.send(fd);

  var self = this;
  post.addEventListener('load', function () {
    if (post.response.meta.status == 200) {
      self.btn.innerHTML = '完了';
    } else {
      self.btn.innerHTML = '失敗';
    }
    setTimeout(function () { self.btn.innerHTML = 'TS予約'; }, 5000);
  });
  post.addEventListener('error', function () {
    alert('Reservation Failed: Maybe, you need a bug report.');
  });
}