Greasy Fork 还支持 简体中文。

Youtube Live Clock

顯示直播及直播存檔當下的時間

目前為 2024-12-09 提交的版本,檢視 最新版本

  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.9
  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 videoData = null
  55. let timeDisplay = null
  56. let progressBar = null
  57.  
  58. const waitElements = () => {
  59. return new Promise((resolve) => {
  60. const observer = new MutationObserver(() => {
  61. videoData = $('#microformat script')
  62. liveBadge = $('.ytp-chrome-bottom .ytp-live-badge')
  63. timeDisplay = $('.ytp-chrome-bottom .ytp-time-display')
  64. progressBar = $('.ytp-chrome-bottom .ytp-progress-bar')
  65.  
  66. if (liveBadge && timeDisplay && progressBar && videoData) {
  67. observer.disconnect()
  68. resolve()
  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. let liveData = JSON.parse(videoData.textContent)
  87. if (!liveData.publication) return ''
  88.  
  89. liveData = liveData.publication[0]
  90. const progressTime = progressBar.getAttribute('aria-valuenow')
  91. return liveData.endDate ? dateFormat(new Date(Date.parse(liveData.startDate) + progressTime * 1000)) : timeFormat(progressTime)
  92. }
  93.  
  94. const main = async () => {
  95. await waitElements()
  96. liveBadge.style = 'margin-left: 10px'
  97. let liveClock = getLiveClock()
  98. liveClock.textContent = updateLiveTime()
  99. const observer = new MutationObserver(() => { liveClock.textContent = updateLiveTime() })
  100. observer.observe(progressBar, { characterData: true, attributeFilter: ['aria-valuenow'] })
  101. }
  102.  
  103. document.addEventListener('yt-navigate-finish', (event) => {
  104. const url = event.detail.endpoint.commandMetadata.webCommandMetadata.url
  105. if (url.startsWith('/watch?v=') || url.startsWith('/live/')) main()
  106. })