🏠 Home 

GitHub Gist Link

Adds a Gist link to GitHub profile pages.

  1. // ==UserScript==
  2. // @name GitHub Gist Link
  3. // @description Adds a Gist link to GitHub profile pages.
  4. // @icon https://github.githubassets.com/favicons/favicon-dark.svg
  5. // @version 1.1
  6. // @author afkarxyz
  7. // @namespace https://github.com/afkarxyz/misc-scripts/
  8. // @supportURL https://github.com/afkarxyz/misc-scripts/issues
  9. // @license MIT
  10. // @match https://github.com/*
  11. // @exclude https://gist.github.com/*
  12. // @grant none
  13. // ==/UserScript==
  14. (function() {
  15. 'use strict';
  16. function addGistLink() {
  17. const usernameElement = document.querySelector('.p-nickname.vcard-username');
  18. if (usernameElement && !usernameElement.querySelector('.gist-link-userscript')) {
  19. const currentURL = window.location.pathname;
  20. const username = currentURL.split('/')[1];
  21. const linkContainer = document.createElement('span');
  22. linkContainer.className = 'gist-link-container';
  23. const gistLink = document.createElement('a');
  24. gistLink.href = `https://gist.github.com/${username}`;
  25. gistLink.textContent = 'Gist';
  26. gistLink.className = 'Link--secondary gist-link-userscript';
  27. gistLink.style.textDecoration = 'none';
  28. linkContainer.appendChild(gistLink);
  29. linkContainer.appendChild(document.createTextNode(' · '));
  30. usernameElement.insertBefore(linkContainer, usernameElement.firstChild);
  31. }
  32. }
  33. setTimeout(addGistLink, 500);
  34. const observer = new MutationObserver(function(mutations) {
  35. const isProfilePage = /^\/[^\/]+\/?$/.test(window.location.pathname);
  36. if (isProfilePage) {
  37. addGistLink();
  38. }
  39. });
  40. observer.observe(document.body, {
  41. childList: true,
  42. subtree: true
  43. });
  44. window.addEventListener('popstate', addGistLink);
  45. window.addEventListener('pushstate', addGistLink);
  46. window.addEventListener('replacestate', addGistLink);
  47. let lastUrl = location.href;
  48. new MutationObserver(() => {
  49. const url = location.href;
  50. if (url !== lastUrl) {
  51. lastUrl = url;
  52. setTimeout(addGistLink, 300);
  53. }
  54. }).observe(document, {subtree: true, childList: true});
  55. })();