Youtube Live Clock

show duration for livestreams and present time for archives

当前为 2024-12-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Live Clock
  3. // @name:zh-TW Youtube Live Clock
  4. // @namespace https://greasyfork.org/scripts/453367
  5. // @version 1.6.8
  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 liveBadge = null
  54. let timeDisplay = null
  55. let progressBar = null
  56.  
  57. const waitElements = () => {
  58. return new Promise((resolve) => {
  59. const observer = new MutationObserver(() => {
  60. liveBadge = $('.ytp-chrome-bottom .ytp-live-badge')
  61. timeDisplay = $('.ytp-chrome-bottom .ytp-time-display')
  62. progressBar = $('.ytp-chrome-bottom .ytp-progress-bar')
  63. if (liveBadge && timeDisplay && progressBar) {
  64. observer.disconnect()
  65. resolve()
  66. }
  67. })
  68. observer.observe(document.body, { attributes: false, childList: true, subtree: true })
  69. })
  70. }
  71.  
  72. const getLiveClock = () => {
  73. let clockElement = $('#present-time')
  74. if (!clockElement) {
  75. clockElement = document.createElement('span')
  76. clockElement.setAttribute('id', 'present-time')
  77. timeDisplay.insertBefore(clockElement, timeDisplay.childNodes[1])
  78. }
  79. return clockElement
  80. }
  81.  
  82. const updateLiveTime = () => {
  83. if (!$('#microformat script')) return ''
  84. let liveData = JSON.parse($('#microformat script').textContent)
  85. if (liveData.publication) {
  86. liveData = liveData.publication[0]
  87. const progressTime = progressBar.getAttribute('aria-valuenow')
  88. return liveData.endDate ? dateFormat(new Date(Date.parse(liveData.startDate) + progressTime * 1000)) : timeFormat(progressTime)
  89. } else return ''
  90. }
  91.  
  92. const main = async () => {
  93. await waitElements()
  94. liveBadge.style = 'margin-left: 10px'
  95. let liveClock = getLiveClock()
  96. const observer = new MutationObserver(() => { liveClock.textContent = updateLiveTime() })
  97. observer.observe(progressBar, { characterData: true, attributeFilter: ['aria-valuenow'] })
  98. }
  99.  
  100. document.addEventListener('yt-navigate-finish', (event) => {
  101. const url = event.detail.endpoint.commandMetadata.webCommandMetadata.url
  102. if (url.startsWith('/watch?v=') || url.startsWith('/live/')) main()
  103. })