From 6b9151e47f9ec28ab8c8463cbf31c065b8f3ac68 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 7 Nov 2022 10:20:13 +0000 Subject: [PATCH 1/2] Add better error messages for localStorage disabled. --- src/components/ChannelPage.vue | 2 +- src/components/ImportPage.vue | 6 +++++- src/components/NavBar.vue | 4 ++-- src/components/WatchVideo.vue | 2 +- src/locales/en.json | 3 ++- src/main.js | 20 +++++++++++++++++--- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/components/ChannelPage.vue b/src/components/ChannelPage.vue index 93a0e055..64cdb27f 100644 --- a/src/components/ChannelPage.vue +++ b/src/components/ChannelPage.vue @@ -195,7 +195,7 @@ export default { }, }); } else { - this.handleLocalSubscriptions(this.channel.id); + if (!this.handleLocalSubscriptions(this.channel.id)) return; } this.subscribed = !this.subscribed; }, diff --git a/src/components/ImportPage.vue b/src/components/ImportPage.vue index 2af7cd6a..9e598018 100644 --- a/src/components/ImportPage.vue +++ b/src/components/ImportPage.vue @@ -158,7 +158,11 @@ export default { : [...new Set((this.getLocalSubscriptions() ?? []).concat(newChannels))]; // Sort for better cache hits subscriptions.sort(); - localStorage.setItem("localSubscriptions", JSON.stringify(subscriptions)); + try { + localStorage.setItem("localSubscriptions", JSON.stringify(subscriptions)); + } catch (e) { + alert(this.$t("info.local_storage")); + } }, }, }; diff --git a/src/components/NavBar.vue b/src/components/NavBar.vue index 6a046e1d..2b810a22 100644 --- a/src/components/NavBar.vue +++ b/src/components/NavBar.vue @@ -137,8 +137,8 @@ export default { shouldShowTrending(_this) { return _this.getPreferenceString("homepage", "trending") != "trending"; }, - showSearchHistory() { - return localStorage.getItem("searchHistory") && localStorage.getItem("search_history"); + showSearchHistory(_this) { + return _this.getPreferenceBoolean("searchHistory", false) && localStorage.getItem("search_history"); }, }, methods: { diff --git a/src/components/WatchVideo.vue b/src/components/WatchVideo.vue index 91c95f1f..adbbb66a 100644 --- a/src/components/WatchVideo.vue +++ b/src/components/WatchVideo.vue @@ -485,7 +485,7 @@ export default { }, }); } else { - this.handleLocalSubscriptions(this.channelId); + if (!this.handleLocalSubscriptions(this.channelId)) return; } this.subscribed = !this.subscribed; }, diff --git a/src/locales/en.json b/src/locales/en.json index ba56ead5..fab0ced3 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -167,6 +167,7 @@ "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", "copied": "Copied!", - "cannot_copy": "Can't copy!" + "cannot_copy": "Can't copy!", + "local_storage": "This action requires localStorage, are cookies enabled?" } } diff --git a/src/main.js b/src/main.js index e5126cc3..c4596ac0 100644 --- a/src/main.js +++ b/src/main.js @@ -119,7 +119,11 @@ const mixin = { return DOMPurify.sanitize(original); }, setPreference(key, value) { - if (localStorage) localStorage.setItem(key, value); + try { + localStorage.setItem(key, value); + } catch { + alert(this.$t("info.local_storage")); + } }, getPreferenceBoolean(key, defaultVal) { var value; @@ -204,7 +208,11 @@ const mixin = { } }, getLocalSubscriptions() { - return JSON.parse(localStorage.getItem("localSubscriptions")); + try { + return JSON.parse(localStorage.getItem("localSubscriptions")); + } catch { + return []; + } }, isSubscribedLocally(channelId) { const localSubscriptions = this.getLocalSubscriptions(); @@ -218,7 +226,13 @@ const mixin = { else localSubscriptions.push(channelId); // Sort for better cache hits localSubscriptions.sort(); - localStorage.setItem("localSubscriptions", JSON.stringify(localSubscriptions)); + try { + localStorage.setItem("localSubscriptions", JSON.stringify(localSubscriptions)); + return true; + } catch { + alert(this.$t("info.local_storage")); + } + return false; }, getUnauthenticatedChannels() { const localSubscriptions = this.getLocalSubscriptions() ?? []; From adf5f7da6416b0e09839057cd10197105eb6da3d Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Wed, 16 Nov 2022 18:51:56 +0000 Subject: [PATCH 2/2] Don't show alerts in some scenarios. --- src/components/ShareModal.vue | 4 ++-- src/components/VideoPlayer.vue | 4 ++-- src/components/WatchVideo.vue | 2 +- src/main.js | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/ShareModal.vue b/src/components/ShareModal.vue index 17429913..10b6c74d 100644 --- a/src/components/ShareModal.vue +++ b/src/components/ShareModal.vue @@ -66,8 +66,8 @@ export default { } }, onChange() { - this.setPreference("shareWithTimeCode", this.withTimeCode); - this.setPreference("shareAsPipedLink", this.pipedLink); + this.setPreference("shareWithTimeCode", this.withTimeCode, true); + this.setPreference("shareAsPipedLink", this.pipedLink, true); }, }, computed: { diff --git a/src/components/VideoPlayer.vue b/src/components/VideoPlayer.vue index d85c44ca..57b5ede1 100644 --- a/src/components/VideoPlayer.vue +++ b/src/components/VideoPlayer.vue @@ -372,13 +372,13 @@ export default { }); videoEl.addEventListener("volumechange", () => { - this.setPreference("volume", videoEl.volume); + this.setPreference("volume", videoEl.volume, true); }); videoEl.addEventListener("ratechange", e => { const rate = videoEl.playbackRate; if (rate > 0 && !isNaN(videoEl.duration) && !isNaN(videoEl.duration - e.timeStamp / 1000)) - this.setPreference("rate", rate); + this.setPreference("rate", rate, true); }); videoEl.addEventListener("ended", () => { diff --git a/src/components/WatchVideo.vue b/src/components/WatchVideo.vue index adbbb66a..e436d4f0 100644 --- a/src/components/WatchVideo.vue +++ b/src/components/WatchVideo.vue @@ -383,7 +383,7 @@ export default { return this.fetchJson(this.apiUrl() + "/comments/" + this.getVideoId()); }, onChange() { - this.setPreference("autoplay", this.selectedAutoPlay); + this.setPreference("autoplay", this.selectedAutoPlay, true); }, async getVideoData() { await this.fetchVideo() diff --git a/src/main.js b/src/main.js index c4596ac0..fb15e695 100644 --- a/src/main.js +++ b/src/main.js @@ -118,11 +118,11 @@ const mixin = { purifyHTML(original) { return DOMPurify.sanitize(original); }, - setPreference(key, value) { + setPreference(key, value, disableAlert = false) { try { localStorage.setItem(key, value); } catch { - alert(this.$t("info.local_storage")); + if (!disableAlert) alert(this.$t("info.local_storage")); } }, getPreferenceBoolean(key, defaultVal) {