在线下载Github仓库文件夹

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

当前为 2023-12-18 提交的版本,查看 最新版本

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

QingJ © 2025

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