Last active
September 10, 2019 03:19
-
-
Save markerikson/0a70f1c13ea211f6718452ea47f30d43 to your computer and use it in GitHub Desktop.
Discord: Split Theme Userscript (Dark Sidebar, Light Chat)
This file contains 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 Discord Split Dark+Light Theme | |
// @version 1 | |
// @match https://discordapp.com/* | |
// @grant none | |
// ==/UserScript== | |
// NOTE: Set Discord to the Light theme. This will override the sidebar and | |
// "guilds" sections to force them to the dark theme. | |
function classnamesMatch(node, partialClassnames) { | |
if (!node || !node.className) { | |
return false; | |
} | |
const partialClassnamesMatch = partialClassnames.some(pc => | |
node.className.includes(pc) | |
); | |
return partialClassnamesMatch; | |
} | |
function observeForChanges( | |
isAdded, | |
partialClassnames = [], | |
onChangedCallback = () => {} | |
) { | |
const observer = new MutationObserver(mutations => { | |
//console.log("Checking nodes..."); | |
let numNodesChecked = 0; | |
const fieldName = isAdded ? "addedNodes" : "removedNodes"; | |
const partialClassnameSelectors = partialClassnames | |
.map(pc => `[class*=${pc}]`) | |
.join(", "); | |
console.log("Classname selectors: ", partialClassnameSelectors); | |
for (let mutation of mutations) { | |
for (let addedNode of mutation[fieldName]) { | |
numNodesChecked++; | |
if (classnamesMatch(addedNode, partialClassnames)) { | |
//console.log("Found matching classname node: ", { addedNode }); | |
onChangedCallback(addedNode); | |
} else { | |
const matchingDescendants = addedNode.querySelectorAll( | |
partialClassnameSelectors | |
); | |
matchingDescendants.forEach(onChangedCallback); | |
} | |
} | |
} | |
//console.log("Total nodes checked: ", numNodesChecked); | |
}); | |
observer.observe(document.documentElement, { | |
childList: true, | |
subtree: true | |
}); | |
} | |
function watchForAddedNodes() { | |
//console.log("Starting to watch for elements..."); | |
observeForChanges(true, ["guilds-", "sidebar-"], node => { | |
//console.log("Node added: ", { node }); | |
node.classList.add("theme-dark"); | |
}); | |
} | |
watchForAddedNodes(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment