Last active
April 11, 2025 09:09
-
-
Save benok/69b98fc38aa5884234fe50326ef014ae to your computer and use it in GitHub Desktop.
GitHub Gist apply dark theme on secret Gists
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 GitHub Gist apply dark theme on secret Gists | |
// @namespace https://github.com/benok/ | |
// @description Apply dark theme to your secret Gists on GitHub Gist (Choosing "Sync with system" from Theme prefs is required!) | |
// @include https://gist.github.com/* | |
// @run-at document-start | |
// @version 2025.4.11.3 | |
// @homepage https://gist.github.com/benok/69b98fc38aa5884234fe50326ef014ae | |
// @downloadURL https://gist.github.com/benok/69b98fc38aa5884234fe50326ef014ae/raw/github-gist-apply-dark-theme-on-secret-gists.user.js | |
// @author benok | |
// @grant none | |
// @license MIT | |
// ==/UserScript== | |
let apply_theme = function() | |
{ | |
if ( (document.querySelector('span.Label.v-align-middle')|| | |
document.querySelector('span.Label.Label--secondary')/* Editing page */).textContent.includes('Secret')) { | |
console.log('secret gist. set data-color-mode to dark'); | |
document.querySelector('html').setAttribute('data-color-mode', 'dark'); | |
} else { | |
console.log('public gist. set data-color-mode to light'); | |
document.querySelector('html').setAttribute('data-color-mode', 'light'); | |
} | |
}; | |
let make_private_gists_bg_gray = function () | |
{ | |
console.log('make_private_gists_bg_gray()'); | |
let lbs = document.querySelectorAll("span.Label"); | |
for (const lb of lbs) { | |
if (lb.textContent.includes('Secret')) { | |
lb.style.cssText += 'background-color:#434343; color:#ffffff;' | |
lb.closest('.gist-snippet').style.cssText += 'background:#cfcfcf;'; | |
} | |
} | |
} | |
function hook_body_attr_change(hook) { | |
// https://stackoverflow.com/a/11546242/26736 | |
let MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
var observer = new MutationObserver(function(mutations, observer) { | |
hook(); | |
}); | |
observer.observe(document.body, { | |
subtree: false, | |
attributes: true | |
}); | |
} | |
(function() { | |
'use strict'; | |
hook_body_attr_change(apply_theme); | |
window.onload = make_private_gists_bg_gray; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Having trouble now.
Setting html's data-color-mode doesn't update the actual page theme. :-(
(Setting the default theme to dark, it works partially, but unstable. May be needed to change apply timing.)