Merge pull request #1692 from TeamPiped/localstorage

Add better error messages for localStorage disabled
This commit is contained in:
Kavin 2022-11-16 18:54:06 +00:00 committed by GitHub
commit 776a9e021c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 15 deletions

View File

@ -195,7 +195,7 @@ export default {
}, },
}); });
} else { } else {
this.handleLocalSubscriptions(this.channel.id); if (!this.handleLocalSubscriptions(this.channel.id)) return;
} }
this.subscribed = !this.subscribed; this.subscribed = !this.subscribed;
}, },

View File

@ -158,7 +158,11 @@ export default {
: [...new Set((this.getLocalSubscriptions() ?? []).concat(newChannels))]; : [...new Set((this.getLocalSubscriptions() ?? []).concat(newChannels))];
// Sort for better cache hits // Sort for better cache hits
subscriptions.sort(); subscriptions.sort();
try {
localStorage.setItem("localSubscriptions", JSON.stringify(subscriptions)); localStorage.setItem("localSubscriptions", JSON.stringify(subscriptions));
} catch (e) {
alert(this.$t("info.local_storage"));
}
}, },
}, },
}; };

View File

@ -137,8 +137,8 @@ export default {
shouldShowTrending(_this) { shouldShowTrending(_this) {
return _this.getPreferenceString("homepage", "trending") != "trending"; return _this.getPreferenceString("homepage", "trending") != "trending";
}, },
showSearchHistory() { showSearchHistory(_this) {
return localStorage.getItem("searchHistory") && localStorage.getItem("search_history"); return _this.getPreferenceBoolean("searchHistory", false) && localStorage.getItem("search_history");
}, },
}, },
methods: { methods: {

View File

@ -66,8 +66,8 @@ export default {
} }
}, },
onChange() { onChange() {
this.setPreference("shareWithTimeCode", this.withTimeCode); this.setPreference("shareWithTimeCode", this.withTimeCode, true);
this.setPreference("shareAsPipedLink", this.pipedLink); this.setPreference("shareAsPipedLink", this.pipedLink, true);
}, },
}, },
computed: { computed: {

View File

@ -372,13 +372,13 @@ export default {
}); });
videoEl.addEventListener("volumechange", () => { videoEl.addEventListener("volumechange", () => {
this.setPreference("volume", videoEl.volume); this.setPreference("volume", videoEl.volume, true);
}); });
videoEl.addEventListener("ratechange", e => { videoEl.addEventListener("ratechange", e => {
const rate = videoEl.playbackRate; const rate = videoEl.playbackRate;
if (rate > 0 && !isNaN(videoEl.duration) && !isNaN(videoEl.duration - e.timeStamp / 1000)) if (rate > 0 && !isNaN(videoEl.duration) && !isNaN(videoEl.duration - e.timeStamp / 1000))
this.setPreference("rate", rate); this.setPreference("rate", rate, true);
}); });
videoEl.addEventListener("ended", () => { videoEl.addEventListener("ended", () => {

View File

@ -383,7 +383,7 @@ export default {
return this.fetchJson(this.apiUrl() + "/comments/" + this.getVideoId()); return this.fetchJson(this.apiUrl() + "/comments/" + this.getVideoId());
}, },
onChange() { onChange() {
this.setPreference("autoplay", this.selectedAutoPlay); this.setPreference("autoplay", this.selectedAutoPlay, true);
}, },
async getVideoData() { async getVideoData() {
await this.fetchVideo() await this.fetchVideo()
@ -485,7 +485,7 @@ export default {
}, },
}); });
} else { } else {
this.handleLocalSubscriptions(this.channelId); if (!this.handleLocalSubscriptions(this.channelId)) return;
} }
this.subscribed = !this.subscribed; this.subscribed = !this.subscribed;
}, },

View File

@ -167,6 +167,7 @@
"preferences_note": "Note: preferences are saved in the local storage of your browser. Deleting your browser data will reset them.", "preferences_note": "Note: preferences are saved in the local storage of your browser. Deleting your browser data will reset them.",
"page_not_found": "Page not found", "page_not_found": "Page not found",
"copied": "Copied!", "copied": "Copied!",
"cannot_copy": "Can't copy!" "cannot_copy": "Can't copy!",
"local_storage": "This action requires localStorage, are cookies enabled?"
} }
} }

View File

@ -118,8 +118,12 @@ const mixin = {
purifyHTML(original) { purifyHTML(original) {
return DOMPurify.sanitize(original); return DOMPurify.sanitize(original);
}, },
setPreference(key, value) { setPreference(key, value, disableAlert = false) {
if (localStorage) localStorage.setItem(key, value); try {
localStorage.setItem(key, value);
} catch {
if (!disableAlert) alert(this.$t("info.local_storage"));
}
}, },
getPreferenceBoolean(key, defaultVal) { getPreferenceBoolean(key, defaultVal) {
var value; var value;
@ -204,7 +208,11 @@ const mixin = {
} }
}, },
getLocalSubscriptions() { getLocalSubscriptions() {
try {
return JSON.parse(localStorage.getItem("localSubscriptions")); return JSON.parse(localStorage.getItem("localSubscriptions"));
} catch {
return [];
}
}, },
isSubscribedLocally(channelId) { isSubscribedLocally(channelId) {
const localSubscriptions = this.getLocalSubscriptions(); const localSubscriptions = this.getLocalSubscriptions();
@ -218,7 +226,13 @@ const mixin = {
else localSubscriptions.push(channelId); else localSubscriptions.push(channelId);
// Sort for better cache hits // Sort for better cache hits
localSubscriptions.sort(); localSubscriptions.sort();
try {
localStorage.setItem("localSubscriptions", JSON.stringify(localSubscriptions)); localStorage.setItem("localSubscriptions", JSON.stringify(localSubscriptions));
return true;
} catch {
alert(this.$t("info.local_storage"));
}
return false;
}, },
getUnauthenticatedChannels() { getUnauthenticatedChannels() {
const localSubscriptions = this.getLocalSubscriptions() ?? []; const localSubscriptions = this.getLocalSubscriptions() ?? [];