Github 镜像访问,加速下载

GitHub 镜像,github 加速

  1. // ==UserScript==
  2. // @name Github 镜像访问,加速下载
  3. // @icon https://github.githubassets.com/favicon.ico
  4. // @namespace https://github.com/jadezi/github-accelerator/
  5. // @version 2.0.8
  6. // @description GitHub 镜像,github 加速
  7. // @author jadezi、wuyuehui
  8. // @license GPL License
  9. // @match *://github.com/*
  10. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js
  11. // @resource customStyles https://gitee.com/jadezi/github-accelerator-css/raw/master/index.css
  12. // @grant GM_addStyle
  13. // @grant GM_setClipboard
  14. // @grant GM_getResourceText
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_registerMenuCommand
  18. // @grant GM_openInTab
  19. // ==/UserScript==
  20.  
  21. (function () {
  22. const clone_url_list = [
  23. {
  24. name: '地址1',
  25. url: 'https://hub.fastgit.xyz',
  26. },
  27. {
  28. name: '地址2',
  29. url: 'https://hub.0z.gs',
  30. },
  31. {
  32. name: '地址3',
  33. url: 'https://api.mtr.pub',
  34. },
  35. ]
  36.  
  37. const download_url = [
  38. 'https://mirror.ghproxy.com',
  39. 'https://gh-proxy.du33169.workers.dev',
  40. ]
  41.  
  42. function init() {
  43. initMirrorUrl()
  44. isProjectUrl()
  45. setTimeout(addDownload, 2000);
  46. addRelease()
  47. }
  48.  
  49. // Project页面不添加镜像面板
  50. function isProjectUrl(){
  51. const pathnameArr = window.location.pathname.split('/');
  52. if(pathnameArr.length>3&&pathnameArr[3]=="projects"){
  53. return
  54. }else{
  55. addPanel()
  56. }
  57. }
  58.  
  59. // 初始化镜像地址
  60. function initMirrorUrl() {
  61. const _clone_url_list = clone_url_list.map(mirrorItem => {
  62. const [clone_url, quick_look_url] = joinCloneMirrorUrl(mirrorItem)
  63. return {
  64. clone_url,
  65. quick_look_url,
  66. }
  67. })
  68. console.log('%c [ _clone_url_list ]', 'font-size:13px; background:pink; color:#bf2c9f;', _clone_url_list)
  69. GM_setValue('clone_url_list', _clone_url_list)
  70. }
  71.  
  72. // 拼接克隆地址
  73. const joinCloneMirrorUrl = (mirrorItem) => {
  74. const { url } = mirrorItem
  75. let commonUrl = ''
  76. commonUrl += "git clone ";
  77. commonUrl += url;
  78. const pathnameArr = window.location.pathname.split('/');
  79. let clone_url = commonUrl + '/' + pathnameArr[1] + '/' + pathnameArr[2] + '.git'
  80. let quick_look_url = url + '/' + pathnameArr[1] + '/' + pathnameArr[2]
  81. return [clone_url, quick_look_url]
  82. }
  83.  
  84. // 初始化镜像面板
  85. function addPanel() {
  86. const clone_url_list = GM_getValue('clone_url_list')
  87. // 镜像面板模板
  88. const mirror_template = `
  89. <div class="mirror-panel clearfix container-xl px-3 px-md-4 px-lg-5 mt-4">
  90. <div class="mb-1">
  91. <button class="btn btn-primary" type="button" id="mirror-btn">镜像网址</button>
  92. </div>
  93. <div class="collapse multi-collapse" id="collapse">
  94. <div class="user-card user-card-body">
  95. <div class="user-alert user-alert-warning" role="alert">clonedepth命令的插入可手动编辑代码关闭</div>
  96. <div class="user-alert user-alert-danger" role="alert">镜像地址请不要登陆自己的账户,造成损失本人概不负责</div>
  97. <!-- 插入克隆模板列表位置 -->
  98. </div>
  99. </div>
  100. </div>
  101. `
  102. // 获取克隆地址模板
  103. const get_clone_template = (mirrorItem, index) => {
  104. const { clone_url, quick_look_url } = mirrorItem
  105. return `
  106. <div class="mb-3">
  107. <div class="h5 mb-1">克隆地址${index + 1}</div>
  108. <div class="input-group">
  109. <input type="text" class="form-control input-monospace input color-bg-subtle" value="${clone_url}"
  110. aria-label="${clone_url}">
  111. <div class="input-group-button">
  112. <clipboard-copy value="${clone_url}" aria-label="Copy to clipboard"
  113. class="btn js-clipboard-copy ClipboardButton" data-copy-feedback="Copied!" role="button" style="border-left:0;border-radius:0">
  114. 复制
  115. </clipboard-copy>
  116. </div>
  117. <div class="input-group-button">
  118. <button class="btn quick_look" type="button" data-url="${quick_look_url}" data class="btn">快速浏览</button>
  119. </div>
  120. </div>
  121. </div>
  122. `
  123. }
  124.  
  125. // 克隆模板数组
  126. const clone_template_List = clone_url_list.map((mirrorItem, index) => {
  127. return get_clone_template(mirrorItem, index)
  128. })
  129.  
  130. $(".repository-content").prepend(mirror_template);
  131.  
  132. clone_template_List.forEach(template => {
  133. $(".user-card-body").append(template);
  134. });
  135.  
  136. // 隐藏面板
  137. $("#mirror-btn").on("click", () => {
  138. togglePanelVisible()
  139. })
  140.  
  141. // 快速浏览
  142. $(".quick_look").on("click", (e) => {
  143. const quick_look_url = e.target.dataset.url
  144. window.open(quick_look_url)
  145. });
  146.  
  147. initPanelVisible()
  148. }
  149.  
  150. // 初始化镜像面板状态,根据上一次状态显示
  151. function initPanelVisible() {
  152. const currentPanelVisible = GM_getValue('panelVisible')
  153. if (currentPanelVisible === true) {
  154. $("#collapse").show();
  155. } else {
  156. $("#collapse").hide();
  157. }
  158. }
  159.  
  160. // Download ZIP
  161. function addDownload() {
  162. const clone_url_list = GM_getValue('clone_url_list')
  163.  
  164. const get_download_template = (mirrorItem, index) => {
  165. const { quick_look_url } = mirrorItem
  166. let download_url = quick_look_url + "/archive/master.zip";
  167. return `
  168. <li class="Box-row Box-row--hover-gray p-3 mt-0">
  169. <a class="d-flex flex-items-center color-fg-default text-bold no-underline" href=${download_url}>
  170. Fast Download ZIP(下载地址${index + 1})
  171. </a>
  172. </li>
  173. `
  174. }
  175.  
  176. const download_template_list = clone_url_list.map((mirrorItem, index) => {
  177. return get_download_template(mirrorItem, index)
  178. })
  179.  
  180. download_template_list.forEach(template => {
  181. $("#local-panel ul").append(template)
  182. });
  183. }
  184.  
  185. // Release
  186. function addRelease() {
  187. $(".Box .Box-footer").each(function () {
  188. $(this).find("li.Box-row").each(function () {
  189. const href = $(this).find("a")[0].href
  190. const li_obj= $(this)
  191. const download_url1 = `${download_url[0]}/${href}`
  192. const download_url2 = `${download_url[1]}/${href}`
  193. let download_template = `
  194. <div class="mt-1" style="display: flex; float: right;position: relative;top: -10px;left: 10px;">
  195. <a class="btn btn-sm mr-1" href="${download_url1}" rel="nofollow">快速下载1</a>
  196. <a class="btn btn-sm" href="${download_url2}" rel="nofollow">快速下载2</a>
  197. </div>
  198. `
  199. li_obj.children('span.float-right').before(download_template);
  200. if(li_obj.children('span.float-right').length<1){
  201. li_obj.children('a').before(download_template);
  202. }
  203. });
  204. });
  205. }
  206.  
  207. // 切换面板显示隐藏
  208. function togglePanelVisible() {
  209. const currentPanelVisible = GM_getValue('panelVisible')
  210. if (currentPanelVisible === true) {
  211. $("#collapse").hide();
  212. } else {
  213. $("#collapse").show();
  214. }
  215. GM_setValue('panelVisible', !currentPanelVisible)
  216. }
  217.  
  218. //
  219. function toggleDepthVisible() {
  220. const currentDepthVisible = GM_getValue('depthVisible')
  221. if (currentDepthVisible === true) {
  222. $("#collapse").hide();
  223. } else {
  224. $("#collapse").show();
  225. }
  226. GM_setValue('depthVisible', !currentDepthVisible)
  227. }
  228.  
  229. // 注册菜单
  230. GM_registerMenuCommand(`【🧲开启 & 关闭 - depth】`, toggleDepthVisible)
  231. GM_registerMenuCommand(`【🔔显示 & 隐藏 - 镜像信息面板】`, togglePanelVisible)
  232. GM_registerMenuCommand(`【📢意见 & 反馈】`, () => { window.GM_openInTab('https://github.com/jadezi/github-accelerator/issues/new', { active: true, insert: true, setParent: true }); })
  233.  
  234. // 设置自定义样式
  235. GM_addStyle(GM_getResourceText("customStyles"));
  236.  
  237. // 初始化
  238. init()
  239. })();