Last active
April 27, 2021 01:04
-
-
Save danielrotaermel/49a3c6c007834a3c29cf03414db20292 to your computer and use it in GitHub Desktop.
userscript to dynamically set dark/light mode on google according to os settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Google Dynamic Dark Mode | |
// @description dynamically set dark/light mode on google according to os settings | |
// @author Daniel Rotaermel | |
// @match *://*google.com* | |
// ==/UserScript== | |
let shouldUseDarkmode = window.matchMedia('(prefers-color-scheme: dark)') | |
.matches; | |
let isUsingDarkmode = document | |
.querySelector('#logo > img') | |
.src.includes('light'); | |
function setDynamicDarkmode() { | |
console.log('trying to set dark/light mode'); | |
setTimeout(() => { | |
try { | |
if (shouldUseDarkmode && !isUsingDarkmode) { | |
console.log('dark mode'); | |
document | |
.querySelector('#searchform > div:nth-child(2) > div > div') | |
.click(); | |
} else if (!shouldUseDarkmode && isUsingDarkmode) { | |
document | |
.querySelector('#searchform > div:nth-child(2) > div > div') | |
.click(); | |
console.log('light mode mode'); | |
} | |
} catch { | |
setDynamicDarkmode(); | |
} | |
}, 10); | |
} | |
window.onload = setDynamicDarkmode; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment