Down Git

Create GitHub Resource Download Link.

目前為 2022-12-13 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Down Git
  3. // @description Create GitHub Resource Download Link.
  4. // @namespace http://mogeko.me
  5. // @supportURL https://github.com/mogeko/userscripts/issues
  6. // @match https://github.com/**
  7. // @match https://github.com/**/tree/**
  8. // @match https://github.com/**/blob/**
  9. // @icon https://besticon.herokuapp.com/icon?size=80..120..200&url=github.com
  10. // @run-at document-idle
  11. // @author Mogeko
  12. // @license MIT
  13. // @version 0.0.4
  14. // @grant none
  15. // ==/UserScript==
  16. (function () {
  17. 'use strict';
  18.  
  19. /**
  20. * <div role="row">
  21. * <div><svg class={{icon}}/></div>,
  22. * <div><span><a>{{name}}</a></span></div>,
  23. * <div><span><a>{{message}}</a></span></div>,
  24. * <div><span>{time}</span></div>,
  25. * </div>
  26. */ function _arrayLikeToArray(arr, len) {
  27. if (len == null || len > arr.length) len = arr.length;
  28. for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
  29. return arr2;
  30. }
  31. function _arrayWithHoles(arr) {
  32. if (Array.isArray(arr)) return arr;
  33. }
  34. function _iterableToArray(iter) {
  35. if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
  36. }
  37. function _nonIterableRest() {
  38. throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  39. }
  40. function _toArray(arr) {
  41. return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest();
  42. }
  43. function _unsupportedIterableToArray(o, minLen) {
  44. if (!o) return;
  45. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  46. var n = Object.prototype.toString.call(o).slice(8, -1);
  47. if (n === "Object" && o.constructor) n = o.constructor.name;
  48. if (n === "Map" || n === "Set") return Array.from(n);
  49. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  50. }
  51. var SVG_ICON = '<svg class="octicon octicon-download hx_color-icon-directory" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M7.47 10.78a.75.75 0 001.06 0l3.75-3.75a.75.75 0 00-1.06-1.06L8.75 8.44V1.75a.75.75 0 00-1.5 0v6.69L4.78 5.97a.75.75 0 00-1.06 1.06l3.75 3.75zM3.75 13a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5z"/></svg>';
  52. var DOWN_GIT = "https://minhaskamal.github.io/DownGit";
  53. var JSDELIVR = "https://cdn.jsdelivr.net";
  54. // /caiyongji/emoji-list/blob/master/LICENSE
  55. function downloader(localUrl) {
  56. var meta = localUrl === null || localUrl === void 0 ? void 0 : localUrl.split("/").filter(function(str) {
  57. return str;
  58. });
  59. var _meta = _toArray(meta); _meta[0]; _meta[1]; var user = _meta[2], repo = _meta[3], flag = _meta[4], branch = _meta[5], rest = _meta.slice(6);
  60. if (flag === "tree") {
  61. return "".concat(DOWN_GIT, "/#/home?url=").concat(localUrl);
  62. } else if (flag === "blob") {
  63. return "".concat(JSDELIVR, "/gh/").concat(user, "/").concat(repo, "@").concat(branch, "/").concat(rest.join("/"));
  64. } else {
  65. return localUrl;
  66. }
  67. }
  68. function setButton(urlNode) {
  69. var wrapNode = document.createElement("div");
  70. var linkNode = document.createElement("a");
  71. var url = downloader(urlNode.href);
  72. wrapNode.setAttribute("class", "mr-3 flex-shrink-0");
  73. linkNode.setAttribute("href", url !== null && url !== void 0 ? url : "");
  74. linkNode.setAttribute("title", "Download ".concat(urlNode.innerHTML));
  75. linkNode.innerHTML = SVG_ICON;
  76. wrapNode.appendChild(linkNode);
  77. return wrapNode;
  78. }
  79. requestIdleCallback(function() {
  80. document.querySelectorAll("div.Box-row").forEach(function(node) {
  81. var urlNode = node.querySelector("a");
  82. var anchorNode = node.querySelector("div.text-right");
  83. if (!urlNode || urlNode.querySelector("span")) return;
  84. node.insertBefore(setButton(urlNode), anchorNode);
  85. });
  86. });
  87.  
  88. })();