Gist Raw Links

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

目前为 2023-07-01 提交的版本。查看 最新版本

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