Youtube Live Clock

show duration for livestreams and present time for archives

  1. // ==UserScript==
  2. // @name Youtube Live Clock
  3. // @name:zh-TW Youtube Live Clock
  4. // @namespace https://greatest.deepsurf.us/scripts/453367
  5. // @version 1.7.3
  6. // @description show duration for livestreams and present time for archives
  7. // @description:zh-TW 顯示直播及直播存檔當下的時間
  8. // @author Derek
  9. // @match *://www.youtube.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. //you can choose your ideal date format by changing the FORMAT's value below
  14. const FORMAT = 1
  15. /*
  16. 1: 2022/10/31 06:37:10 (default)
  17. 2: 10/31/2022 06:37:10
  18. 3: 31/10/2022 06:37:10
  19. 4: Mon 31/10/2022 06:37:10
  20. 5: Monday 31/10/2022 06:37:10
  21. 6: Monday 31 October 2022 06:37:10
  22. */
  23.  
  24. const $ = (element) => document.querySelector(element)
  25.  
  26. const abbr = {
  27. week: { Sun: 'Sunday', Mon: 'Monday', Tue: 'Tuesday', Wed: 'Wednesday', Thu: 'Thursday', Fri: 'Friday', Sat: 'Saturday' },
  28. monthFull: { Jan: 'January', Feb: 'February', Mar: 'March', Apr: 'April', May: 'May', Jun: 'June', Jul: 'July', Aug: 'August', Sep: 'September', Oct: 'October', Nov: 'November', Dec: 'December' },
  29. month: { Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' }
  30. }
  31.  
  32. const twoDigit = (num) => num.toString().padStart(2, '0')
  33.  
  34. const timeFormat = (time) => {
  35. const second = time % 60
  36. const minute = Math.floor((time / 60) % 60)
  37. const hour = Math.floor(time / 3600)
  38. return hour > 0 ? `${hour}:${twoDigit(minute)}:${twoDigit(second)}` : `${minute}:${twoDigit(second)}`
  39. }
  40.  
  41. const dateFormat = (presentTime) => {
  42. const [week, month, day, year, time] = presentTime.toString().split(' ')
  43. return {
  44. 1: ` (${year}/${abbr.month[month]}/${day} ${time})`,
  45. 2: ` (${abbr.month[month]}/${day}/${year} ${time})`,
  46. 3: ` (${day}/${abbr.month[month]}/${year} ${time})`,
  47. 4: ` (${week} ${day}/${abbr.month[month]}/${year} ${time})`,
  48. 5: ` (${abbr.week[week]} ${day}/${abbr.month[month]}/${year} ${time})`,
  49. 6: ` (${abbr.week[week]} ${day} ${abbr.monthFull[month]} ${year} ${time})`
  50. } [FORMAT]
  51. }
  52.  
  53. let videoId, liveBadge, timeDisplay, progressBar, liveData, publication, observer
  54. let videoData = null
  55.  
  56. const waitElements = () => {
  57. return new Promise((resolve) => {
  58. const observer = new MutationObserver(() => {
  59. liveBadge = $('.ytp-chrome-bottom .ytp-live-badge')
  60. timeDisplay = $('.ytp-chrome-bottom .ytp-time-display')
  61. progressBar = $('.ytp-chrome-bottom .ytp-progress-bar')
  62.  
  63. if (liveBadge && timeDisplay && progressBar) {
  64. if (videoData !== $('#microformat script')) {
  65. videoData = $('#microformat script')
  66. observer.disconnect()
  67. resolve()
  68. }
  69. }
  70. })
  71. observer.observe(document.body, { attributes: false, childList: true, subtree: true })
  72. })
  73. }
  74.  
  75. const getLiveClock = () => {
  76. let clockElement = $('#present-time')
  77. if (!clockElement) {
  78. clockElement = document.createElement('span')
  79. clockElement.setAttribute('id', 'present-time')
  80. timeDisplay.insertBefore(clockElement, timeDisplay.childNodes[1])
  81. }
  82. return clockElement
  83. }
  84.  
  85. const updateLiveTime = () => {
  86. const progressTime = progressBar.getAttribute('aria-valuenow')
  87. return publication.endDate ? dateFormat(new Date(Date.parse(publication.startDate) + progressTime * 1000)) : timeFormat(progressTime)
  88. }
  89.  
  90. const main = async (vid) => {
  91. if (videoId === vid) return
  92. videoId = vid
  93.  
  94. if (observer) observer.disconnect()
  95. await waitElements()
  96.  
  97. liveData = JSON.parse(videoData.textContent)
  98. if (!liveData.publication) {
  99. if ($('#present-time')) $('#present-time').remove()
  100. return
  101. }
  102. publication = liveData.publication[0]
  103.  
  104. liveBadge.style = 'margin-left: 10px'
  105. let liveClock = getLiveClock()
  106. liveClock.textContent = updateLiveTime()
  107.  
  108. observer = new MutationObserver(() => { liveClock.textContent = updateLiveTime() })
  109. observer.observe(progressBar, { characterData: true, attributeFilter: ['aria-valuenow'] })
  110. }
  111.  
  112. document.addEventListener('yt-navigate-finish', (event) => {
  113. const url = event.detail.endpoint.commandMetadata.webCommandMetadata.url
  114. if (url.startsWith('/watch?v=') || url.startsWith('/live/')) main(url.match(/[A-z0-9_-]{11}/)[0])
  115. })