cytube_set_page_title

残り時間とタイトルをタブに表示

当前为 2023-09-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         cytube_set_page_title
// @namespace    https://cytube.xyz/
// @version      1.7
// @description  残り時間とタイトルをタブに表示
// @author       utubo
// @match        *://cytube.xyz/*
// @match        *://cytube.mm428.net/*
// @grant        none
// ==/UserScript==

(window.unsafeWindow || window).eval(` // ← チャンネルのjsにセットするときはこの行(と最後の行)を削除
(function() {
  // Util
  // アップされた動画はPLAYER.mediaLengthとかPLAYER.getTimeが動かないCytubeのバグかな?
  // しょうがないので履歴に表示されてるmm:ssから終了時間を取得する(プレイリストはオフの場合がある)
  var getMediaLength = () => {
    var len = 0;
    var qeTime = document.getElementById('queue_history').getElementsByClassName('qe_time')[0];
    if (!qeTime) return 0;
    for (var a of qeTime.textContent.split(':')) {
      len = len * 60 + (a | 0);
    }
    return len;
  };
  // PLAYER.getTimeの代わりにPLAYER.player.currentTimeが使えるっぽい
  var getTime = () => new Promise(resolve => {
    if (PLAYER.player && PLAYER.player.currentTime) {
      resolve(PLAYER.player.currentTime());
    } else if (PLAYER.getTime) {
      PLAYER.getTime(resolve);
    } else {
      resolve(0);
    }
  });
  // ここからメイン
  var lastMediaId = '';
  var mediaTitle = '';
  var mediaLength = 0;
  var setPageTitle = async () => {
    if (lastMediaId != PLAYER.mediaId) {
      lastMediaId = PLAYER.mediaId;
      mediaTitle =
        document.getElementById('currenttitle')
        .textContent
        .replace(__('Currently Playing: '), '');
      mediaLength = (PLAYER.mediaLength || getMediaLength()); // 一応PLAYER.mediaLengthも見ておこ…
    }
    var time = await getTime();
    var rest = mediaLength - time;
    var mmss = (rest <= 0) ? '--:--' : ((rest / 60 | 0) + ':' + ('00' + (rest % 60 | 0)).slice(-2));
    var title = mmss + ' ' + mediaTitle + ' - ' + CHANNEL.opts.pagetitle;
    PAGETITLE = title;
    document.title = title;
  };
  clearInterval(window.GM_SET_PAGE_TITLE_TIMER);
  window.GM_SET_PAGE_TITLE_TIMER = setInterval(setPageTitle, 1000);
})();
`); // ← チャンネルのjsにセットするときはこの行も削除