Github 镜像访问,加速下载

GitHub 镜像,github 加速

2022-04-04 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

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