在线下载Github仓库文件夹

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

当前为 2022-06-30 提交的版本,查看 最新版本

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

QingJ © 2025

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