Skip to content

Instantly share code, notes, and snippets.

@13xforever
Last active February 4, 2023 06:38
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 13xforever/8ff0b7106c8b3c3eff0e13ca6fb471ab to your computer and use it in GitHub Desktop.
Save 13xforever/8ff0b7106c8b3c3eff0e13ca6fb471ab to your computer and use it in GitHub Desktop.
Tampermonkey script to sync Feedly theme to OS
// ==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