Gist Raw Links

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

目前为 2017-12-15 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Gist Raw Links
  3. // @version 0.1.5
  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
  16. // @icon https://assets-cdn.github.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 : function(response) {
  91. let json = false;
  92. try {
  93. json = JSON.parse(response.responseText);
  94. } catch (err) {
  95. console.error(`Invalid JSON for gist ${gistid}`);
  96. return false;
  97. }
  98. if (json && json.files) {
  99.  
  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. event.preventDefault();
  112. event.stopPropagation();
  113. if (!$(".dropdown-item", target.parentNode)) {
  114. loadFileList(target);
  115. }
  116. }
  117. });
  118. }
  119.  
  120. function $(str, el) {
  121. return (el || document).querySelector(str);
  122. }
  123.  
  124. function $$(str, el) {
  125. return Array.from((el || document).querySelectorAll(str));
  126. }
  127.  
  128. document.addEventListener("pjax:end", update);
  129. update();
  130. addBindings();
  131. })();