Skip to content

Instantly share code, notes, and snippets.

@13xforever
Last active February 1, 2023 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13xforever/a33541b685d9e17ae61bc479eab4580b to your computer and use it in GitHub Desktop.
Save 13xforever/a33541b685d9e17ae61bc479eab4580b to your computer and use it in GitHub Desktop.
Greasmonkey/Tampermonkey user script to sync Twitter Light/Dark theme with OS
// ==UserScript==
// @name Twitter Auto Theme
// @version 0.2
// @description Automatically switches between light and dark themes on Twitter.com (web version) based on system settings. Based on https://greasyfork.org/en/scripts/452575-twitter-auto-theme/code
// @author 13xforever
// @match https://twitter.com/*
// @run-at document-start
// @license MIT
// ==/UserScript==
(function() {
"use strict";
if (!window.matchMedia) {
return;
}
const LIGHT = "0";
const DIM = "1";
const DARK = "2";
function checkMode(q) {
let currentNightMode = document.cookie.split(";").find(cookie => cookie.includes("night_mode")).split("=")[1];
let newNightMode = q.matches ? DIM : LIGHT;
if (newNightMode !== currentNightMode) {
const MAX_AGE = 365 * 24 * 60 * 60;
document.cookie = `night_mode=${newNightMode}; domain=.twitter.com; secure; max-age=${MAX_AGE}`;
}
}
const query = window.matchMedia('(prefers-color-scheme: dark)');
checkMode(query);
query.addEventListener('change', checkMode);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment