solkaz / YouTube Dark Auto-Enable

// ==UserScript==
// @name     YouTube Dark Auto-Enable
// @description Auto-enables YouTube Dark theme (https://support.google.com/youtube/answer/7385323)
// @version  1
// @grant    none
// @match    *://*.youtube.com/*
// @noframes
// @run-at document-start
// @require https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js
// @copyright 2017, solkaz (https://openuserjs.org/users/solkaz)
// @license MIT
// ==/UserScript==

const decodePREF = (cookieValue) =>
  new Map(cookieValue.split('&').map(value => value.split('=')));

const encodePREF = prefsMap => {
  let encoded = '';
  prefsMap.forEach((value, key) => {
    encoded = encoded.concat(`${key}=${value}&`);
  });
  return encoded.substring(0, encoded.length - 1);
};

const prefsMap = decodePREF(Cookies.get('PREF') || '');

if (!prefsMap.get('f6')) {
  prefsMap.set('f6', 400);
  Cookies.set('PREF', encodePREF(prefsMap), {
    domain: '.youtube.com'
  });
}