Github Gist Compact View (With Private Gists)

Displays Gists (including private ones) in a compact view with clickable links, visibility status, and last update date

  1. // ==UserScript==
  2. // @name Github Gist Compact View (With Private Gists)
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.0
  5. // @description Displays Gists (including private ones) in a compact view with clickable links, visibility status, and last update date
  6. // @author Sergi0
  7. // @match https://gist.github.com/*
  8. // @grant none
  9. // @icon https://gist.github.com/favicon.ico
  10. // @license MIT
  11. // @homepageURL https://greasyfork.org/en/scripts/529737-github-gist-compact-view
  12. // @supportURL https://greasyfork.org/en/scripts/529737-github-gist-compact-view/feedback
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. console.log("UserScript GitHub Gist Compact View started...");
  19.  
  20. // Replace with your personal access token (PAT)
  21. const personalAccessToken = 'YOUR_PERSONAL_ACCESS_TOKEN_HERE';
  22.  
  23. // Wait until the page has fully loaded
  24. window.addEventListener('load', () => {
  25.  
  26. // Extract the username from the URL
  27. const pathParts = window.location.pathname.split("/").filter(Boolean);
  28. const username = pathParts.length > 0 ? pathParts[0] : null;
  29.  
  30. if (!username) {
  31. console.warn("No username found in the URL.");
  32. return;
  33. }
  34.  
  35. console.log(`Detected user: ${username}`);
  36.  
  37. // Function to load the Gists and display them in the container
  38. function loadGists() {
  39. fetch(`https://api.github.com/users/${username}/gists`, {
  40. method: 'GET',
  41. headers: {
  42. 'Authorization': `token ${personalAccessToken}` // Authenticate with the token
  43. }
  44. })
  45. .then(response => {
  46. if (!response.ok) {
  47. throw new Error(`Request error: ${response.status} ${response.statusText}`);
  48. }
  49. return response.json();
  50. })
  51. .then(gists => {
  52. if (gists.length === 0) {
  53. console.warn("The user has no Gists.");
  54. return;
  55. }
  56.  
  57. // Find the container to display the information
  58. const container = document.querySelector("#gist-pjax-container > div > div > div.col-9.col-md-9.col-12");
  59.  
  60. if (!container) {
  61. console.warn("Container not found.");
  62. return;
  63. }
  64.  
  65. // Clear the container
  66. container.innerHTML = "";
  67.  
  68. // Create a new container for the Gists
  69. const newContainer = document.createElement("div");
  70. newContainer.className = "custom-gist-container";
  71. newContainer.innerHTML = `<h3>Gists of ${username} (${gists.length})</h3><ul>`;
  72.  
  73. // Add each Gist as a clickable link with visibility and last update date
  74. gists.forEach(gist => {
  75. const listItem = document.createElement("li");
  76. const gistLink = document.createElement("a");
  77. gistLink.href = gist.html_url;
  78. gistLink.target = "_blank"; // Open link in a new tab
  79. gistLink.textContent = gist.description || "No description";
  80. // Determine visibility (public or private)
  81. const visibility = gist.public ? 'Public' : 'Private';
  82.  
  83. // Get the last update date or creation date
  84. const lastUpdated = new Date(gist.updated_at);
  85. const lastUpdatedDate = lastUpdated.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
  86.  
  87. // Append visibility and last update date
  88. listItem.innerHTML = `
  89. ${gistLink.outerHTML}
  90. <span>(${visibility})</span>
  91. <span>Last updated: ${lastUpdatedDate}</span>
  92. `;
  93. newContainer.appendChild(listItem);
  94. });
  95.  
  96. // Close the list and insert the new container into the page
  97. newContainer.innerHTML += "</ul>";
  98. container.appendChild(newContainer);
  99. console.log("New Gist view successfully inserted.");
  100. })
  101. .catch(error => console.error("Error loading Gists:", error));
  102. }
  103.  
  104. // Execute the function to load Gists
  105. loadGists();
  106. });
  107.  
  108. })();