Skip to content

Instantly share code, notes, and snippets.

@jrunning
Last active July 19, 2021 17:20
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 jrunning/f083d0023822e23528cd015374c9acf2 to your computer and use it in GitHub Desktop.
Save jrunning/f083d0023822e23528cd015374c9acf2 to your computer and use it in GitHub Desktop.
Bookmarklet to remove dark mode styles from a web page

Copy the following URL to a bookmark and give it a cheeky name like "Let there be light":

javascript:(()=>{let e=(e,s)=>{let l=0;for(let o=0;o<s.length&&!(++l>1e4);o++)t(e,o,s[o])},t=(t,s,l)=>{"conditionText"in l&&(/prefers-color-scheme:\s*dark/.test(l.conditionText)?t.deleteRule(s):e(l,l.cssRules))};for(let t of document.styleSheets)try{e(t,t.cssRules)}catch(e){}})();
let handleRules = (owner, rules) => {
for (let i = 0; i < rules.length; i++) {
handleRule(owner, i, rules[i]);
}
}
let handleRule = (owner, idx, rule) => {
if ('conditionText' in rule) {
if (/prefers-color-scheme:\s*dark/.test(rule.conditionText)) {
owner.deleteRule(idx);
} else {
handleRules(rule, rule.cssRules);
}
}
}
for (let ss of document.styleSheets) {
try {
handleRules(ss, ss.cssRules);
} catch (err) {
}
}
function handleRules(owner, rules) {
let iter = 0;
for (let i = 0; i < rules.length; i++) {
iter++;
if (iter > 1000) {
console.log('oops, stopping');
break;
}
handleRule(owner, i, rules[i]);
}
}
function handleRule(owner, idx, rule) {
if (rule instanceof CSSMediaRule) {
if (/prefers-color-scheme:\s*dark/.test(rule.conditionText)) {
console.log('deleting', idx, rule.conditionText);
owner.deleteRule(idx);
} else {
console.log('recursing', idx, rule.conditionText);
handleRules(rule, rule.cssRules);
}
}
}
function handleStyleSheets(doc) {
for (let ss of document.styleSheets) {
try {
handleRules(ss, ss.cssRules);
} catch (err) {
console.log(`couldn't get rules from ${ss.href}`);
}
}
}
handleStyleSheets(document.styleSheets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment