-
-
Save moeshin/f63d97740d82a3b562d67c1987a1dac8 to your computer and use it in GitHub Desktop.
GitHub Clone or download、releases 下载加速
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name FastGit | |
// @version 0.8.1 | |
// @description GitHub Clone or download、Releases 下载加速 | |
// @author Vhxubo | |
// @license MIT | |
// @icon https://github.githubassets.com/favicon.ico | |
// @homepage https://gist.github.com/moeshin/f63d97740d82a3b562d67c1987a1dac8 | |
// @namespace https://gist.github.com/moeshin/f63d97740d82a3b562d67c1987a1dac8 | |
// @match https://github.com/*/* | |
// @match https://hub.fastgit.org/*/* | |
// @match https://hub.fastgit.xyz/*/* | |
// @grant none | |
// @note 2020.07.02_V0.8 适配新版 UI(Code);修改 Releases 下载方式(更直观);支持 hub.fastgit.org | |
// @note 2020.06.30_V0.7 修改 Releases 下载接口 | |
// @note 2020.06.29_V0.6 Releases 界面点击文件体积下载,不支持 Source code 下载 | |
// @note 2020.06.27_V0.5 适配新版 UI | |
// @note 2020.05.06_V0.4 新增: zipProxy - zip 下载链接 | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const gitProxy = 'https://hub.fastgit.xyz'; | |
const rawProxy = 'https://raw.fastgit.org'; | |
const sshProxy = '[email protected]'; | |
const releaseProxy = 'https://download.fastgit.org'; | |
const svgDownload = '<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path></svg>'; | |
const aTitle = 'FastGit 加速' | |
function onLoad() { | |
if (window.location.href.indexOf('releases') === -1) { | |
const node = document.querySelector('get-repo-controller') || document.querySelector('get-repo'); | |
if (node) { | |
const regex = /"((\/.*)+\.zip)"/; | |
const oldHtml = node.outerHTML; | |
const zipLink = gitProxy + regex.exec(oldHtml)[1]; | |
const outHtml = oldHtml | |
.replace('Clone or download', 'FastGit') | |
.replace('Clone', 'FastGit') | |
.replace('Code', 'FastGit') | |
.replace(/https:\/\/github.com/g, gitProxy) | |
.replace(regex, zipLink) | |
.replace(/git@github.com/g, sshProxy) | |
.replace('https%3A%2F%2Fgithub.com', 'https%3A%2F%2Fhub.fastgit.org') | |
// https://github.com/JetBrains/toolbox-browser-extension | |
.replace(/(<\/?)get-repo\b/g, '$1div') | |
; | |
node.insertAdjacentHTML('afterend', outHtml); | |
if (node.tagName === 'GET-REPO') { | |
node.classList.add('mr-2'); | |
} | |
} | |
const nodeList = document.querySelectorAll('[aria-labelledby=files]>.Box-row'); | |
if (nodeList) { | |
for (let node of nodeList) { | |
const a = node.querySelector('[role=rowheader] a'); | |
const name = a.getAttribute('title'); | |
const href = rawProxy + a.getAttribute('href').replace(/(^\/.+?\/.+?\/)blob\//, '$1'); | |
const html = node.querySelector('[aria-label=File]') | |
? `<a title="${aTitle}" download=${name} href="${href}">${svgDownload}</a>` | |
: ''; | |
node.querySelector('.text-right[role=gridcell]').insertAdjacentHTML( | |
'beforebegin', | |
`<div role="gridcell" class="mx-1" style="width: 16px;">${html}</div>` | |
); | |
} | |
} | |
} else { | |
const nodeList = document.querySelectorAll('.Box--condensed a'); | |
if (nodeList) { | |
for (let node of nodeList) { | |
const resHref = node.getAttribute('href'); | |
node.insertAdjacentHTML( | |
'afterend', | |
`<a title="${aTitle}" class="pl-2 float-right" href="${ | |
releaseProxy + resHref | |
}">${svgDownload}</a>` | |
); | |
} | |
} | |
} | |
} | |
document.addEventListener('pjax:success', onLoad); | |
onLoad(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment