Down Git

Create GitHub Resource Download Link.

当前为 2024-07-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Down Git
  3. // @namespace http://mogeko.me
  4. // @version 0.0.7
  5. // @author Zheng Junyi
  6. // @description Create GitHub Resource Download Link.
  7. // @license MIT
  8. // @icon https://besticon.herokuapp.com/icon?size=80..120..200&url=github.com
  9. // @homepage https://github.com/mogeko/userscripts/tree/master/packages/down-git#readme
  10. // @homepageURL https://github.com/mogeko/userscripts/tree/master/packages/down-git#readme
  11. // @source https://github.com/mogeko/userscripts.git
  12. // @supportURL https://github.com/mogeko/userscripts/issues
  13. // @match https://github.com/**
  14. // @match https://github.com/**/tree/**
  15. // @match https://github.com/**/blob/**
  16. // @grant none
  17. // @run-at document-idle
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. const SVG_ICON = `<svg class="octicon octicon-download hx_color-icon-directory" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M7.47 10.78a.75.75 0 001.06 0l3.75-3.75a.75.75 0 00-1.06-1.06L8.75 8.44V1.75a.75.75 0 00-1.5 0v6.69L4.78 5.97a.75.75 0 00-1.06 1.06l3.75 3.75zM3.75 13a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5z"/></svg>`;
  24. const DOWN_GIT = "https://minhaskamal.github.io/DownGit";
  25. const JSDELIVR = "https://cdn.jsdelivr.net";
  26. function downloader(localUrl) {
  27. const meta = localUrl == null ? void 0 : localUrl.split("/").filter((str) => str);
  28. const [_a, _b, user, repo, flag, branch, ...rest] = meta;
  29. if (flag === "tree") {
  30. return `${DOWN_GIT}/#/home?url=${localUrl}`;
  31. } else if (flag === "blob") {
  32. return `${JSDELIVR}/gh/${user}/${repo}@${branch}/${rest.join("/")}`;
  33. } else {
  34. return localUrl;
  35. }
  36. }
  37. function setButton(urlNode) {
  38. const wrapNode = document.createElement("div");
  39. const linkNode = document.createElement("a");
  40. const url = downloader(urlNode.href);
  41. wrapNode.setAttribute("class", "mr-3 flex-shrink-0");
  42. linkNode.setAttribute("href", url ?? "");
  43. linkNode.setAttribute("title", `Download ${urlNode.innerHTML}`);
  44. linkNode.innerHTML = SVG_ICON;
  45. wrapNode.appendChild(linkNode);
  46. return wrapNode;
  47. }
  48. requestIdleCallback(() => {
  49. document.querySelectorAll("div.Box-row").forEach((node) => {
  50. const urlNode = node.querySelector("a");
  51. const anchorNode = node.querySelector("div.text-right");
  52. if (!urlNode || urlNode.querySelector("span")) return;
  53. node.insertBefore(setButton(urlNode), anchorNode);
  54. });
  55. });
  56.  
  57. })();