Skip to content

Instantly share code, notes, and snippets.

@IdrisQe
Created December 29, 2022 07:29
Show Gist options
  • Select an option

  • Save IdrisQe/e4532285d916408acc217a49d56605bc to your computer and use it in GitHub Desktop.

Select an option

Save IdrisQe/e4532285d916408acc217a49d56605bc to your computer and use it in GitHub Desktop.
Automatic Google Dark Mode UserScript (v2.1)
// ==UserScript==
// @name Google Dark Mode Auto-Enable
// @description Make Google go dark automatically
// @version 2.1
// @author IdrisQe
// @match *://*.google.com/*
// @match *://*.google.ca/*
// @run-at document-start
// @inject-into page
// ==/UserScript==
let darkModeIsOn = false;
const timer = setInterval(tryDarkMode, 20);
function tryDarkMode() {
if (!darkModeIsOn) {
if (document.readyState === 'complete') {
try {
let darkModeButton = document.querySelector('#YUIDDb > div');
if (!darkModeButton) {
let radialButton = document.querySelector('g-radio-button-group > div[data-index="1"] > div:nth-child(2)');
if (radialButton) {
const radialButtonStyleMatrixArray = window.getComputedStyle(
document.querySelector('g-radio-button-group > div[data-index="1"] > div:nth-child(2)')).getPropertyValue('transform').match(/(-?[0-9\.]+)/g);
if (radialButtonStyleMatrixArray[0] === '0') {
darkModeIsOn = true;
document.querySelector('g-radio-button-group > div[data-index="1"]').click();
}
}
} else if (darkModeButton.textContent === 'Dark theme: Off') {
darkModeIsOn = true;
darkModeButton.click();
}
} catch {
return;
}
}
} else {
clearInterval(timer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment