From 2a45b4eba031cc59551150a7e674e5b5943ab490 Mon Sep 17 00:00:00 2001 From: bbielsa Date: Mon, 25 Oct 2021 20:59:36 -0400 Subject: [PATCH] Save and load the position for the video using a local storage object, the object is a dictionary, where the key is the video ID, and the value is the time at which the user last left off watching the video. If the user deselected the 'remember video position' checkbox in the preferences this dictionary is cleared --- assets/js/player.js | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/assets/js/player.js b/assets/js/player.js index a461c53d6..4c12f1fe8 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -38,6 +38,8 @@ embed_url.searchParams.delete('v'); short_url = location.origin + '/' + video_data.id + embed_url.search; embed_url = location.origin + '/embed/' + video_data.id + embed_url.search; +var remember_position_key = "remember_position"; + var shareOptions = { socials: ['fbFeed', 'tw', 'reddit', 'email'], @@ -199,6 +201,27 @@ if (video_data.premiere_timestamp && Math.round(new Date() / 1000) < video_data. player.getChild('bigPlayButton').hide(); } +if (video_data.params.remember_position) { + const remeberedTime = get_video_time(); + let lastUpdated = 0; + + set_seconds_after_start(remeberedTime); + + player.on("timeupdate", e => { + const raw = player.currentTime(); + const time = Math.floor(raw); + + if(lastUpdated !== time) { + save_video_time(time); + lastUpdated = time; + } + }); +} +else { + console.log("Removing data for remebered positions"); + remove_all_video_times(); +} + if (video_data.params.autoplay) { var bpb = player.getChild('bigPlayButton'); bpb.hide(); @@ -330,6 +353,55 @@ function skip_seconds(delta) { player.currentTime(newTime); } +function set_seconds_after_start(delta) { + const start = video_data.params.video_start; + player.currentTime(start + delta); +} + +function save_video_time(seconds) { + const videoId = video_data.id; + const all_video_times = get_all_video_times(); + + all_video_times[videoId] = seconds; + + set_all_video_times(all_video_times); +} + +function get_video_time() { + try { + const videoId = video_data.id; + const all_video_times = get_all_video_times(); + const timestamp = all_video_times[videoId]; + + return timestamp; + } + catch { + return 0; + } +} + +function set_all_video_times(times) { + const json = JSON.stringify(times); + + localStorage.setItem(remember_position_key, json); +} + +function get_all_video_times() { + try { + const raw = localStorage.getItem(remember_position_key); + const times = JSON.parse(raw); + + return times || {}; + } + catch { + return {}; + } +} + +function remove_all_video_times() { + localStorage.removeItem(remember_position_key); +} + function set_time_percent(percent) { const duration = player.duration(); const newTime = duration * (percent / 100);