Skip to content

Instantly share code, notes, and snippets.

@exurd
Last active May 13, 2025 21:49
Show Gist options
  • Save exurd/32f3e889c0f7b4433ee4d475bad9f573 to your computer and use it in GitHub Desktop.
Save exurd/32f3e889c0f7b4433ee4d475bad9f573 to your computer and use it in GitHub Desktop.
Custom GitHub profile CSS by creating `.custard/style.css` in your special repository.
// ==UserScript==
// @name custard
// @namespace exurd.custard
// @version 4
// @description custom github profile css
// @author exurd
// @match https://github.com/*
// @connect raw.githubusercontent.com
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// ==/UserScript==
const CSS_MARKER = '/* custard-profile-style */';
(function() {
'use strict';
let currentProfile = null;
function cleanupStyles() {
document.querySelectorAll('style').forEach(e => {
if (e.textContent.includes(CSS_MARKER)) {
e.remove();
}
});
}
function applyCustomStyle() {
const path = window.location.pathname.replace(/^\/|\/$/g, '');
const parts = path.split('/');
const username = (parts.length === 1 && parts[0]) ? parts[0] : null;
if (username === currentProfile) {
return;
}
currentProfile = username;
cleanupStyles();
if (!username) {
return;
}
const cssUrl = `https://raw.githubusercontent.com/${username}/${username}/main/.custard/style.css`;
GM_xmlhttpRequest({
method: "GET",
url: cssUrl,
onload: function(response) {
if (response.status === 200) {
const wrappedCSS = `${CSS_MARKER}\n${response.responseText}`;
GM_addStyle(wrappedCSS);
} else {
console.error(`custard: failed to load CSS from ${cssUrl} (status ${response.status})`);
}
},
onerror: function(err) {
console.error("custard: error fetching CSS:", err);
}
});
}
window.addEventListener('load', applyCustomStyle);
window.addEventListener('popstate', applyCustomStyle);
document.addEventListener('pjax:end', applyCustomStyle);
['pushState', 'replaceState'].forEach(fnName => {
const original = history[fnName];
history[fnName] = function() {
const ret = original.apply(this, arguments);
setTimeout(applyCustomStyle, 0);
return ret;
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment