Skip to content

Instantly share code, notes, and snippets.

@sillyslux
Created January 9, 2020 15:04
Show Gist options
  • Save sillyslux/ef8314473af358952215aa46fa9ac602 to your computer and use it in GitHub Desktop.
Save sillyslux/ef8314473af358952215aa46fa9ac602 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name jsfiddle auto dark switch
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @include https://jsfiddle.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const e = document.querySelector("#editor-theme");
const isDark = window.matchMedia('(prefers-color-scheme: dark)');
if(isDark.matches) {
e.href = e.href.replace(/editor-.*?.css/, "editor-dark.css");
document.head.insertAdjacentHTML('beforeend', '<link href="https://jsfiddle.net/css/dist-skeleton-dark.css?update_06_08_2019_1" id="skeleton-theme" rel="stylesheet" type="text/css">');
} else {
e.href = e.href.replace(/editor-.*?.css/, "editor-light.css");
document.head.insertAdjacentHTML('beforeend', '<link href="https://jsfiddle.net/css/dist-skeleton-light.css?update_06_08_2019_1" id="skeleton-theme" rel="stylesheet" type="text/css">');
}
const s = document.querySelector("#skeleton-theme");
isDark.addListener((evt) => {
if(evt.matches && evt.media.includes('dark')){
if (e) e.href = e.href.replace(/editor-.*?.css/, "editor-dark.css");
if (s) s.href = s.href.replace(/skeleton-.*?.css/, "skeleton-dark.css");
} else {
if (e) e.href = e.href.replace(/editor-.*?.css/, "editor-light.css");
if (s) s.href = s.href.replace(/skeleton-.*?.css/, "skeleton-light.css");
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment