Gist Raw Links

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

目前為 2019-02-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Gist Raw Links
  3. // @version 0.1.8
  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 * { pointer-events:none; }
  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 = "select-menu js-menu-container js-select-menu";
  30.  
  31. function addButton(node) {
  32. const button = item.cloneNode();
  33. button.innerHTML = `
  34. <a class="select-menu-button js-menu-target ghrl-get-list" aria-expanded="false" aria-haspopup="true">
  35. 🍣 Raw urls
  36. </a>
  37. <div class="select-menu-modal-holder">
  38. <div class="select-menu-modal js-menu-content">
  39. <div class="select-menu-header">
  40. <svg aria-label="Close" class="octicon octicon-x js-menu-close" height="16" role="img" version="1.1" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"/></svg>
  41. <span class="select-menu-title">Files</span>
  42. </div>
  43. <div class="js-select-menu-deferred-content ghrl-files">
  44. <div>
  45. <img src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="32" alt="">
  46. </div>
  47. </div>
  48. </div>
  49. </div>`;
  50. node.insertBefore(button, node.childNodes[0]);
  51. }
  52.  
  53. function update() {
  54. const gists = $$(".gist-snippet");
  55. let indx = gists.length;
  56. if (indx) {
  57. while (indx--) {
  58. // only save dabblet files from list
  59. if (!$(".ghrl-get-list", gists[indx])) {
  60. addButton($(".gist-count-links", gists[indx]));
  61. }
  62. }
  63. }
  64. }
  65.  
  66. function addList(link, files) {
  67. let url,
  68. html = "";
  69. Object.keys(files).forEach(file => {
  70. // remove version sha from raw_url to always point at
  71. // the latest version of the file - see #18
  72. url = files[file].raw_url.replace(/raw\/\w+\//, "raw/");
  73. html += `
  74. <a class="dropdown-item ghrl-file" role="menuitem" href="${url}">
  75. ${file}
  76. </a>`;
  77. });
  78. $(".ghrl-files", link.parentNode).innerHTML = html;
  79. }
  80.  
  81. function loadFileList(link) {
  82. let url,
  83. el = link.closest(".select-menu");
  84. el = $("a", el.nextElementSibling);
  85. if (el) {
  86. url = el.href.split("/");
  87. GM.xmlHttpRequest({
  88. method : "GET",
  89. url : `https://api.github.com/gists/${url.pop()}`,
  90. onload : response => {
  91. if (response.status !== 200) {
  92. $(".ghrl-files", link.parentNode).innerHTML = response.message;
  93. return console.error(response);
  94. }
  95. let json = false;
  96. try {
  97. json = JSON.parse(response.responseText);
  98. } catch (err) {
  99. return console.error(`Invalid JSON for gist ${gistid}`);
  100. }
  101. if (json && json.files) {
  102. addList(link, json.files);
  103. }
  104. }
  105. });
  106. }
  107. }
  108.  
  109. function addBindings() {
  110. document.addEventListener("click", function(event) {
  111. const target = event.target;
  112. if (target.classList.contains("ghrl-get-list")) {
  113. event.preventDefault();
  114. event.stopPropagation();
  115. if (!$(".dropdown-item", target.parentNode)) {
  116. loadFileList(target);
  117. }
  118. }
  119. });
  120. }
  121.  
  122. function $(str, el) {
  123. return (el || document).querySelector(str);
  124. }
  125.  
  126. function $$(str, el) {
  127. return Array.from((el || document).querySelectorAll(str));
  128. }
  129.  
  130. document.addEventListener("pjax:end", update);
  131. update();
  132. addBindings();
  133. })();