Last active
February 4, 2023 06:38
-
-
Save 13xforever/8ff0b7106c8b3c3eff0e13ca6fb471ab to your computer and use it in GitHub Desktop.
Tampermonkey script to sync Feedly theme to OS
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 Feedly Auto Theme | |
// @version 0.1 | |
// @description Automatically switches between light and dark themes on feedly.com based on system settings. | |
// @author 13xforever | |
// @match https://feedly.com/* | |
// @run-at document-idle | |
// @license MIT | |
// ==/UserScript== | |
(function() { | |
"use strict"; | |
if (!window.matchMedia) { | |
return; | |
} | |
const LIGHT = "theme--light"; | |
const DARK = "theme--dark"; | |
const switchSelector = 'button[aria-label="Switch Between Themes"]'; | |
function waitForElm(selector) { | |
return new Promise(resolve => { | |
if (document.querySelector(selector)) { | |
return resolve(document.querySelector(selector)); | |
} | |
const observer = new MutationObserver(() => { | |
if (document.querySelector(selector)) { | |
resolve(document.querySelector(selector)); | |
observer.disconnect(); | |
} | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true | |
}); | |
}); | |
} | |
function checkMode(q) { | |
try { | |
let currentNightMode = document.body.classList.contains(DARK) ? DARK : LIGHT; | |
let newNightMode = q.matches ? DARK : LIGHT; | |
if (newNightMode !== currentNightMode) { | |
document.querySelector(switchSelector).click(); | |
} | |
} catch (ex) { | |
console.log(`Failed: ${ex}`); | |
} | |
} | |
waitForElm(switchSelector).then(() => { | |
setTimeout(() => { | |
const query = window.matchMedia('(prefers-color-scheme: dark)'); | |
checkMode(query); | |
query.addEventListener('change', checkMode); | |
}, 200); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment