Gist Raw Links

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

当前为 2019-02-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Gist Raw Links
  3. // @version 0.2.0
  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. // @include 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. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
  16. // @icon https://github.githubassets.com/pinned-octocat.svg
  17. // ==/UserScript==
  18. (() => {
  19. "use strict";
  20.  
  21. GM.addStyle(`
  22. .ghrl-get-list, .ghrl-files a { cursor:pointer; }
  23. .ghrl-files div { text-align:center; }
  24. .gist-count-links { z-index: 21; }
  25. `);
  26.  
  27. const item = document.createElement("li");
  28. item.className = "d-inline-block mr-3";
  29.  
  30. function addButton(node) {
  31. const button = item.cloneNode();
  32. button.innerHTML = `
  33. <details class="details-reset details-overlay select-menu">
  34. <summary class="select-menu-button">
  35. <span class="ghrl-get-list" data-menu-button>🍣 Raw urls</span>
  36. </summary>
  37. <details-menu class="select-menu-modal position-absolute ghrl-files" style="z-index: 99;" aria-label="Raw gist links">
  38. <div class="select-menu-header">
  39. <span class="select-menu-title">Filter options</span>
  40. </div>
  41. <div class="select-menu-list">
  42. <img src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="32" alt="">
  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="js-selected-navigation-item select-menu-item ghrl-file" role="menuitem" aria-current="page>
  70. <span class="select-menu-item-text" data-menu-button-text>
  71. ${file}
  72. </span>
  73. </a>`;
  74. });
  75. $(".ghrl-files", link.closest("li")).innerHTML = html;
  76. }
  77.  
  78. function loadFileList(link) {
  79. let url,
  80. el = link.closest("li");
  81. el = $("a", el.nextElementSibling);
  82. if (el) {
  83. url = el.href.split("/");
  84. const gistid = url.pop();
  85. GM.xmlHttpRequest({
  86. method : "GET",
  87. url : `https://api.github.com/gists/${gistid}`,
  88. onload : response => {
  89. if (response.status !== 200) {
  90. $(".ghrl-files", link.parentNode).innerHTML = response.message;
  91. return console.error(response);
  92. }
  93. let json = false;
  94. try {
  95. json = JSON.parse(response.responseText);
  96. } catch (err) {
  97. return console.error(`Invalid JSON for gist ${gistid}`);
  98. }
  99. if (json && json.files) {
  100. addList(link, json.files);
  101. }
  102. }
  103. });
  104. }
  105. }
  106.  
  107. function addBindings() {
  108. document.addEventListener("click", function(event) {
  109. const target = event.target;
  110. if (target.classList.contains("ghrl-get-list")) {
  111. if (!$(".dropdown-item", target.parentNode)) {
  112. loadFileList(target);
  113. }
  114. }
  115. });
  116. }
  117.  
  118. function $(str, el) {
  119. return (el || document).querySelector(str);
  120. }
  121.  
  122. function $$(str, el) {
  123. return Array.from((el || document).querySelectorAll(str));
  124. }
  125.  
  126. document.addEventListener("pjax:end", update);
  127. update();
  128. addBindings();
  129. })();

QingJ © 2025

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