Skip to content

Commit 5bb5f80

Browse files
committed
[userscript] youtube-speed-override: show time remaining + percentage complete when alt/option key is held
1 parent 5ff0187 commit 5bb5f80

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

userscripts/youtube-speed-override/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
```javascript
44
// ==UserScript==
55
// @name YouTube Speed Override
6-
// @description Override the default youtube controls for > and <
6+
// @description Override the default youtube controls for > and < and show remaining time when Option key is held
77
// @author Glenn 'devalias' Grant (devalias.net)
88
// @homepageURL https://github.com/0xdevalias/userscripts
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.5
12+
// @version 1.6
1313
// @match https://www.youtube.com/*
1414
// @grant GM_getValue
1515
// @grant GM_setValue

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

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// ==UserScript==
22
// @name YouTube Speed Override
3-
// @description Override the default youtube controls for > and <
3+
// @description Override the default youtube controls for > and < and show remaining time when Option key is held
44
// @author Glenn 'devalias' Grant (devalias.net)
55
// @homepageURL https://github.com/0xdevalias/userscripts
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.5
9+
// @version 1.6
1010
// @match https://www.youtube.com/*
1111
// @grant GM_getValue
1212
// @grant GM_setValue
@@ -19,12 +19,14 @@
1919
'use strict';
2020

2121
const LOG_PREFIX = '[userscript::youtube-speed-override]';
22+
2223
let DEBUG = GM_getValue('isDebugEnabled', false);
24+
let isAltKeyPressed = false;
2325

2426
function debugLog(...args) {
2527
if (!DEBUG) return
2628

27-
console.log(LOG_PREFIX, ...args);
29+
console.log(LOG_PREFIX, ...args, { DEBUG, isAltKeyPressed });
2830
}
2931

3032
function registerOrUpdateDebugMenuItem() {
@@ -86,6 +88,8 @@
8688
const videoTimeDisplayDurationSelector =
8789
'#movie_player .ytp-chrome-bottom .ytp-left-controls .ytp-time-display .ytp-time-duration';
8890

91+
const durationSpan = await waitForElement('#movie_player .ytp-chrome-bottom .ytp-left-controls .ytp-time-display .ytp-time-duration');
92+
8993
// HACK: Track this internally to prevent weird interactions with the default YouTube handler
9094
// let videoPlaybackRate = video.playbackRate;
9195

@@ -204,30 +208,48 @@
204208

205209
// Function to update the time display with the adjusted length
206210
function updateAdjustedTimeDisplay() {
207-
if (video.playbackRate === 1) return;
211+
if (!video.duration) return
212+
213+
// const originalCurrentTime = formatDuration(video.currentTime);
214+
const originalDuration = formatDuration(video.duration);
215+
216+
// If rate is 1x, only update if alt/option key is pressed, otherwise reset to default
217+
if (!isAltKeyPressed && video.playbackRate === 1) {
218+
// Avoid updating if it's already the same content (might prevent some mutation handlers from triggering?)
219+
if (durationSpan.innerText === originalDuration) return
220+
221+
durationSpan.innerText = originalDuration
222+
return;
223+
}
208224

209-
const durationSpan = document.querySelector(
210-
videoTimeDisplayDurationSelector,
225+
const adjustedCurrentTime = formatDuration(
226+
video.currentTime / video.playbackRate,
227+
);
228+
const adjustedDuration = formatDuration(
229+
video.duration / video.playbackRate,
211230
);
212231

213-
if (durationSpan) {
214-
// const originalCurrentTime = formatDuration(video.currentTime);
215-
const adjustedCurrentTime = formatDuration(
216-
video.currentTime / video.playbackRate,
217-
);
218-
219-
const originaDuration = formatDuration(video.duration);
220-
const adjustedDuration = formatDuration(
221-
video.duration / video.playbackRate,
222-
);
223-
durationSpan.innerText = `${originaDuration} (${adjustedCurrentTime} / ${adjustedDuration} adjusted for ${video.playbackRate}x playback rate)`;
232+
if (!isAltKeyPressed) {
233+
durationSpan.innerText = `${originalDuration} (${adjustedCurrentTime} / ${adjustedDuration} adjusted for ${video.playbackRate}x playback rate)`;
234+
} else {
235+
const remainingTime = video.duration - video.currentTime;
236+
const adjustedRemainingTime = formatDuration(remainingTime / video.playbackRate);
237+
const percentageComplete = ((video.currentTime / video.duration) * 100).toFixed(2);
238+
239+
durationSpan.innerText = `${originalDuration} (${adjustedRemainingTime} remaining / ${percentageComplete}% complete; adjusted for ${video.playbackRate}x playback rate)`;
224240
}
225241
}
226242

227243
// Override the default controls for > and <
228244
document.addEventListener(
229245
'keydown',
230246
(e) => {
247+
debugLog(`keydown event: ${e.key}`)
248+
249+
if (e.altKey) {
250+
isAltKeyPressed = true;
251+
}
252+
231253
// TODO: I think this still doesn't properly prevent the default YouTube code from running, so sometimes we increase by more than 0.25
232254
// This might be fixed since I added capture: true
233255
if (e.key === '>') {
@@ -273,6 +295,18 @@
273295
{ capture: true },
274296
);
275297

298+
document.addEventListener(
299+
'keyup',
300+
(e) => {
301+
debugLog(`keyup event: ${e.key}`)
302+
303+
if (!e.altKey) {
304+
isAltKeyPressed = false;
305+
}
306+
},
307+
{ capture: true },
308+
);
309+
276310
video.addEventListener('ratechange', () => {
277311
// When the video playbackRate changes, update the playback speed in the settings menu to match our custom override
278312
updateSettingsMenu();

0 commit comments

Comments
 (0)