Gist Raw Links

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

当前为 2020-03-28 提交的版本,查看 最新版本

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