Down Git

Create GitHub Resource Download Link.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Down Git
// @namespace    http://mogeko.me
// @version      0.0.9
// @author       Zheng Junyi
// @description  Create GitHub Resource Download Link.
// @license      MIT
// @icon         https://besticon.herokuapp.com/icon?size=80..120..200&url=github.com
// @homepage     https://github.com/mogeko/userscripts/tree/master/packages/down-git#readme
// @homepageURL  https://github.com/mogeko/userscripts/tree/master/packages/down-git#readme
// @source       https://github.com/mogeko/userscripts.git
// @supportURL   https://github.com/mogeko/userscripts/issues
// @match        https://github.com/**
// @match        https://github.com/**/tree/**
// @match        https://github.com/**/blob/**
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  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>`;
  const DOWN_GIT = "https://minhaskamal.github.io/DownGit";
  const JSDELIVR = "https://cdn.jsdelivr.net";
  function downloader(localUrl) {
    const meta = localUrl == null ? void 0 : localUrl.split("/").filter((str) => str);
    const [_a, _b, user, repo, flag, branch, ...rest] = meta;
    if (flag === "tree") {
      return `${DOWN_GIT}/#/home?url=${localUrl}`;
    }
    if (flag === "blob") {
      return `${JSDELIVR}/gh/${user}/${repo}@${branch}/${rest.join("/")}`;
    }
    return localUrl;
  }
  function setButton(urlNode) {
    const wrapNode = document.createElement("div");
    const linkNode = document.createElement("a");
    const url = downloader(urlNode.href);
    wrapNode.setAttribute("class", "mr-3 flex-shrink-0");
    linkNode.setAttribute("href", url ?? "");
    linkNode.setAttribute("title", `Download ${urlNode.innerHTML}`);
    linkNode.innerHTML = SVG_ICON;
    wrapNode.appendChild(linkNode);
    return wrapNode;
  }
  requestIdleCallback(() => {
    for (const node of document.querySelectorAll("div.Box-row")) {
      const urlNode = node.querySelector("a");
      const anchorNode = node.querySelector("div.text-right");
      if (!urlNode || urlNode.querySelector("span")) return;
      node.insertBefore(setButton(urlNode), anchorNode);
    }
  });

})();