Merge pull request #2744 from tirz/feature-fix_null_ptr_for_localStorage

fix: null ptr while retaining video time
This commit is contained in:
Samantaz Fox 2022-01-05 03:23:22 +01:00 committed by GitHub
commit edcc155482
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 19 deletions

View File

@ -59,6 +59,16 @@ videojs.Hls.xhr.beforeRequest = function(options) {
var player = videojs('player', 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/')) { if (location.pathname.startsWith('/embed/')) {
player.overlay({ player.overlay({
@ -386,25 +396,35 @@ function get_video_time() {
} }
function set_all_video_times(times) { function set_all_video_times(times) {
const json = JSON.stringify(times); if (storage) {
if (times) {
localStorage.setItem(save_player_pos_key, json); 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() { function get_all_video_times() {
try { if (storage) {
const raw = localStorage.getItem(save_player_pos_key); const raw = storage.getItem(save_player_pos_key);
const times = JSON.parse(raw); if (raw !== null) {
try {
return times || {}; return JSON.parse(raw);
} } catch (e) {
catch { console.debug('get_all_video_times: ' + e);
return {}; }
}
} }
return {};
} }
function remove_all_video_times() { function remove_all_video_times() {
localStorage.removeItem(save_player_pos_key); set_all_video_times(null);
} }
function set_time_percent(percent) { function set_time_percent(percent) {

View File

@ -11,7 +11,9 @@ toggle_theme.addEventListener('click', function () {
xhr.open('GET', url, true); xhr.open('GET', url, true);
set_mode(dark_mode); 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(); xhr.send();
}); });
@ -23,9 +25,12 @@ window.addEventListener('storage', function (e) {
}); });
window.addEventListener('DOMContentLoaded', function () { window.addEventListener('DOMContentLoaded', function () {
window.localStorage.setItem('dark_mode', document.getElementById('dark_mode_pref').textContent); const dark_mode = document.getElementById('dark_mode_pref').textContent;
// Update localStorage if dark mode preference changed on preferences page try {
update_mode(window.localStorage.dark_mode); // 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) { function scheme_switch (e) {
// ignore this method if we have a preference set // ignore this method if we have a preference set
if (localStorage.getItem('dark_mode')) { try {
return; if (localStorage.getItem('dark_mode')) {
} return;
}
} catch {}
if (e.matches) { if (e.matches) {
if (e.media.includes("dark")) { if (e.media.includes("dark")) {
set_mode(true); set_mode(true);