Added support for watch progress for the session - #184
Conversation
|
Since the persistent version would have to be done quite differently, would prefer we just do that first. I wrote out an implementation plan here if you or anyone else would like to give it a shot: |
|
@user234683 Sorry for the delay. I forgot about this. I would like to implement it. Probably start on this weekend. I'll keep you posted. Thanks! |
PR Reviewer Guide 🔍
|
There was a problem hiding this comment.
Pull request overview
Adds session-scoped “resume playback” support by persisting each video’s last watched timestamp in sessionStorage, so a video can continue from where it was left off within the same browser session.
Changes:
- Save
video.currentTimetosessionStorageon page unload. - Restore saved time on
loadedmetadatato resume playback position.
Comments suppressed due to low confidence (4)
youtube/static/js/watch.js:217
video_idis assigned withoutvar/let/const, which creates an implicit global variable. This can lead to hard-to-debug collisions and differs from the rest of the file’s variable declarations; declare it explicitly (and ideally make itconst).
// save watched time to session storage
video_id = window.location.href.split("?v=")[1].split("&")[0]
youtube/static/js/watch.js:227
- Restoring
video.currentTimeonloadedmetadatawill override the existing&tstart-time behavior (data.time_startis applied earlier). If a user opens a link with?t=..., they likely expect that timestamp to take precedence over the saved session resume time; consider only restoring whendata.time_startis 0 (or otherwise define/implement precedence explicitly).
video.addEventListener('loadedmetadata', function () {
if (sessionStorage.getItem(video_id) !== null) {
const prevWatchTime = sessionStorage.getItem(video_id);
if (video.duration > prevWatchTime && prevWatchTime > 0) {
video.currentTime = prevWatchTime;
}
youtube/static/js/watch.js:227
sessionStorage.getItem()returns a string, butprevWatchTimeis used in numeric comparisons and assigned tovideo.currentTime. Convert it once (e.g.,Number(...)/parseFloat(...)) and validate it’s finite before comparing/assigning to avoid relying on implicit coercion.
if (sessionStorage.getItem(video_id) !== null) {
const prevWatchTime = sessionStorage.getItem(video_id);
if (video.duration > prevWatchTime && prevWatchTime > 0) {
video.currentTime = prevWatchTime;
}
youtube/static/js/watch.js:220
- Saving progress only in a
beforeunloadhandler is unreliable on some browsers (and can be skipped with bfcache/tab-kill scenarios). Consider also persisting onpagehideand/orvisibilitychange(and/or throttledtimeupdate) so watch progress is saved more consistently without depending on unload.
window.addEventListener('beforeunload', function(e) {
sessionStorage.setItem(video_id, video.currentTime);
});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| video_id = window.location.href.split("?v=")[1].split("&")[0] | ||
| window.addEventListener('beforeunload', function(e) { | ||
| sessionStorage.setItem(video_id, video.currentTime); | ||
| }); | ||
|
|
||
| video.addEventListener('loadedmetadata', function () { | ||
| if (sessionStorage.getItem(video_id) !== null) { |
Outcome
This PR is to save the watched video duration for various videos, so that they can resume playing from where they left off.
In the current implementation, I am saving time_stamps in session storage, so these persist only for the current session.
I am expecting it to make persistent in future PRs.
Demo
demo video