11// ==UserScript==
22// @name Github 增强 - 高速下载
3- // @version 1.4.1
3+ // @version 1.4.2
44// @author X.I.U
55// @description 高速下载 Git Clone、Release、Raw、Code(ZIP) 等文件、项目列表单文件快捷下载 (☁)
66// @match *://github.com/*
77// @match *://hub.fastgit.org/*
88// @icon https://i.loli.net/2021/03/30/ULV9XunaHesqGIR.png
9- // @require https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
10- /* globals $ */
119// @grant GM_registerMenuCommand
1210// @grant GM_unregisterMenuCommand
1311// @grant GM_openInTab
2220( function ( ) {
2321 var backColor = '#ffffff' ;
2422 var fontColor = '#888888' ;
25- if ( $ ( 'html' ) . attr ( 'data-color-mode' ) == 'dark' ) { // 黑暗模式判断
26- if ( $ ( 'html' ) . attr ( 'data-dark-theme' ) == 'dark_dimmed' ) {
23+ if ( document . getElementsByTagName ( 'html' ) [ 0 ] . getAttribute ( 'data-color-mode' ) == 'dark' ) { // 黑暗模式判断
24+ if ( document . getElementsByTagName ( 'html' ) [ 0 ] . getAttribute ( 'data-dark-theme' ) == 'dark_dimmed' ) {
2725 backColor = '#272e37' ;
2826 fontColor = '#768390' ;
2927 } else {
3028 backColor = '#161a21' ;
3129 fontColor = '#b2b8bf' ;
3230 }
3331 }
32+
3433 var download_url = [
3534 [ 'https://gh.con.sh' , '美国' ] ,
3635 [ 'https://gh.api.99988866.xyz' , '美国' ] ,
5958 style = [ 'padding:0 6px;margin-right: -1px;border-radius: 2px;background-color: ' + backColor + ';border-color: rgba(27, 31, 35, 0.1);font-size: 11px;color: ' + fontColor + ';' ] ,
6059 menu_raw_fast = GM_getValue ( 'xiu2_menu_raw_fast' ) ,
6160 menu_menu_raw_fast_ID , menu_feedBack_ID ;
62- if ( menu_raw_fast == null || menu_raw_fast == '中国国内' ) { menu_raw_fast = 1 ; GM_setValue ( 'xiu2_menu_raw_fast' , 1 ) } ; // 调整上个版本的设置存储变量内容
61+ if ( menu_raw_fast == null ) { menu_raw_fast = 1 ; GM_setValue ( 'xiu2_menu_raw_fast' , 1 ) } ;
6362
6463 registerMenuCommand ( ) ;
6564 // 注册脚本菜单
107106
108107 // Release
109108 function addRelease ( ) {
110- $ ( ".Box.Box--condensed" ) . each ( function ( ) {
111- $ ( this ) . find ( ".d-flex.Box-body>a" ) . each ( function ( ) {
112- let href = $ ( this ) . attr ( "href" ) ,
109+ let original = document . getElementsByClassName ( 'Box Box--condensed' )
110+ if ( ! original ) return
111+ Array . from ( original ) . forEach ( function ( current ) {
112+ current . querySelectorAll ( '.d-flex.Box-body > a' ) . forEach ( function ( current2 ) {
113+ let href = current2 . href ,
113114 url = [
114115 download_url [ 0 ] [ 0 ] + '/https://github.com' + href ,
115116 download_url [ 1 ] [ 0 ] + '/https://github.com' + href ,
124125 html += `<a style="${ style [ 0 ] } " class="btn" href="${ url [ i ] } " rel="noreferrer noopener nofollow">${ download_url [ i ] [ 1 ] } </a>`
125126 }
126127 html += `</div>`
127- $ ( this ) . next ( ) . after ( html ) ;
128+ current2 . nextElementSibling . insertAdjacentHTML ( 'afterend' , html ) ;
128129 } ) ;
129130 // 修改[文件大小]元素样式
130131 document . querySelectorAll ( 'small.pl-2.color-text-secondary.flex-shrink-0' ) . forEach ( el => { el . style . cssText = 'display: flex; justify-content: flex-end; flex-grow: 1; margin-right: 8px;' } ) ;
131132
132133
133134 // Source Code
134- $ ( this ) . find ( " .d-block.Box-body>a" ) . each ( function ( ) {
135- let href = $ ( this ) . attr ( " href" ) ,
135+ current . querySelectorAll ( ' .d-block.Box-body > a' ) . forEach ( function ( current2 ) {
136+ let href = current2 . href ,
136137 url = [
137138 download_url [ 0 ] [ 0 ] + '/https://github.com' + href ,
138139 download_url [ 1 ] [ 0 ] + '/https://github.com' + href ,
147148 html += `<a style="${ style [ 0 ] } " class="btn" href="${ url [ i ] } " rel="noreferrer noopener nofollow">${ download_url [ i ] [ 1 ] } </a>`
148149 }
149150 html += `</div>`
150- $ ( this ) . after ( html ) ;
151+ current2 . insertAdjacentHTML ( 'afterend' , html ) ;
151152 } ) ;
152153 } ) ;
153154 // 修改 Source code 样式,使其和加速按钮并列一排
157158
158159 // Download ZIP
159160 function addDownloadZIP ( ) {
160- $ ( " .dropdown-menu.dropdown-menu-sw.p-0 ul li:last-child" ) . each ( function ( ) {
161- let href = $ ( this ) . children ( "a" ) . attr ( "href" ) ,
162- url = [
163- download_url [ 0 ] [ 0 ] + "/https://github.com" + href ,
164- download_url [ 1 ] [ 0 ] + "/https://github.com" + href ,
165- download_url [ 2 ] [ 0 ] + href ,
166- download_url [ 3 ] [ 0 ] + "/https://github.com" + href ,
167- download_url [ 4 ] [ 0 ] + "/https://github.com" + href ,
168- download_url [ 5 ] [ 0 ] + ' /https://github.com' + href
169- ] ,
170- html = `` ;
171- for ( let i = 0 ; i < url . length ; i ++ )
172- {
173- html += `<li class="Box-row Box-row--hover-gray p-0"><a class="d-flex flex-items-center text-gray-dark text-bold no-underline p-3" rel="noreferrer noopener nofollow" href=" ${ url [ i ] } "> ${ svg [ 0 ] } Download ZIP ${ download_url [ i ] [ 1 ] } </a></li>`
174- }
175- $ ( this ) . after ( html ) ;
176- } ) ;
161+ let original = document . querySelector ( ' .dropdown-menu.dropdown-menu-sw.p-0 ul li:last-child' ) ;
162+ if ( ! original ) return
163+ let href = original . getElementsByTagName ( 'a' ) [ 0 ] . href ,
164+ url = [
165+ download_url [ 0 ] [ 0 ] + "/https://github.com" + href ,
166+ download_url [ 1 ] [ 0 ] + "/https://github.com" + href ,
167+ download_url [ 2 ] [ 0 ] + href ,
168+ download_url [ 3 ] [ 0 ] + "/https://github.com" + href ,
169+ download_url [ 4 ] [ 0 ] + " /https://github.com" + href ,
170+ download_url [ 5 ] [ 0 ] + '/https://github.com' + href
171+ ] ,
172+ html = `` ;
173+ for ( let i = 0 ; i < url . length ; i ++ )
174+ {
175+ html += `<li class="Box-row Box-row--hover-gray p-0"><a class="d-flex flex-items-center color-text-primary text-bold no-underline p-3" rel="noreferrer noopener nofollow" href=" ${ url [ i ] } "> ${ svg [ 0 ] } Download ZIP ${ download_url [ i ] [ 1 ] } </a></li>`
176+ }
177+ original . insertAdjacentHTML ( 'afterend' , html ) ;
177178 }
178179
179180
180181 // Git Clone
181182 function addGitClone ( ) {
182- $ ( " [role=' tabpanel' ] div.input-group" ) . first ( ) . each ( function ( ) {
183- let href_split = location . href . split ( "/" ) ,
184- url = [
185- clone_url [ 0 ] [ 0 ] + "/" + href_split [ 3 ] + "/" + href_split [ 4 ] + ".git" ,
186- clone_url [ 1 ] [ 0 ] + "/github.com /" + href_split [ 3 ] + "/" + href_split [ 4 ] + ".git" ,
187- clone_url [ 2 ] [ 0 ] + "/" + href_split [ 3 ] + "/" + href_split [ 4 ] + ".git"
188- ] ,
189- html = `` ;
190- for ( let i = 0 ; i < url . length ; i ++ )
191- {
192- html += `<div class="input-group" style="margin-top: 4px;" title="加速源: ${ clone_url [ i ] [ 1 ] } (点击可直接复制)"><input value=" ${ url [ i ] } " aria-label=" ${ url [ i ] } " type="text" class="form-control input-monospace input-sm bg-gray-light" data-autoselect="" readonly=""><div class="input-group-button"><clipboard-copy value=" ${ url [ i ] } " aria-label="Copy to clipboard" class="btn btn-sm" tabindex="0" role="button"> ${ svg [ 1 ] } </clipboard-copy></div></div>`
193- }
194- $ ( this ) . after ( html ) ;
195- } ) ;
183+ let original = document . querySelector ( ' [role=" tabpanel" ] div.input-group' ) ;
184+ if ( ! original ) return
185+ let href_split = location . href . split ( "/" ) ,
186+ url = [
187+ clone_url [ 0 ] [ 0 ] + "/" + href_split [ 3 ] + "/" + href_split [ 4 ] + ".git" ,
188+ clone_url [ 1 ] [ 0 ] + "/github.com/ " + href_split [ 3 ] + "/" + href_split [ 4 ] + ".git" ,
189+ clone_url [ 2 ] [ 0 ] + "/" + href_split [ 3 ] + "/" + href_split [ 4 ] + ".git"
190+ ] ,
191+ html = `` ;
192+ for ( let i = 0 ; i < url . length ; i ++ )
193+ {
194+ html += `<div class="input-group" style="margin-top: 4px;" title="加速源: ${ clone_url [ i ] [ 1 ] } (点击可直接复制)"><input value=" ${ url [ i ] } " aria-label=" ${ url [ i ] } " type="text" class="form-control input-monospace input-sm bg-gray-light" data-autoselect="" readonly=""><div class="input-group-button"><clipboard-copy value=" ${ url [ i ] } " aria-label="Copy to clipboard" class="btn btn-sm" tabindex="0" role="button"> ${ svg [ 1 ] } </clipboard-copy></div></div>`
195+ }
196+ original . insertAdjacentHTML ( 'afterend' , html ) ;
196197 }
197198
198199
199200 // Raw
200201 function addRawFile ( ) {
201- $ ( "# raw-url" ) . each ( function ( ) {
202- let href = location . href . replace ( 'https://github.com' , '' ) ,
203- href2 = href . replace ( '/blob/ ' , '/ ' ) ,
204- url = [
205- raw_url [ 1 ] [ 0 ] + href2 ,
206- raw_url [ 2 ] [ 0 ] + "/gh" + href . replace ( '/blob/' , '@' ) ,
207- raw_url [ 3 ] [ 0 ] + href2 ,
208- raw_url [ 4 ] [ 0 ] + "/" + raw_url [ 0 ] [ 0 ] + href2
209- ] ,
210- html = `` ;
211- for ( let i = 0 ; i < url . length ; i ++ )
212- {
213- html += `<a href=" ${ url [ i ] } " title=" ${ raw_url [ i + 1 ] [ 2 ] } " role="button" rel="noreferrer noopener nofollow" class="btn btn-sm BtnGroup-item"> ${ raw_url [ i + 1 ] [ 1 ] } </a>`
214- }
215- $ ( this ) . after ( html ) ;
216- } ) ;
202+ let original = document . getElementById ( ' raw-url' )
203+ if ( ! original ) return
204+ let href = location . href . replace ( 'https://github.com ' , '' ) ,
205+ href2 = href . replace ( '/blob/' , '/' ) ,
206+ url = [
207+ raw_url [ 1 ] [ 0 ] + href2 ,
208+ raw_url [ 2 ] [ 0 ] + "/gh" + href . replace ( '/blob/' , '@' ) ,
209+ raw_url [ 3 ] [ 0 ] + href2 ,
210+ raw_url [ 4 ] [ 0 ] + "/" + raw_url [ 0 ] [ 0 ] + href2
211+ ] ,
212+ html = `` ;
213+ for ( let i = 0 ; i < url . length ; i ++ )
214+ {
215+ html += `<a href=" ${ url [ i ] } " title=" ${ raw_url [ i + 1 ] [ 2 ] } " role="button" rel="noreferrer noopener nofollow" class="btn btn-sm BtnGroup-item"> ${ raw_url [ i + 1 ] [ 1 ] } </a>`
216+ }
217+ original . insertAdjacentHTML ( 'afterend' , html ) ;
217218 }
218219
219220
220221 // 添加 Raw 下载链接(☁)
221222 function addRawDownLink ( ) {
222223 // 如果不是项目文件页面,就返回
223- let files = $ ( 'div.Box-row svg.octicon.octicon-file' ) ;
224+ let files = document . querySelectorAll ( 'div.Box-row svg.octicon.octicon-file' ) ;
224225 if ( files . length === 0 ) return ;
225- let files1 = $ ( 'a.fileDownLink' ) ;
226+ let files1 = document . querySelectorAll ( 'a.fileDownLink' ) ;
226227 if ( files1 . length > 0 ) return ;
227228
228229 // 鼠标指向则显示
244245 } ;
245246
246247 // 循环添加
247- files . each ( function ( i , fileElm ) {
248+ files . forEach ( function ( fileElm , i ) {
248249 let trElm = fileElm . parentNode . parentNode ,
249250 cntElm_a = trElm . querySelector ( '.css-truncate.css-truncate-target.d-block.width-fit a' ) ,
250251 cntElm_svg = trElm . querySelector ( '.mr-3.flex-shrink-0 svg.octicon.octicon-file.color-icon-tertiary' ) ,
251252 Name = cntElm_a . innerText ,
252253 href = cntElm_a . attributes . href . nodeValue . replace ( 'https://github.com' , '' ) ;
253254 let href2 = href . replace ( '/blob/' , '/' ) , url , url_name , url_tip = '' ;
254- console . log ( cntElm_a , cntElm_svg , Name , href )
255255 switch ( menu_raw_fast )
256256 {
257257 case 2 :
271271 break ;
272272 }
273273 let html = ` <a href="${ url } " download="${ Name } " target="_blank" rel="noreferrer noopener nofollow" class="fileDownLink" style="display: none;" title="「${ url_name } 」 [Alt + 左键] 或 [右键 - 另存为...] 下载文件。 注意:鼠标点击 [☁] 图标,而不是左侧的文件名! ${ url_tip } 提示:点击浏览器右上角 Tampermonkey 扩展图标 - [ ${ raw_url [ menu_raw_fast ] [ 1 ] } ] 加速源 (☁) 即可切换。">${ svg [ 2 ] } </a>` ;
274- $ ( cntElm_svg ) . after ( html ) ;
274+ cntElm_svg . insertAdjacentHTML ( 'afterend' , html ) ;
275275 // 绑定鼠标事件
276- trElm . onmouseover = mouseOverHandler ;
277- trElm . onmouseout = mouseOutHandler ;
276+ trElm . onmouseover = mouseOverHandler ;
277+ trElm . onmouseout = mouseOutHandler ;
278278 } ) ;
279279 }
280280
283283 function delRawDownLink ( ) {
284284 let aElm = document . querySelectorAll ( '.fileDownLink' ) ;
285285 if ( aElm . length === 0 ) return ;
286- for ( let num = 0 ; num < aElm . length ; num ++ ) {
287- aElm [ num ] . remove ( ) ;
288- } ;
286+ aElm . forEach ( function ( fileElm ) {
287+ fileElm . remove ( )
288+ } )
289289 }
290290} ) ( ) ;
0 commit comments