Greasy Fork 还支持 简体中文。

Gist Raw Links

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

目前為 2020-09-08 提交的版本,檢視 最新版本

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