Gist Raw Links

Add a button that contains a list of gist raw file links

  1. // ==UserScript==
  2. // @name Gist Raw Links
  3. // @version 0.2.2
  4. // @description Add a button that contains a list of gist raw file links
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @match https://gist.github.com/*
  9. // @run-at document-idle
  10. // @grant GM_addStyle
  11. // @grant GM.addStyle
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM.xmlHttpRequest
  14. // @connect api.github.com
  15. // @connect assets-cdn.github.com
  16. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
  17. // @icon https://github.githubassets.com/pinned-octocat.svg
  18. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  19. // ==/UserScript==
  20.  
  21. /* global GM */
  22. (() => {
  23. "use strict";
  24.  
  25. GM.addStyle(`
  26. .ghrl-get-list, .ghrl-files a { cursor:pointer; }
  27. .ghrl-files div { text-align:center; }
  28. .gist-count-links { z-index: 21; }
  29. `);
  30.  
  31. const item = document.createElement("li");
  32. item.className = "d-inline-block mr-3";
  33.  
  34. function addButton(node) {
  35. const button = item.cloneNode();
  36. button.innerHTML = `
  37. <details class="details-reset details-overlay select-menu ghrl-wrapper">
  38. <summary class="select-menu-button" aria-haspopup="menu">
  39. <span class="ghrl-get-list" data-menu-button>🍣 Raw urls</span>
  40. </summary>
  41. <details-menu class="select-menu-modal position-absolute ghrl-files" style="z-index: 99;" role="menu" aria-label="Raw gist links">
  42. <div class="select-menu-list">
  43. <img src="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif" width="32">
  44. </div>
  45. </details-menu>
  46. </details>`;
  47. node.insertBefore(button, node.childNodes[0]);
  48. }
  49.  
  50. function update() {
  51. const gists = $$(".gist-snippet");
  52. let indx = gists.length;
  53. if (indx) {
  54. while (indx--) {
  55. // only save dabblet files from list
  56. if (!$(".ghrl-get-list", gists[indx])) {
  57. addButton($(".gist-snippet-meta ul", gists[indx]));
  58. }
  59. }
  60. }
  61. }
  62.  
  63. function addList(link, files) {
  64. let html = "";
  65. Object.keys(files).forEach(file => {
  66. // remove version sha from raw_url to always point at
  67. // the latest version of the file - see #18
  68. const url = files[file].raw_url.replace(/raw\/\w+\//, "raw/");
  69. html += `
  70. <a href="${url}" class="select-menu-item ghrl-file" role="menuitem">
  71. ${file}
  72. </a>`;
  73. });
  74. $(".ghrl-files", link.closest("li")).innerHTML = html;
  75. }
  76.  
  77. function loadFileList(link) {
  78. let el = link.closest("li");
  79. el = $("a", el && el.nextElementSibling);
  80. if (el) {
  81. const gistid = el.href.split("/").slice(-1);
  82. GM.xmlHttpRequest({
  83. method : "GET",
  84. url : `https://api.github.com/gists/${gistid}`,
  85. onload : response => {
  86. if (response.status !== 200) {
  87. $(".ghrl-files", link.parentNode).innerHTML = response.message;
  88. return console.error(response);
  89. }
  90. let json = false;
  91. try {
  92. json = JSON.parse(response.responseText);
  93. } catch (err) {
  94. return console.error(`Invalid JSON for gist ${gistid}`);
  95. }
  96. if (json && json.files) {
  97. addList(link, json.files);
  98. }
  99. }
  100. });
  101. }
  102. }
  103.  
  104. function addBindings() {
  105. document.addEventListener("click", function(event) {
  106. const target = event.target.closest("details");
  107. if (target && target.classList.contains("ghrl-wrapper")) {
  108. loadFileList(target);
  109. }
  110. });
  111. }
  112.  
  113. function $(str, el) {
  114. return (el || document).querySelector(str);
  115. }
  116.  
  117. function $$(str, el) {
  118. return Array.from((el || document).querySelectorAll(str));
  119. }
  120.  
  121. document.addEventListener("pjax:end", update);
  122. update();
  123. addBindings();
  124. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址