Default YouTube to Dark Theme

Makes YouTube default to its Dark Theme by initialising the relevant cookie

Author
uxamend
Daily installs
0
Total installs
452
Ratings
3 0 0
Version
0.2
Created
2020-08-10
Updated
2020-08-10
License
CC0-1.0
Applies to

Background

If you regularly clear your cookies, or limit their lifespan, then YouTube will keep forgetting that you chose to use the Dark Theme. You might find this annoying and wish for just that one piece of information to be remembered. You might want to use a userscript to achieve this.

Unmaintained

YouTube has rendered this script unnecessary for me, because they added the option to "Use device theme" and made it the default. In my case, this achieves the same thing as running this script, so I have stopped using the script and therefore I am no longer testing whether it still functions correctly. There might still be cases where this script is useful, but consider it unmaintained.

How it works

At document-start, the script checks to see whether the PREF cookie for youtube.com exists. If it exists, then the script does nothing, on the assumption that your preferences are as you left them. If the PREF cookie does not exist, then the script assumes that cookies have been cleared/deleted at some point, and it initialises the PREF cookie with the value f6=400, which seems to correspond to enabling the Dark Theme.

And that's it. There are other scripts that force-enable the Dark Theme, but they all seem to do it by navigating the user interface to press the toggle button that enables/disables the Dark Theme. I think setting the cookie is a more elegant solution.

But I was trying to avoid cookies, and now you're auto-generating one when I visit YouTube!

If you turn on the Dark Theme by pressing the toggle switch in the menu, then YouTube is going to set a cookie. That's how the setting persists across multiple open tabs. It's what you might call an "essential cookie" in GDPR-speak. It's fundamental to providing the functionality. This script just cuts out the middle man that is YouTube's user interface.

(I suspect most of you already know this, though.)

Version history

0.1. First release. 0.2. Correction to logic to determine whether PREF cookie exists.