Youtube live play uptime counter

Shows the play position of the video from the start of a livestream. When watching at the live position, this should be the uptime.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name     Youtube live play uptime counter
// @description Shows the play position of the video from the start of a livestream. When watching at the live position, this should be the uptime.
// @author Portablejim
// @namespace https://github.com/portablejim/GMScripts
// @version  3
// @license      GPL
// @grant    none
// @match https://www.youtube.com/watch*
// @match https://youtube.com/watch*
// @match https://www.youtube.com/live/*
// @match https://youtube.com/live/*
// ==/UserScript==

(function() {
    'use strict';

    let attach = () =>
    {
      console.log('Uptime counter: Init.');
      if (document.querySelector('.ytp-live-badge') && document.querySelector('.ytp-live-badge').getAttribute('disabled') !== 'true')
      {
        let videoEl = document.querySelector('video');
        let intervalId = setInterval(() => {
          ((time) => { 
            let liveUptimeEl = document.getElementById('liveUptime');
            if(!liveUptimeEl)
            {
                // try to create the element
                console.log('Uptime counter: Trying to add live uptime element');
                let headingEl = document.querySelector('#title h1');
                if(headingEl)
                {
                    console.log('Uptime counter: Heading found');
                    let timeCodeEl = document.createElement('p');
                    timeCodeEl.id = 'liveUptime';
                    timeCodeEl.style.display = 'inline-block';
                    timeCodeEl.style.marginInlineStart = '16px';
                    timeCodeEl.timeoutId = intervalId;
                    headingEl.appendChild(timeCodeEl);
                    liveUptimeEl = timeCodeEl;
                }
            }

            if(liveUptimeEl)
            {
                const SECONDS_IN_HOUR = 60*60;
                if(time >= SECONDS_IN_HOUR)
                {
                    liveUptimeEl.innerText = '[From start: ' + Math.floor(time/SECONDS_IN_HOUR).toFixed(0) + ':' + Math.floor((time/60)%60).toFixed(0).padStart(2, '0') + ':' + Math.floor(time % 60).toFixed(0).padStart(2, '0') + ']'; 
                }
                else
                {
                    liveUptimeEl.innerText = '[From start: ' + Math.floor(time/60).toFixed(0) + ':' + Math.floor(time % 60).toFixed(0).padStart(2, '0') + ']'; 
                }
            }
          })(videoEl.currentTime);
        }, 200);
      }
      else if(!document.querySelector('h1'))
      {
        console.log('Uptime counter: No heading yet, wait.');
        setTimeout(attach, 1000);
      }
      else
      {
        console.log('Uptime counter: Not a live video');
      }
    };
    setTimeout(attach, 1000);
})();