Download github repo sub-folder

download github sub-folder via one click, copy the single file's source code easily

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

  1. // ==UserScript==
  2. // @name Download github repo sub-folder
  3. // @version 0.6.0
  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.  
  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. let tid = 0;
  139. main();
  140. // observePageChange()
  141. document.addEventListener('DOMSubtreeModified', onBodyChanged);
  142. function main() {
  143. if (!utils.isRepo())
  144. return;
  145. addDownloadBtn();
  146. addDownload2FileList();
  147. }
  148. function onBodyChanged() {
  149. clearTimeout(tid);
  150. // @ts-ignore
  151. tid = setTimeout(main, 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. $navi = document.querySelector('[data-testid="tree-overflow-menu-anchor"]');
  159. if (!$navi)
  160. return;
  161. }
  162. $navi = $navi.parentElement;
  163. }
  164. const downloadBtn = getDownloadBtn($navi);
  165. if ($navi.contains(downloadBtn))
  166. return;
  167. $navi.appendChild(downloadBtn);
  168. }
  169. function getDownloadBtn($fileNavi) {
  170. let downloadBtn = document.getElementById(DOWNLOAD_BTN_ID);
  171. if (!downloadBtn) {
  172. downloadBtn = document.createElement('a');
  173. downloadBtn.id = DOWNLOAD_BTN_ID;
  174. }
  175. const isRoot = utils.isRepoRootDir();
  176. downloadBtn.className = `btn d-none d-md-block ${isRoot ? 'ml-2' : ''}`;
  177. downloadBtn.target = '_blank';
  178. let url = '';
  179. if (isRoot) {
  180. const link = $fileNavi.querySelector('get-repo a[href$=".zip"]');
  181. url = link.href;
  182. }
  183. else {
  184. url = utils.getGithubDownloadUrl(utils.getCurrentUrlPath());
  185. }
  186. downloadBtn.textContent = 'Download';
  187. downloadBtn.href = url;
  188. return downloadBtn;
  189. }
  190. function addDownload2FileList() {
  191. if (document.getElementById(STYLE_ELEMENT_ID))
  192. return;
  193. const style = document.createElement('style');
  194. style.id = STYLE_ELEMENT_ID;
  195. const styleContent = `
  196. .react-directory-filename-column { position: relative; }
  197. .react-directory-filename-column:after,
  198. .Box .Box-row > [role="gridcell"]:first-child:after {
  199. position: absolute;
  200. left: 20px;
  201. top: 10px;
  202. opacity: 0.6;
  203. pointer-events: none;
  204. content: '↓';
  205. font-size: 0.8em;
  206. z-index: 11;
  207. }
  208. .react-directory-filename-column svg {
  209. cursor: pointer;
  210. z-index: 10;
  211. }
  212. .react-directory-filename-column:after {
  213. left: 4px;
  214. top: 12px;
  215. color: white;
  216. }
  217. [data-color-mode="light"] .react-directory-filename-column:after {
  218. color: black;
  219. }
  220. @media (prefers-color-scheme: light) {
  221. [data-color-mode=auto][data-light-theme*=light] .react-directory-filename-column:after {
  222. color: black;
  223. }
  224. }
  225.  
  226. /** hide download icon on mobile */
  227. @media (max-width: 500px) {
  228. .Box .Box-row > [role="gridcell"]:first-child:after,
  229. .react-directory-filename-column:after {
  230. display: none
  231. }
  232. }
  233.  
  234. .Box .Box-row > [role="gridcell"]:first-child > svg {
  235. cursor: pointer;
  236. }
  237. `;
  238. style.textContent = styleContent;
  239. document.head.appendChild(style);
  240. addEvent2FileIcon();
  241. }
  242. function addEvent2FileIcon() {
  243. document.documentElement.addEventListener('click', (e) => {
  244. var _a, _b, _c, _d, _e, _f, _g, _h;
  245. // @ts-ignore
  246. const target = (e.target && e.target.ownerSVGElement || e.target);
  247. if (!target || (target.tagName || '').toLowerCase() !== 'svg')
  248. return;
  249. const label = target.getAttribute('aria-label') || '';
  250. let url = '';
  251. let isFile = false;
  252. if (['Directory', 'File'].includes(label)) {
  253. 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;
  254. isFile = label === 'File';
  255. }
  256. else if ((_e = target.parentElement) === null || _e === void 0 ? void 0 : _e.classList.contains('react-directory-filename-column')) {
  257. url = (_h = (_g = (_f = target.nextElementSibling) === null || _f === void 0 ? void 0 : _f.querySelector) === null || _g === void 0 ? void 0 : _g.call(_f, 'a')) === null || _h === void 0 ? void 0 : _h.href;
  258. console.warn("url", url);
  259. isFile = target.classList.contains('color-fg-muted');
  260. }
  261. else {
  262. return;
  263. }
  264. if (!url)
  265. return;
  266. utils.openLink(utils.getGithubDownloadUrl(url, isFile));
  267. }, {
  268. capture: true,
  269. });
  270. }
  271. })();
  272.  
  273.  
  274. /***/ }),
  275. /* 1 */
  276. /***/ (function(module, exports, __webpack_require__) {
  277.  
  278. "use strict";
  279.  
  280. Object.defineProperty(exports, "__esModule", { value: true });
  281. exports.getGithubDownloadUrl = exports.openLink = exports.getCurrentUrlPath = exports.getRawBtn = exports.getUrlTextResponse = exports.isTextBasedSinglePage = exports.isRepoRootDir = exports.isPrivateRepo = exports.isRepo = exports.isGist = void 0;
  282. /**
  283. * is gist website
  284. */
  285. function isGist() {
  286. return location.hostname === 'gist.github.com';
  287. }
  288. exports.isGist = isGist;
  289. function isRepo() {
  290. if (!document.querySelector('.repository-content, #js-repo-pjax-container'))
  291. return false;
  292. const meta = document.querySelector('meta[name="selected-link"]');
  293. if (meta && meta.getAttribute('value') === 'repo_commits')
  294. return false;
  295. if (document.querySelector('.js-navigation-container>.TimelineItem'))
  296. return false;
  297. return true;
  298. }
  299. exports.isRepo = isRepo;
  300. function isPrivateRepo() {
  301. const label = document.querySelector('#js-repo-pjax-container .hide-full-screen .Label');
  302. return label && label.textContent === 'Private';
  303. }
  304. exports.isPrivateRepo = isPrivateRepo;
  305. function isRepoRootDir() {
  306. return !!document.querySelector('.repository-content get-repo');
  307. }
  308. exports.isRepoRootDir = isRepoRootDir;
  309. function isTextBasedSinglePage() {
  310. if (!getRawBtn())
  311. return;
  312. if (document.getElementById('readme'))
  313. return true;
  314. const boxBody = document.querySelector('table.highlight');
  315. if (boxBody)
  316. return true;
  317. return false;
  318. }
  319. exports.isTextBasedSinglePage = isTextBasedSinglePage;
  320. function getUrlTextResponse(url) {
  321. // https://github.com/oe/search/raw/gh-pages/app-icon-retina.f492fc13.png
  322. // https://cdn.jsdelivr.net/gh/oe/search@gh-pages/app-icon-retina.f492fc13.png
  323. // https://github.com/oe/search/raw/master/CNAME
  324. let apiUrl = url
  325. .replace('github.com/', 'cdn.jsdelivr.net/gh/')
  326. .replace('/raw/', '@');
  327. return new Promise((resolve, reject) => {
  328. // @ts-ignore
  329. GM_xmlhttpRequest({
  330. url: apiUrl,
  331. method: 'GET',
  332. onload: (s) => {
  333. resolve(s.responseText);
  334. }
  335. });
  336. });
  337. }
  338. exports.getUrlTextResponse = getUrlTextResponse;
  339. // if is single file page, then it has a raw btn
  340. function getRawBtn() {
  341. return document.getElementById('raw-url');
  342. }
  343. exports.getRawBtn = getRawBtn;
  344. // remove qeurystring & hash
  345. function getCurrentUrlPath() {
  346. const url = location.origin + location.pathname;
  347. return url.replace(/\/$/, '');
  348. }
  349. exports.getCurrentUrlPath = getCurrentUrlPath;
  350. function openLink(url) {
  351. const link = document.createElement('a');
  352. link.target = '_blank';
  353. link.href = url;
  354. link.click();
  355. }
  356. exports.openLink = openLink;
  357. function getGithubDownloadUrl(url, isFile) {
  358. if (isFile) {
  359. try {
  360. const u = new URL(url);
  361. let paths = u.pathname.split('/');
  362. paths[3] = 'raw';
  363. u.pathname = paths.join('/');
  364. return u.href;
  365. }
  366. catch (error) { }
  367. }
  368. return `https://downgit.evecalm.com/#/home?url=${encodeURIComponent(url)}`;
  369. }
  370. exports.getGithubDownloadUrl = getGithubDownloadUrl;
  371.  
  372.  
  373. /***/ })
  374. /******/ ]);