From 0453d08eedf8caa8c825efca028df63e9ec127ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Gaillard?= Date: Sun, 26 Dec 2021 13:45:27 +0100 Subject: [PATCH 1/2] fix: null ptr while retaining time --- assets/js/player.js | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/assets/js/player.js b/assets/js/player.js index 0cc4bab9..2608158b 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -59,6 +59,16 @@ videojs.Hls.xhr.beforeRequest = function(options) { var player = videojs('player', options); +const storage = (() => { + try { + if (localStorage.length !== -1) { + return localStorage; + } + } catch (e) { + console.info('No storage available: ' + e); + } + return undefined; +})(); if (location.pathname.startsWith('/embed/')) { player.overlay({ @@ -386,25 +396,35 @@ function get_video_time() { } function set_all_video_times(times) { - const json = JSON.stringify(times); - - localStorage.setItem(save_player_pos_key, json); + if (storage) { + if (times) { + try { + storage.setItem(save_player_pos_key, JSON.stringify(times)); + } catch (e) { + console.debug('set_all_video_times: ' + e); + } + } else { + storage.removeItem(save_player_pos_key); + } + } } function get_all_video_times() { - try { - const raw = localStorage.getItem(save_player_pos_key); - const times = JSON.parse(raw); - - return times || {}; - } - catch { - return {}; + if (storage) { + const raw = storage.getItem(save_player_pos_key); + if (raw !== null) { + try { + return JSON.parse(raw); + } catch (e) { + console.debug('get_all_video_times: ' + e); + } + } } + return {}; } function remove_all_video_times() { - localStorage.removeItem(save_player_pos_key); + set_all_video_times(null); } function set_time_percent(percent) { From 73a142fd9b778162c129af2da1c6bdbe9b8ed69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Gaillard?= Date: Sun, 26 Dec 2021 13:53:32 +0100 Subject: [PATCH 2/2] fix: null ptr while loading/switching theme --- assets/js/themes.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/assets/js/themes.js b/assets/js/themes.js index 543b849e..470f10bf 100644 --- a/assets/js/themes.js +++ b/assets/js/themes.js @@ -11,7 +11,9 @@ toggle_theme.addEventListener('click', function () { xhr.open('GET', url, true); set_mode(dark_mode); - window.localStorage.setItem('dark_mode', dark_mode ? 'dark' : 'light'); + try { + window.localStorage.setItem('dark_mode', dark_mode ? 'dark' : 'light'); + } catch {} xhr.send(); }); @@ -23,9 +25,12 @@ window.addEventListener('storage', function (e) { }); window.addEventListener('DOMContentLoaded', function () { - window.localStorage.setItem('dark_mode', document.getElementById('dark_mode_pref').textContent); - // Update localStorage if dark mode preference changed on preferences page - update_mode(window.localStorage.dark_mode); + const dark_mode = document.getElementById('dark_mode_pref').textContent; + try { + // Update localStorage if dark mode preference changed on preferences page + window.localStorage.setItem('dark_mode', dark_mode); + } catch {} + update_mode(dark_mode); }); @@ -37,9 +42,11 @@ lightScheme.addListener(scheme_switch); function scheme_switch (e) { // ignore this method if we have a preference set - if (localStorage.getItem('dark_mode')) { - return; - } + try { + if (localStorage.getItem('dark_mode')) { + return; + } + } catch {} if (e.matches) { if (e.media.includes("dark")) { set_mode(true);