fix helpers storage

This commit is contained in:
meow 2022-05-06 07:21:19 +03:00
parent 835237382f
commit fd890f9c0a
3 changed files with 20 additions and 22 deletions

View File

@ -171,7 +171,7 @@ window.helpers = window.helpers || {
*/
/**
* Universal storage proxy. Uses inside localStorage or cookies
* Universal storage, stores and returns JS objects. Uses inside localStorage or cookies
* @type {invidiousStorage}
*/
storage: (function () {
@ -181,8 +181,8 @@ window.helpers = window.helpers || {
if (localStorageIsUsable) {
return {
get: function (key) { return localStorage[key]; },
set: function (key, value) { localStorage[key] = value; },
get: function (key) { if (localStorage[key]) return JSON.parse(decodeURIComponent(localStorage[key])); },
set: function (key, value) { localStorage[key] = encodeURIComponent(JSON.stringify(value)); },
remove: function (key) { localStorage.removeItem(key); }
};
}
@ -193,26 +193,20 @@ window.helpers = window.helpers || {
const cookiePrefix = key + '=';
function findCallback(cookie) {return cookie.startsWith(cookiePrefix);}
const matchedCookie = document.cookie.split('; ').find(findCallback);
if (matchedCookie)
return matchedCookie.replace(cookiePrefix, '');
return null;
if (matchedCookie) {
const cookieBody = matchedCookie.replace(cookiePrefix, '');
if (cookieBody.length === 0) return;
return JSON.parse(decodeURIComponent(cookieBody));
}
},
set: function (key, value) {
const cookie_data = encodeURIComponent(JSON.stringify(value));
// Set expiration in 2 year
const date = new Date();
date.setTime(date.getTime() + 2*365.25*24*60*60);
date.setFullYear(date.getFullYear()+2);
const ip_regex = /^((\d+\.){3}\d+|[A-Fa-f0-9]*:[A-Fa-f0-9:]*:[A-Fa-f0-9:]+)$/;
let domain_used = location.hostname;
// Fix for a bug in FF where the leading dot in the FQDN is not ignored
if (domain_used.charAt(0) !== '.' && !ip_regex.test(domain_used) && domain_used !== 'localhost')
domain_used = '.' + location.hostname;
document.cookie = key + '=' + cookie_data + '; SameSite=Strict; path=/; domain=' +
domain_used + '; expires=' + date.toGMTString() + ';';
document.cookie = key + '=' + cookie_data + '; expires=' + date.toGMTString();
},
remove: function (key) {
document.cookie = key + '=; Max-Age=0';

View File

@ -48,7 +48,7 @@ function create_notification_stream(subscriptions) {
}
delivered.push(notification.videoId);
helpers.storage.set('notification_count', parseInt(helpers.storage.get('notification_count') || '0') + 1);
helpers.storage.set('notification_count', (helpers.storage.get('notification_count') || 0) + 1);
var notification_ticker = document.getElementById('notification_ticker');
if (parseInt(helpers.storage.get('notification_count')) > 0) {
@ -72,7 +72,12 @@ function handle_notification_error(event) {
}
addEventListener('load', function (e) {
helpers.storage.set('notification_count', document.getElementById('notification_count') ? document.getElementById('notification_count').innerText : '0');
var notification_count = document.getElementById('notification_count');
if (notification_count) {
helpers.storage.set('notification_count', parseInt(notification_count.innerText));
} else {
helpers.storage.set('notification_count', 0);
}
if (helpers.storage.get('stream')) {
helpers.storage.remove('stream');

View File

@ -432,7 +432,7 @@ function save_video_time(seconds) {
all_video_times[videoId] = seconds;
helpers.storage.set(save_player_pos_key, JSON.stringify(all_video_times));
helpers.storage.set(save_player_pos_key, all_video_times);
}
function get_video_time() {
@ -444,8 +444,7 @@ function get_video_time() {
}
function get_all_video_times() {
const raw = helpers.storage.get(save_player_pos_key);
return raw ? JSON.parse(raw) : {};
return helpers.storage.get(save_player_pos_key) || {};
}
function remove_all_video_times() {