|
6 | 6 | // @supportURL https://github.com/0xdevalias/userscripts/issues
|
7 | 7 | // @downloadURL https://github.com/0xdevalias/userscripts/raw/main/userscripts/youtube-speed-override/youtube-speed-override.user.js
|
8 | 8 | // @namespace https://www.devalias.net/
|
9 |
| -// @version 1.2 |
| 9 | +// @version 1.3 |
10 | 10 | // @match https://www.youtube.com/*
|
11 | 11 | // @grant none
|
12 | 12 | // ==/UserScript==
|
|
28 | 28 | '#movie_player > .ytp-settings-menu'
|
29 | 29 | );
|
30 | 30 |
|
| 31 | +// const videoTimeDisplayCurrentSelector = '#movie_player .ytp-chrome-bottom .ytp-left-controls .ytp-time-display .ytp-time-current'; |
| 32 | + const videoTimeDisplayDurationSelector = '#movie_player .ytp-chrome-bottom .ytp-left-controls .ytp-time-display .ytp-time-duration'; |
| 33 | + |
31 | 34 | // HACK: Track this internally to prevent weird interactions with the default YouTube handler
|
32 | 35 | // let videoPlaybackRate = video.playbackRate;
|
33 | 36 |
|
|
117 | 120 | // }
|
118 | 121 | }
|
119 | 122 |
|
| 123 | + // Helper function to format time duration |
| 124 | + function formatDuration(durationInSeconds) { |
| 125 | + const hours = Math.floor(durationInSeconds / 3600); |
| 126 | + const minutes = Math.floor((durationInSeconds % 3600) / 60); |
| 127 | + const seconds = Math.floor(durationInSeconds % 60); |
| 128 | + |
| 129 | + let durationString = ''; |
| 130 | + if (hours > 0) { |
| 131 | + durationString += `${hours}:`; |
| 132 | + } |
| 133 | + durationString += `${(hours > 0 && minutes < 10) ? '0' : ''}${minutes}:${seconds.toString().padStart(2, '0')}`; |
| 134 | + |
| 135 | + return durationString; |
| 136 | + } |
| 137 | + |
| 138 | +// // Function to update the current time display with the adjusted time |
| 139 | +// function updateAdjustedCurrentTimeDisplay() { |
| 140 | +// if (video.playbackRate === 1) return; |
| 141 | + |
| 142 | +// const currentTimeSpan = document.querySelector(videoTimeDisplayCurrentSelector); |
| 143 | + |
| 144 | +// if (currentTimeSpan) { |
| 145 | +// const originalCurrentTime = formatDuration(video.currentTime); |
| 146 | +// const adjustedCurrentTime = formatDuration(video.currentTime / video.playbackRate); |
| 147 | +// currentTimeSpan.innerText = `${originalCurrentTime} (${adjustedCurrentTime} adjusted)`; |
| 148 | +// } |
| 149 | +// } |
| 150 | + |
| 151 | + // Function to update the time display with the adjusted length |
| 152 | + function updateAdjustedTimeDisplay() { |
| 153 | + const durationSpan = document.querySelector(videoTimeDisplayDurationSelector); |
| 154 | + |
| 155 | + if (durationSpan) { |
| 156 | + // const originalCurrentTime = formatDuration(video.currentTime); |
| 157 | + const adjustedCurrentTime = formatDuration(video.currentTime / video.playbackRate); |
| 158 | + |
| 159 | + const originaDuration = formatDuration(video.duration); |
| 160 | + const adjustedDuration = formatDuration(video.duration / video.playbackRate); |
| 161 | + durationSpan.innerText = `${originaDuration} (${adjustedCurrentTime} / ${adjustedDuration} adjusted for ${video.playbackRate}x playback rate)`; |
| 162 | + } |
| 163 | + } |
| 164 | + |
120 | 165 | // Override the default controls for > and <
|
121 | 166 | document.addEventListener(
|
122 | 167 | 'keydown',
|
|
162 | 207 | { capture: true }
|
163 | 208 | );
|
164 | 209 |
|
165 |
| - // When the video playbackRate changes, update the playback speed in the settings menu to match our custom override |
166 |
| - video.addEventListener('ratechange', updateSettingsMenu); |
| 210 | + video.addEventListener('ratechange', () => { |
| 211 | + // When the video playbackRate changes, update the playback speed in the settings menu to match our custom override |
| 212 | + updateSettingsMenu(); |
| 213 | + |
| 214 | + // When the video playbackRate changes, update the time display |
| 215 | + updateAdjustedTimeDisplay(); |
| 216 | + }); |
| 217 | + |
| 218 | + // Add event listener to update the time display adjusted based on playback rate |
| 219 | + // video.addEventListener('timeupdate', updateAdjustedCurrentTimeDisplay); |
| 220 | + video.addEventListener('durationupdate', updateAdjustedTimeDisplay); |
| 221 | + video.addEventListener('timeupdate', updateAdjustedTimeDisplay); |
167 | 222 |
|
168 | 223 | // When the settings menu changes, update the playback speed in the settings menu to match our custom override
|
169 | 224 | const settingsMenuObserver = new MutationObserver(updateSettingsMenu);
|
|
0 commit comments