Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

themes Patch 5 #2107

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions js&css/web-accessible/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 18 additions & 5 deletions js&css/web-accessible/www.youtube.com/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
------------------------------------------------------------------------------*/

ImprovedTube.setTheme = function () {
let cookieValue = '';
let darkCookie;

switch(this.storage.theme) {
case 'custom':
Expand Down Expand Up @@ -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';
Expand All @@ -101,7 +101,7 @@ ImprovedTube.setTheme = function () {
}
break

case 'default':
case 'default':
case 'dawn':
case 'sunset':
case 'night':
Expand All @@ -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);
};