Skip to content

Instantly share code, notes, and snippets.

@pjpscriv
Last active April 6, 2025 06:12
Show Gist options
  • Save pjpscriv/6d29841107c93feeb042e730f29baea1 to your computer and use it in GitHub Desktop.
Save pjpscriv/6d29841107c93feeb042e730f29baea1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Github Profile StackOverflow Icon
// @namespace http://pjpscriv.com/
// @version 2025-03
// @description Prettify StackOverflow links on Github Profiles, as is done for other social media platforms.
// @author @pjpscriv
// @match https://github.com/*
// @match https://gist.github.com/*
// @exclude https://github.com/*/.*
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/GitHub_Invertocat_Logo.svg/240px-GitHub_Invertocat_Logo.svg.png
// @grant none
// ==/UserScript==
const logging = true;
const logName = '[GH Profile SO Link]';
(function() {
'use strict';
const stackDomain = "://stackoverflow.com";
const elSelector = "[itemprop=social], [itemprop=url]";
const linkSelector = ".Link--primary";
const svgSelector = "svg";
const iconPaths = `
<path d="M12.6199 14.5722V10.2894H14.0458V16H1.22034V10.2894H2.64624V14.5722H12.6199Z" fill="currentColor"/>
<path d="M4.21543 9.89546L11.1928 11.3635L11.4861 9.96645L4.50871 8.49838L4.21543 9.89546ZM5.13854 6.55087L11.6017 9.56424L12.2038 8.2702L5.74058 5.25682L5.13854 6.55087ZM6.92715 3.37649L12.4058 7.94428L13.3183 6.84733L7.8397 2.27955L6.92715 3.37649ZM10.4638 0L9.3197 0.851929L13.5747 6.5791L14.7189 5.72717L10.4638 0ZM4.07373 13.1445H11.2035V11.7169H4.07373V13.1445Z" fill="currentColor"/>
`;
const soProfileRegEx = /stackoverflow\.com\/(?:users|u)\/(\d+)(?:\/([^\/]+))?/;
function replaceSOLinks() {
// Attempt to get links
const els = document.querySelectorAll(elSelector);
const filteredEls = Array.from(els).filter(el => el.querySelector(linkSelector).href.includes(stackDomain))
// If profile has no SO link(s) - exit
if (filteredEls.length < 1) {
return;
}
filteredEls.forEach(el => {
// Change Icon
const svgEl = el.querySelector(svgSelector);
svgEl.innerHTML = iconPaths;
// Extract text
const linkEl = el.querySelector(linkSelector)
const text = linkEl.innerHTML;
const match = text.match(soProfileRegEx);
const userId = match ? match[1] : null;
const userName = match && match[2] ? match[2] : null;
// Change text
if (!!userName && !!userId) {
linkEl.innerHTML = `${userName}`;
} else if (!!userId) {
linkEl.innerHTML = `user:${userId}`;
}
});
}
// Run for first time
if (logging) { console.log(`${logName}: Run on first load`); }
replaceSOLinks();
// Set up URL change listener
let lastURL = window.location.href;
const observer = new MutationObserver(() => {
if (window.location.href === lastURL) { return; }
lastURL = window.location.href;
if (logging) { console.log(`${logName}: URL chaged, re-running`); }
replaceSOLinks();
});
observer.observe(document.body, { childList: true, subtree: true });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment