From 2eab3c09563b17c95142d4437e009634acea6059 Mon Sep 17 00:00:00 2001 From: Rasz_pl Date: Mon, 18 Mar 2024 23:21:16 +0100 Subject: [PATCH 1/2] Update themes.js PREF f6 Cookie fix document.querySelector('ytd-masthead')?.setAttribute('dark'); oops --- .../web-accessible/www.youtube.com/themes.js | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/js&css/web-accessible/www.youtube.com/themes.js b/js&css/web-accessible/www.youtube.com/themes.js index d9f774e61..a302a504d 100644 --- a/js&css/web-accessible/www.youtube.com/themes.js +++ b/js&css/web-accessible/www.youtube.com/themes.js @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------*/ ImprovedTube.setTheme = function () { - let cookieValue = ''; + let darkCookie; switch(this.storage.theme) { case 'custom': @@ -88,9 +88,9 @@ ImprovedTube.setTheme = function () { case 'black': case 'dark': - cookieValue = '400'; + darkCookie = true; document.documentElement.setAttribute('dark', ''); - document.querySelector('ytd-masthead')?.setAttribute('dark'); + document.querySelector('ytd-masthead')?.setAttribute('dark', ''); document.querySelector('ytd-masthead')?.removeAttribute('style'); if (document.getElementById("cinematics")) { document.getElementById('cinematics').style.visibility = 'visible'; @@ -101,7 +101,7 @@ ImprovedTube.setTheme = function () { } break - case 'default': + case 'default': case 'dawn': case 'sunset': case 'night': @@ -115,5 +115,18 @@ ImprovedTube.setTheme = function () { break } - this.setPrefCookieValueByName('f6', cookieValue); + let cookie = this.getPrefCookieValueByName('f6'); + // f6 stores more than Theme. Treat it like hex number, we are only allowed to add/remove 0x80000 (light theme) and 0x400 (dark theme). + if (cookie && !isNaN(cookie)) { + // valid f6 + let negation = parseInt(cookie, 16) & parseInt(80400, 16); + cookie = (parseInt(cookie, 16) - negation); // remove 80000 and 400 + cookie = cookie ^ (darkCookie ? parseInt(400, 16) : 0); // apply optional darkCookie + cookie = cookie.toString(16); // back to hex + } else { + // missing or corrupted f6, fully overwrite + cookie = darkCookie ? 400 : null; + } + + this.setPrefCookieValueByName('f6', cookie); }; From 14210b7bac413629fa8b4dbf001a0999d029802d Mon Sep 17 00:00:00 2001 From: Rasz_pl Date: Mon, 18 Mar 2024 23:30:09 +0100 Subject: [PATCH 2/2] Update functions.js dont override cookie every time dont override cookie every time added getPrefCookieValueByName --- js&css/web-accessible/functions.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/js&css/web-accessible/functions.js b/js&css/web-accessible/functions.js index fc56c671b..a5b40d6db 100644 --- a/js&css/web-accessible/functions.js +++ b/js&css/web-accessible/functions.js @@ -497,24 +497,30 @@ ImprovedTube.getCookieValueByName = function (name) { } else return ''; }; -ImprovedTube.setPrefCookieValueByName = function (name, value) { +ImprovedTube.getPrefCookieValueByName = function (name) { let prefs = this.getParams(this.getCookieValueByName('PREF')); - let newprefs = ''; + return prefs[name]; +}; + +// set PREF cookie name=value or delete name if value == null +ImprovedTube.setPrefCookieValueByName = function (name, value) { + let originalPref = this.getCookieValueByName('PREF'); + let prefs = this.getParams(originalPref); + let newPrefs = ''; let ampersant = ''; - if (value) { - prefs[name] = value; - } + prefs[name] = value; for (let pref in prefs) { - if (pref) { - if (pref!=name || value) { - newprefs += ampersant + pref + '=' + prefs[pref]; - ampersant = '&'; - } + if (prefs[pref]) { + newPrefs += ampersant + pref + '=' + prefs[pref]; + ampersant = '&'; } } - this.setCookie('PREF', newprefs); + // only write cookie if its different from the old one + if (originalPref != newPrefs) { + this.setCookie('PREF', newPrefs); + } }; ImprovedTube.setCookie = function (name, value) {