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