Last active
April 11, 2025 10:25
-
-
Save benok/301f7a9667a598202f10111af054ae77 to your computer and use it in GitHub Desktop.
GitHub apply dark theme on private repo
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 apply dark theme on private repo | |
// @namespace https://github.com/benok/ | |
// @description Apply dark theme to your private repository pages on Github | |
// @match https://github.com/* | |
// @run-at document-start | |
// @version 2025.4.11.3 | |
// @homepage https://gist.github.com/benok/301f7a9667a598202f10111af054ae77 | |
// @downloadURL https://gist.github.com/benok/301f7a9667a598202f10111af054ae77/raw/github-apply-dark-theme-on-private-repos.user.js | |
// @updateURL https://gist.github.com/benok/301f7a9667a598202f10111af054ae77/raw/github-apply-dark-theme-on-private-repos.user.js | |
// @author benok | |
// @grant none | |
// @license MIT | |
// ==/UserScript== | |
let apply_theme = function() | |
{ | |
// lock icon exists in repo header | |
if (document.querySelector("a.AppHeader-context-item svg.octicon.octicon-lock") != null) { | |
console.log('private repo. set data-color-mode to dark'); | |
document.querySelector('html').setAttribute('data-color-mode', 'dark'); | |
} else { | |
console.log('public repo. set data-color-mode to light'); | |
document.querySelector('html').setAttribute('data-color-mode', 'light'); | |
} | |
}; | |
let make_private_buttons_black = function(observer, elem) | |
{ | |
if (observer != null) { | |
observer.disconnect(); | |
} | |
console.log('make_private_buttons_black()'); | |
let lbs = document.querySelectorAll("span.Label"); | |
for (const lb of lbs) { | |
if (lb.textContent.includes('Private')) { | |
lb.style.cssText += 'background-color:#434343; color:#ffffff;' | |
//lb.closest('.private').style.cssText += 'background:#cfcfcf;'; // done by css | |
} | |
} | |
if (observer != null) { | |
observer.observe(elem, { | |
subtree: false, | |
attributes: true | |
}); | |
} | |
} | |
function hook_body_attr_change(hook) { | |
// https://stackoverflow.com/a/11546242/26736 | |
let MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
let observer = new MutationObserver(function(mutations, observer) { | |
hook(); | |
}); | |
observer.observe(document.body, { | |
subtree: false, | |
attributes: true | |
}); | |
} | |
function on_load() { | |
console.log('on_load()'); | |
let user_repo_list = document.querySelector('#user-repositories-list'); | |
if (user_repo_list != null) { | |
console.log('found user repo list'); | |
make_private_buttons_black(); // call once | |
hook_user_repo_list_change(user_repo_list, make_private_buttons_black); | |
} | |
} | |
function hook_user_repo_list_change(elem, hook) { | |
console.log('hook_user_repo_list_change'); | |
let MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
let observer = new MutationObserver(function(mutations, observer) { | |
console.log('observer handled.'); | |
(function (obs,elm) { | |
obs.disconnect(); // hook modifies elem. need to disconnect before call | |
hook(); | |
obs.observe(elm, { | |
subtree: true, | |
attributes: true | |
}); | |
})(observer, elem); | |
}); | |
observer.observe(elem, { | |
subtree: true, | |
attributes: true | |
}); | |
} | |
function append_css() { | |
const el= document.createElement('style'); | |
// make private repo's bacground gray | |
el.textContent = 'li.private {background-color: #cfcfcf}'; | |
document.head.appendChild(el); | |
} | |
(function() { | |
'use strict'; | |
append_css(); | |
hook_body_attr_change(apply_theme); | |
window.onload = on_load; | |
})(); |
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.)