Default YouTube to Dark Theme

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Default YouTube to Dark Theme
// @version     0.2
// @description Makes YouTube default to its Dark Theme by initialising the relevant cookie
// @author      uxamend
// @namespace   https://greasyfork.org/en/users/231373-uxamend
// @match       *://*.youtube.com/*
// @grant       none
// @run-at      document-start
// @license     CC0-1.0
// ==/UserScript==

"use strict";

var c = document.cookie

// If there's no PREF cookie, then create one with a value that enables the Dark Theme
if(!c || !c.match("^PREF=|; ?PREF=")) {
  document.cookie = "PREF=f6=400;domain=youtube.com";
}

// We are only interested in setting the cookie if this is the first visit to YouTube since
// cookies were cleared/deleted.
// 
// If the cookie already exists, then we don't need or want to do anything.
//
// Overwriting an existing PREF cookie could risk data loss of other preferences, since we
// don't know how exactly preferences are expressed. All we know is that PREF=f6=400
// gives us YouTube's defaults except with Dark Theme enabled.