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

QingJ © 2025

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