Youtube Live Clock

顯示直播當下的時間

目前為 2022-11-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name               Youtube Live Clock
// @name:zh-TW         Youtube Live Clock
// @namespace          https://greasyfork.org/scripts/453367
// @version            1.4.1
// @description        show present time of the livestream
// @description:zh-TW  顯示直播當下的時間
// @author             Derek
// @match              *://www.youtube.com/*
// @grant              none
// ==/UserScript==

let $ = (element) => document.querySelector(element)
let $$ = (element) => document.querySelectorAll(element)
let twoDigit = (num) => {
  if (String(num).length === 2) return num
  else return '0' + String(num)
}
let formatTime = (time) => {
  let second = time % 60
  let minute = (time - second) % 3600 / 60
  let hour = (time - minute * 60 - second) / 3600

  if (time < 3600) return `${minute}:${twoDigit(second)}  `
  else return `${hour}:${twoDigit(minute)}:${twoDigit(second)}  `
}
let getClock = () => {
  let liveClock = $('.present-time')
  if (!liveClock) {
      let timeDisplay = $$('.ytp-time-display')
      let clockElement = document.createElement('span')
      clockElement.setAttribute('class', 'present-time')
      timeDisplay[timeDisplay.length - 1].childNodes[1].appendChild(clockElement)
      liveClock = $('.present-time')
  }
  return liveClock
}
let updateTime = () => {
  let isLive = JSON.parse($('.ytd-player-microformat-renderer').textContent).publication
  if (isLive && !$('.html5-ypc-overlay')) {
      let progressBar = $$('.ytp-progress-bar')
      let progressTime = progressBar[progressBar.length - 1].getAttribute('aria-valuenow')
      if (isLive[0].endDate) {
          let presentTime = new Date(Date.parse(isLive[0].startDate) + progressTime * 1000)
          let date = (presentTime).toString().split(' ')
          return ` (${date[3]}/${twoDigit(presentTime.getMonth() + 1)}/${date[2]} ${date[4]})  `
      } else return formatTime(progressTime)
  } else return ''
}

let main = () => {
  if (window.location.href.includes('/watch?v=')) {
    let liveClock = getClock()
    let liveBadge = $('.ytp-live-badge')
    liveBadge.style = 'margin-left: 10px'
    let progressBar = $('.ytp-progress-bar')
    let observer = new MutationObserver(() => { liveClock.textContent = updateTime() })
    observer.observe(progressBar, { attributes: true })
  }
}

document.addEventListener('yt-navigate-finish', main)