DownGit

Create GitHub Resource Download Link.

当前为 2022-04-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name DownGit
  3. // @namespace http://mogeko.me
  4. // @version 0.0.2
  5. // @description Create GitHub Resource Download Link.
  6. // @author mogeko
  7. // @match https://github.com/*/*
  8. // @match https://github.com/*/*/tree/*/*
  9. // @exclude https://github.com/*/*/blob/*/*
  10. // @icon https://github.githubassets.com/favicons/favicon.png
  11. // @run-at document-idle
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. /**
  16. * <div role="row">
  17. * <div><svg class={{icon}}/></div>,
  18. * <div><span><a>{{name}}</a></span></div>,
  19. * <div><span><a>{{message}}</a></span></div>,
  20. * <div><span>{time}</span></div>,
  21. * </div>
  22. */
  23.  
  24. 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>`;
  25.  
  26. const downloader = (local_url) => {
  27. const meta = local_url.split("/").filter((str) => str);
  28. console.log(meta);
  29. if (meta[4] == "tree") {
  30. return `https://minhaskamal.github.io/DownGit/#/home?url=${local_url}`;
  31. } else if (meta[4] == "blob") {
  32. const file = meta.slice(6).join("/");
  33. return `https://cdn.jsdelivr.net/gh/${meta[2]}/${meta[3]}@${meta[5]}/${file}`;
  34. } else {
  35. return local_url;
  36. }
  37. };
  38.  
  39. const getButton = (url_node, downloader) => {
  40. const div = document.createElement("div");
  41. div.className = "mr-3 flex-shrink-0";
  42. const a = document.createElement("a");
  43. a.href = downloader(url_node.href);
  44. a.title = `Download ${url_node.innerHTML}`;
  45. a.innerHTML = svg_icon;
  46. div.appendChild(a);
  47. return div;
  48. };
  49.  
  50. (function () {
  51. "use strict";
  52.  
  53. const context = document.querySelectorAll("div.Box-row");
  54. context.forEach((node) => {
  55. const url = node.querySelector("a");
  56. const last_child = node.querySelector("div.text-right");
  57. if (url.querySelector("span")) return;
  58. const button = getButton(url, downloader);
  59. node.insertBefore(button, last_child);
  60. });
  61. })();