Skip to content

Commit 9d2aae8

Browse files
committed
[userscript] youtube-speed-override: show adjusted duration based on playbackRate
1 parent 611d36a commit 9d2aae8

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

userscripts/youtube-speed-override/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// @supportURL https://github.com/0xdevalias/userscripts/issues
1010
// @downloadURL https://github.com/0xdevalias/userscripts/raw/main/userscripts/youtube-speed-override/youtube-speed-override.user.js
1111
// @namespace https://www.devalias.net/
12-
// @version 1.2
12+
// @version 1.3
1313
// @match https://www.youtube.com/*
1414
// @grant none
1515
// ==/UserScript==

userscripts/youtube-speed-override/youtube-speed-override.user.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// @supportURL https://github.com/0xdevalias/userscripts/issues
77
// @downloadURL https://github.com/0xdevalias/userscripts/raw/main/userscripts/youtube-speed-override/youtube-speed-override.user.js
88
// @namespace https://www.devalias.net/
9-
// @version 1.2
9+
// @version 1.3
1010
// @match https://www.youtube.com/*
1111
// @grant none
1212
// ==/UserScript==
@@ -28,6 +28,9 @@
2828
'#movie_player > .ytp-settings-menu'
2929
);
3030

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+
3134
// HACK: Track this internally to prevent weird interactions with the default YouTube handler
3235
// let videoPlaybackRate = video.playbackRate;
3336

@@ -117,6 +120,48 @@
117120
// }
118121
}
119122

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+
120165
// Override the default controls for > and <
121166
document.addEventListener(
122167
'keydown',
@@ -162,8 +207,18 @@
162207
{ capture: true }
163208
);
164209

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);
167222

168223
// When the settings menu changes, update the playback speed in the settings menu to match our custom override
169224
const settingsMenuObserver = new MutationObserver(updateSettingsMenu);

0 commit comments

Comments
 (0)