Youtube Live Clock

Show present time of the livestream!

当前为 2022-10-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube Live Clock
  3. // @namespace https://greasyfork.org/scripts/453367
  4. // @version 1.3.2
  5. // @description Show present time of the livestream!
  6. // @author Derek
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // @noframes
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict'
  14.  
  15. let twoDigit = (num) => {
  16. if (String(num).length === 2) return num
  17. else return '0' + String(num)
  18. }
  19. let formatTime = (time) => {
  20. let second = time % 60
  21. let minute = (time - second) % 3600 / 60
  22. let hour = (time - minute * 60 - second) / 3600
  23. let day = (time - hour * 3600 - minute * 60 - second) / 86400
  24.  
  25. if (time < 3600) return `${minute}:${twoDigit(second)} `
  26. else if (3600 <= time < 86400) return `${hour}:${twoDigit(minute)}:${twoDigit(second)} `
  27. else return `${day}:${twoDigit(hour)}:${twoDigit(minute)}:${twoDigit(second)} `
  28. }
  29. let getClock = () => {
  30. let liveClock = document.querySelector('.present-time')
  31. if (!liveClock) {
  32. let timeDisplay = document.querySelector('.ytp-time-display').childNodes[1]
  33. let clockElement = document.createElement('span')
  34. clockElement.setAttribute('class', 'present-time')
  35. timeDisplay.appendChild(clockElement)
  36. liveClock = document.querySelector('.present-time')
  37. }
  38. return liveClock
  39. }
  40. let updateTime = () => {
  41. let isLive = JSON.parse(document.querySelector('.ytd-player-microformat-renderer').textContent).publication
  42. if (isLive) {
  43. let progressTime = document.querySelector('.ytp-progress-bar').getAttribute('aria-valuenow')
  44. if (isLive[0].endDate) {
  45. let presentTime = new Date(Date.parse(isLive[0].startDate) + progressTime * 1000)
  46. let date = (presentTime).toString().split(' ')
  47. return ` (${date[3]}/${twoDigit(presentTime.getMonth() + 1)}/${date[2]} ${date[4]}) `
  48. } else return formatTime(progressTime)
  49. } else return ''
  50. }
  51.  
  52. let main = () => {
  53. setTimeout(() => {
  54. let liveClock = getClock()
  55. let progressBar = document.querySelector('.ytp-progress-bar')
  56. let observer = new MutationObserver(() => { liveClock.textContent = updateTime() })
  57. observer.observe(progressBar, {attributes: true})
  58. }, 1500)
  59. }
  60.  
  61. document.addEventListener('yt-navigate-finish', main)
  62. })();