3939 [ 'menu_refreshCorrection' , '刷新不返回根目录' , '刷新不返回根目录' , true ] ,
4040 [ 'menu_rightClickMenu' , '右键文件显示菜单' , '右键文件显示菜单' , true ] ,
4141 [ 'menu_directDownload' , '点击直接下载文件 (分享链接列表页)' , '点击直接下载文件' , true ] ,
42- [ 'menu_folderDescdesMenu' , '调整描述(话说)编辑框大小' , '调整描述(话说)编辑框大小' , true ]
42+ [ 'menu_folderDescdesMenu' , '调整描述(话说)编辑框大小' , '调整描述(话说)编辑框大小' , true ] ,
43+ [ 'menu_fileSort' , '文件排序' , '文件排序' , true ]
4344 ] , menu_ID = [ ] , lastFolderID ;
4445 for ( let i = 0 ; i < menu_ALL . length ; i ++ ) { // 如果读取到的值为 null 就写入默认值
4546 if ( GM_getValue ( menu_ALL [ i ] [ 0 ] ) == null ) { GM_setValue ( menu_ALL [ i ] [ 0 ] , menu_ALL [ i ] [ 3 ] ) } ;
133134 dragEnter ( ) ; // 拖入文件自动显示上传框
134135 setTimeout ( viewTop , 1000 ) ; // 监听并修改右键菜单 [外链分享地址] 为 [复制并打开分享链接] / [复制分享链接] / [打开分享链接] 之一
135136 setTimeout ( copyAllfileSha , 500 ) ; // 一键复制所有分享链接
137+
138+ setTimeout ( filesSort , 200 ) ; // 文件排序
136139 }
137140 }
138141
572575 const observer = new MutationObserver ( callback ) ;
573576 observer . observe ( mainframe . document , { childList : true , subtree : true } ) ;
574577 }
578+
579+ // 文件排序
580+ function filesSort ( ) {
581+ const frame = mainframe ;
582+ const frameDoc = frame . document ;
583+
584+ const currentStatus = {
585+ by : 'name' ,
586+ order : 'asc' ,
587+ }
588+
589+ let allButtons = undefined ;
590+
591+ // 文件名排序
592+ function sortByName ( name1 , name2 ) {
593+ function sort ( a , b ) {
594+ const strA = a . toLocaleLowerCase ( ) . split ( '' ) ;
595+ const strB = b . toLocaleLowerCase ( ) . split ( '' ) ;
596+ for ( let i = 0 ; i < strA . length ; i ++ ) {
597+ if ( strA [ i ] === strB [ i ] ) {
598+ continue ;
599+ }
600+ if ( notChinese ( strA [ i ] ) && notChinese ( strB [ i ] ) ) {
601+ return strA [ i ] < strB [ i ] ? - 1 : 1 ;
602+ } else if ( notChinese ( strA [ i ] ) ) {
603+ return - 1 ;
604+ } else if ( notChinese ( strB [ i ] ) ) {
605+ return 1 ;
606+ } else {
607+ return strA [ i ] . localeCompare ( strB [ i ] ) ;
608+ }
609+ }
610+ if ( strA . length === strB . length ) {
611+ return 0 ;
612+ } else if ( strA . length < strB . length ) {
613+ return 1 ;
614+ }
615+ return - 1 ;
616+ }
617+ function notChinese ( char ) {
618+ const charCode = char . charCodeAt ( 0 )
619+ return charCode >= 0 && charCode <= 128
620+ }
621+ return sort ( name1 , name2 ) ;
622+ }
623+
624+ // 创建排序按鈕
625+ function createAllButtons ( ) {
626+ if ( ! menu_value ( 'menu_fileSort' ) ) return ;
627+ const tabTitle = frameDoc . querySelector ( '#container > div.n1 > div.f_th' ) ;
628+ const name = tabTitle . querySelector ( 'div.f_name' ) ;
629+ const size = tabTitle . querySelector ( 'div.f_size' ) ;
630+ const time = tabTitle . querySelector ( 'div.f_time' ) ;
631+ const down = tabTitle . querySelector ( 'div.f_down' ) ; // 下载量
632+ return {
633+ name : {
634+ el : createButton ( name , 'name' ) ,
635+ order : 'asc' ,
636+ } ,
637+ size : {
638+ el : createButton ( size , 'size' ) ,
639+ order : 'asc' ,
640+ } ,
641+ time : {
642+ el : createButton ( time , 'time' ) ,
643+ order : 'asc' ,
644+ } ,
645+ down : {
646+ el : createButton ( down , 'down' ) ,
647+ order : 'asc' ,
648+ }
649+ } ;
650+ }
651+ function createButton ( element , by ) {
652+ // element.insertAdjacentHTML('beforeend', '<a class="col_sort_btn" href="javascript: void;" style="font-size: 16px; float: right;">⇧</a>');
653+ let button = frameDoc . createElement ( 'a' ) ;
654+ button . className = 'col_sort_btn' ;
655+ button . href = 'javascript: void(0);' ;
656+ button . style . fontSize = '16px' ;
657+ button . style . float = 'right' ;
658+ button . textContent = '⇧' ;
659+ button . onclick = ( ) => clickSortButton ( by , button ) ;
660+ element . appendChild ( button ) ;
661+ return button ;
662+ }
663+
664+
665+ function getFiles ( ) {
666+ const list = frameDoc . querySelector ( '#filelist' ) ;
667+ const files = list . childNodes ;
668+ const filesInfo = [ ] ;
669+ const now = new Date ( ) ;
670+ for ( const file of files ) {
671+ const name = file . querySelector ( '.f_name > span.aspanlink > .f_name_title' ) . textContent ;
672+ const size = parseByteSize ( file . querySelector ( '.f_size' ) . textContent ) ;
673+ const time = file . querySelector ( '.f_time' ) . textContent ;
674+ const down = parseInt ( file . querySelector ( '.f_down' ) . textContent ) ;
675+ filesInfo . push ( {
676+ info : {
677+ name : name ,
678+ size : size ,
679+ time : parseTime ( now , time ) ,
680+ down : down
681+ } ,
682+ node : file
683+ } )
684+ }
685+ return filesInfo ;
686+ }
687+ // 转换文件大小
688+ function parseByteSize ( text ) {
689+ const unit = [ 'B' , 'K' , 'M' , 'G' , 'T' ] ;
690+ let size = 0 ;
691+ for ( let i = 0 ; i < unit . length ; i ++ ) {
692+ if ( text . indexOf ( unit [ i ] ) > - 1 ) {
693+ size = parseFloat ( text . replace ( unit [ i ] , '' ) ) * Math . pow ( 1024 , i ) ;
694+ break ;
695+ }
696+ }
697+ return size ;
698+ }
699+ // 转换时间格式
700+ function parseTime ( now , text ) {
701+ if ( text . indexOf ( '秒前' ) > - 1 ) {
702+ return now - parseInt ( text . replace ( '秒前' , '' ) ) * 1000 ;
703+ } else if ( text . indexOf ( '分钟前' ) > - 1 ) {
704+ return now - parseInt ( text . replace ( '分钟前' , '' ) ) * 60 * 1000 ;
705+ } else if ( text . indexOf ( '小时前' ) > - 1 ) {
706+ return now - parseInt ( text . replace ( '小时前' , '' ) ) * 60 * 60 * 1000 ;
707+ } else if ( text . indexOf ( '天前' ) > - 1 ) {
708+ return now - parseInt ( text . replace ( '天前' , '' ) ) * 24 * 60 * 60 * 1000 ; // 我不知道有没有以下几种情况,暂时先写上
709+ } else if ( text . indexOf ( '月前' ) > - 1 ) {
710+ return now - parseInt ( text . replace ( '月前' , '' ) ) * 30 * 24 * 60 * 60 * 1000 ;
711+ } else if ( text . indexOf ( '年前' ) > - 1 ) {
712+ return now - parseInt ( text . replace ( '年前' , '' ) ) * 365 * 24 * 60 * 60 * 1000 ;
713+ }
714+ return Date . parse ( text ) ;
715+ }
716+
717+ // 排序
718+ function sortFiles ( files , by , order ) {
719+ let compareFunc ;
720+ if ( by === 'name' ) {
721+ compareFunc = ( a , b ) => {
722+ return ( order == 'asc' ? 1 : - 1 ) * sortByName ( a . info . name , b . info . name ) ;
723+ }
724+ } else {
725+ compareFunc = ( a , b ) => {
726+ const result = a . info [ by ] > b . info [ by ] ? 1 : - 1 ;
727+ return ( order == 'asc' ? 1 : - 1 ) * result ;
728+ }
729+ }
730+ files . sort ( compareFunc ) ;
731+ }
732+
733+ function sortFileList ( ) {
734+ const files = getFiles ( ) ;
735+ if ( files . length > 0 ) {
736+ sortFiles ( files , currentStatus . by , currentStatus . order ) ;
737+ // console.log(files)
738+ const fileList = frameDoc . querySelector ( '#filelist' ) ;
739+ for ( let i = 0 ; i < files . length ; i ++ ) {
740+ fileList . appendChild ( files [ i ] . node ) ;
741+ }
742+ }
743+ }
744+
745+ function fileListCallback ( records ) {
746+ if ( ! menu_value ( 'menu_fileSort' ) ) return ;
747+ for ( const event of records ) {
748+ // 自己修改的时候不排序
749+ if ( event . removedNodes . length > 0 ) return ;
750+ }
751+
752+ sortFileList ( ) ;
753+
754+ }
755+
756+ function clickSortButton ( by , button ) {
757+ if ( currentStatus . by === by ) {
758+ currentStatus . order = button . textContent === '⬆' ? 'desc' : 'asc' ;
759+ } else {
760+ currentStatus . order = button . textContent === '⇧' ? 'asc' : 'desc' ;
761+ for ( const key in allButtons ) {
762+ if ( key === by ) continue ;
763+ if ( Object . hasOwnProperty . call ( allButtons , key ) ) {
764+ const btnItem = allButtons [ key ] ;
765+ if ( btnItem . el . textContent === '⬆' ) {
766+ btnItem . el . textContent = '⇧' ;
767+ } else if ( btnItem . el . textContent === '⬇' ) {
768+ btnItem . el . textContent = '⇩' ;
769+ }
770+ }
771+ }
772+ }
773+ button . textContent = currentStatus . order === 'asc' ? '⬆' : '⬇' ;
774+ currentStatus . by = by ;
775+ sortFileList ( ) ;
776+
777+ }
778+
779+ // create buttons
780+ setTimeout ( ( ) => {
781+ if ( allButtons === undefined ) {
782+ allButtons = createAllButtons ( ) ;
783+ allButtons [ currentStatus . by ] . el . textContent = '⬆' ;
784+ }
785+ } , 500 ) ;
786+
787+ // sort files
788+ const fileList = frameDoc . querySelector ( '#filelist' ) ;
789+ const observer = new MutationObserver ( fileListCallback ) ;
790+ observer . observe ( fileList , { childList : true , attributes : false } ) ;
791+
792+
793+ }
575794} ) ( ) ;
0 commit comments