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