在线下载Github仓库文件夹

无需克隆GitHub仓库, 一键在线下载 Github仓库子文件夹; 同时还能在源码详情页一键复制源码

当前为 2022-10-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Download github repo sub-folder
  3. // @version 0.5.2
  4. // @author Saiya
  5. // @description download github sub-folder via one click, copy the single file's source code easily
  6. // @supportURL https://github.com/oe/download-git-userscript/issues
  7. // @match https://github.com/*
  8. // @match https://gist.github.com/*
  9. // @namespace https://app.evecalm.com
  10. // @name:zh-CN 在线下载Github仓库文件夹
  11. // @description:zh-CN 无需克隆GitHub仓库, 一键在线下载 Github仓库子文件夹; 同时还能在源码详情页一键复制源码
  12. // @homepageURL https://github.com/oe/download-git-userscript
  13. // @licence MIT
  14. // @icon https://github.githubassets.com/pinned-octocat.svg
  15. // @connect cdn.jsdelivr.net
  16. // @grant GM_setClipboard
  17. // @grant GM_xmlhttpRequest
  18. // @noframes
  19. // ==/UserScript==
  20.  
  21. /******/ (function(modules) { // webpackBootstrap
  22. /******/ // The module cache
  23. /******/ var installedModules = {};
  24. /******/
  25. /******/ // The require function
  26. /******/ function __webpack_require__(moduleId) {
  27. /******/
  28. /******/ // Check if module is in cache
  29. /******/ if(installedModules[moduleId]) {
  30. /******/ return installedModules[moduleId].exports;
  31. /******/ }
  32. /******/ // Create a new module (and put it into the cache)
  33. /******/ var module = installedModules[moduleId] = {
  34. /******/ i: moduleId,
  35. /******/ l: false,
  36. /******/ exports: {}
  37. /******/ };
  38. /******/
  39. /******/ // Execute the module function
  40. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  41. /******/
  42. /******/ // Flag the module as loaded
  43. /******/ module.l = true;
  44. /******/
  45. /******/ // Return the exports of the module
  46. /******/ return module.exports;
  47. /******/ }
  48. /******/
  49. /******/
  50. /******/ // expose the modules object (__webpack_modules__)
  51. /******/ __webpack_require__.m = modules;
  52. /******/
  53. /******/ // expose the module cache
  54. /******/ __webpack_require__.c = installedModules;
  55. /******/
  56. /******/ // define getter function for harmony exports
  57. /******/ __webpack_require__.d = function(exports, name, getter) {
  58. /******/ if(!__webpack_require__.o(exports, name)) {
  59. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  60. /******/ }
  61. /******/ };
  62. /******/
  63. /******/ // define __esModule on exports
  64. /******/ __webpack_require__.r = function(exports) {
  65. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  66. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  67. /******/ }
  68. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  69. /******/ };
  70. /******/
  71. /******/ // create a fake namespace object
  72. /******/ // mode & 1: value is a module id, require it
  73. /******/ // mode & 2: merge all properties of value into the ns
  74. /******/ // mode & 4: return value when already ns object
  75. /******/ // mode & 8|1: behave like require
  76. /******/ __webpack_require__.t = function(value, mode) {
  77. /******/ if(mode & 1) value = __webpack_require__(value);
  78. /******/ if(mode & 8) return value;
  79. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  80. /******/ var ns = Object.create(null);
  81. /******/ __webpack_require__.r(ns);
  82. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  83. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  84. /******/ return ns;
  85. /******/ };
  86. /******/
  87. /******/ // getDefaultExport function for compatibility with non-harmony modules
  88. /******/ __webpack_require__.n = function(module) {
  89. /******/ var getter = module && module.__esModule ?
  90. /******/ function getDefault() { return module['default']; } :
  91. /******/ function getModuleExports() { return module; };
  92. /******/ __webpack_require__.d(getter, 'a', getter);
  93. /******/ return getter;
  94. /******/ };
  95. /******/
  96. /******/ // Object.prototype.hasOwnProperty.call
  97. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  98. /******/
  99. /******/ // __webpack_public_path__
  100. /******/ __webpack_require__.p = "";
  101. /******/
  102. /******/
  103. /******/ // Load entry module and return exports
  104. /******/ return __webpack_require__(__webpack_require__.s = 0);
  105. /******/ })
  106. /************************************************************************/
  107. /******/ ([
  108. /* 0 */
  109. /***/ (function(module, exports, __webpack_require__) {
  110.  
  111. "use strict";
  112.  
  113. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  114. if (k2 === undefined) k2 = k;
  115. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  116. }) : (function(o, m, k, k2) {
  117. if (k2 === undefined) k2 = k;
  118. o[k2] = m[k];
  119. }));
  120. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  121. Object.defineProperty(o, "default", { enumerable: true, value: v });
  122. }) : function(o, v) {
  123. o["default"] = v;
  124. });
  125. var __importStar = (this && this.__importStar) || function (mod) {
  126. if (mod && mod.__esModule) return mod;
  127. var result = {};
  128. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  129. __setModuleDefault(result, mod);
  130. return result;
  131. };
  132. Object.defineProperty(exports, "__esModule", { value: true });
  133. const utils = __importStar(__webpack_require__(1));
  134. (function () {
  135. const DOWNLOAD_BTN_ID = 'xiu-download-btn';
  136. const STYLE_ELEMENT_ID = 'xiu-style-element';
  137. let tid = 0;
  138. main();
  139. // observePageChange()
  140. document.addEventListener('DOMSubtreeModified', onBodyChanged);
  141. function main() {
  142. if (!utils.isRepo())
  143. return;
  144. addDownloadBtn();
  145. addDownload2FileList();
  146. }
  147. function onBodyChanged() {
  148. clearTimeout(tid);
  149. // @ts-ignore
  150. tid = setTimeout(main, 100);
  151. }
  152. function addDownloadBtn() {
  153. let $navi = document.querySelector('.application-main .file-navigation');
  154. if (!$navi) {
  155. $navi = document.getElementById('blob-more-options-details');
  156. if (!$navi)
  157. return;
  158. $navi = $navi.parentElement;
  159. }
  160. const downloadBtn = getDownloadBtn($navi);
  161. if ($navi.contains(downloadBtn))
  162. return;
  163. $navi.appendChild(downloadBtn);
  164. }
  165. function getDownloadBtn($fileNavi) {
  166. let downloadBtn = document.getElementById(DOWNLOAD_BTN_ID);
  167. if (!downloadBtn) {
  168. downloadBtn = document.createElement('a');
  169. downloadBtn.id = DOWNLOAD_BTN_ID;
  170. }
  171. downloadBtn.className = 'btn d-none d-md-block ml-2';
  172. downloadBtn.target = '_blank';
  173. let url = '';
  174. if (utils.isRepoRootDir()) {
  175. const link = $fileNavi.querySelector('get-repo a[href$=".zip"]');
  176. url = link.href;
  177. }
  178. else {
  179. url = utils.getGithubDownloadUrl(utils.getCurrentUrlPath());
  180. }
  181. downloadBtn.textContent = 'Download';
  182. downloadBtn.href = url;
  183. return downloadBtn;
  184. }
  185. function addDownload2FileList() {
  186. if (document.getElementById(STYLE_ELEMENT_ID))
  187. return;
  188. const style = document.createElement('style');
  189. style.id = STYLE_ELEMENT_ID;
  190. const styleContent = `
  191. .Box .Box-row > [role="gridcell"]:first-child:after {
  192. position: absolute;
  193. left: 20px;
  194. top: 10px;
  195. opacity: 0.6;
  196. pointer-events: none;
  197. content: '↓';
  198. font-size: 0.8em;
  199. }
  200.  
  201. .Box .Box-row > [role="gridcell"]:first-child > svg {
  202. cursor: pointer;
  203. }
  204. `;
  205. style.textContent = styleContent;
  206. document.head.appendChild(style);
  207. addEvent2FileIcon();
  208. }
  209. function addEvent2FileIcon() {
  210. document.documentElement.addEventListener('click', (e) => {
  211. var _a, _b, _c, _d;
  212. // @ts-ignore
  213. const target = (e.target && e.target.ownerSVGElement || e.target);
  214. if (!target || (target.tagName || '').toLowerCase() !== 'svg')
  215. return;
  216. const label = target.getAttribute('aria-label') || '';
  217. if (!['Directory', 'File'].includes(label))
  218. return;
  219. const url = (_d = (_c = (_b = (_a = target.parentElement) === null || _a === void 0 ? void 0 : _a.nextElementSibling) === null || _b === void 0 ? void 0 : _b.querySelector) === null || _c === void 0 ? void 0 : _c.call(_b, 'a')) === null || _d === void 0 ? void 0 : _d.href;
  220. if (!url)
  221. return;
  222. const isFile = label === 'File';
  223. utils.openLink(utils.getGithubDownloadUrl(url, isFile));
  224. });
  225. }
  226. })();
  227.  
  228.  
  229. /***/ }),
  230. /* 1 */
  231. /***/ (function(module, exports, __webpack_require__) {
  232.  
  233. "use strict";
  234.  
  235. Object.defineProperty(exports, "__esModule", { value: true });
  236. exports.getGithubDownloadUrl = exports.openLink = exports.getCurrentUrlPath = exports.getRawBtn = exports.getUrlTextResponse = exports.isTextBasedSinglePage = exports.isRepoRootDir = exports.isPrivateRepo = exports.isRepo = exports.isGist = void 0;
  237. /**
  238. * is gist website
  239. */
  240. function isGist() {
  241. return location.hostname === 'gist.github.com';
  242. }
  243. exports.isGist = isGist;
  244. function isRepo() {
  245. if (!document.querySelector('.repository-content, #js-repo-pjax-container'))
  246. return false;
  247. const meta = document.querySelector('meta[name="selected-link"]');
  248. if (meta && meta.getAttribute('value') === 'repo_commits')
  249. return false;
  250. if (document.querySelector('.js-navigation-container>.TimelineItem'))
  251. return false;
  252. return true;
  253. }
  254. exports.isRepo = isRepo;
  255. function isPrivateRepo() {
  256. const label = document.querySelector('#js-repo-pjax-container .hide-full-screen .Label');
  257. return label && label.textContent === 'Private';
  258. }
  259. exports.isPrivateRepo = isPrivateRepo;
  260. function isRepoRootDir() {
  261. return !!document.querySelector('.repository-content get-repo');
  262. }
  263. exports.isRepoRootDir = isRepoRootDir;
  264. function isTextBasedSinglePage() {
  265. if (!getRawBtn())
  266. return;
  267. if (document.getElementById('readme'))
  268. return true;
  269. const boxBody = document.querySelector('table.highlight');
  270. if (boxBody)
  271. return true;
  272. return false;
  273. }
  274. exports.isTextBasedSinglePage = isTextBasedSinglePage;
  275. function getUrlTextResponse(url) {
  276. // https://github.com/oe/search/raw/gh-pages/app-icon-retina.f492fc13.png
  277. // https://cdn.jsdelivr.net/gh/oe/search@gh-pages/app-icon-retina.f492fc13.png
  278. // https://github.com/oe/search/raw/master/CNAME
  279. let apiUrl = url
  280. .replace('github.com/', 'cdn.jsdelivr.net/gh/')
  281. .replace('/raw/', '@');
  282. return new Promise((resolve, reject) => {
  283. // @ts-ignore
  284. GM_xmlhttpRequest({
  285. url: apiUrl,
  286. method: 'GET',
  287. onload: (s) => {
  288. resolve(s.responseText);
  289. }
  290. });
  291. });
  292. }
  293. exports.getUrlTextResponse = getUrlTextResponse;
  294. // if is single file page, then it has a raw btn
  295. function getRawBtn() {
  296. return document.getElementById('raw-url');
  297. }
  298. exports.getRawBtn = getRawBtn;
  299. // remove qeurystring & hash
  300. function getCurrentUrlPath() {
  301. const url = location.origin + location.pathname;
  302. return url.replace(/\/$/, '');
  303. }
  304. exports.getCurrentUrlPath = getCurrentUrlPath;
  305. function openLink(url) {
  306. const link = document.createElement('a');
  307. link.target = '_blank';
  308. link.href = url;
  309. link.click();
  310. }
  311. exports.openLink = openLink;
  312. function getGithubDownloadUrl(url, isFile) {
  313. if (isFile) {
  314. try {
  315. const u = new URL(url);
  316. let paths = u.pathname.split('/');
  317. paths[3] = 'raw';
  318. u.pathname = paths.join('/');
  319. return u.href;
  320. }
  321. catch (error) { }
  322. }
  323. return `https://downgit.evecalm.com/#/home?url=${encodeURIComponent(url)}`;
  324. }
  325. exports.getGithubDownloadUrl = getGithubDownloadUrl;
  326.  
  327.  
  328. /***/ })
  329. /******/ ]);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址