From 9ff4a42cd01bf51a0e30a18bb539e6ee18bac13c Mon Sep 17 00:00:00 2001 From: ywxt Date: Sun, 21 Aug 2022 03:27:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lanzou-Enhanced.user.js | 221 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 1 deletion(-) diff --git a/Lanzou-Enhanced.user.js b/Lanzou-Enhanced.user.js index b1b80d7d4..ecfee9c27 100644 --- a/Lanzou-Enhanced.user.js +++ b/Lanzou-Enhanced.user.js @@ -39,7 +39,8 @@ ['menu_refreshCorrection', '刷新不返回根目录', '刷新不返回根目录', true], ['menu_rightClickMenu', '右键文件显示菜单', '右键文件显示菜单', true], ['menu_directDownload', '点击直接下载文件 (分享链接列表页)', '点击直接下载文件', true], - ['menu_folderDescdesMenu', '调整描述(话说)编辑框大小', '调整描述(话说)编辑框大小', true] + ['menu_folderDescdesMenu', '调整描述(话说)编辑框大小', '调整描述(话说)编辑框大小', true], + ['menu_fileSort', '文件排序', '文件排序', true] ], menu_ID = [], lastFolderID; for (let i=0;i= 0 && charCode <= 128 + } + return sort(name1, name2); + } + + // 创建排序按鈕 + function createAllButtons() { + if (!menu_value('menu_fileSort')) return; + const tabTitle = frameDoc.querySelector('#container > div.n1 > div.f_th'); + const name = tabTitle.querySelector('div.f_name'); + const size = tabTitle.querySelector('div.f_size'); + const time = tabTitle.querySelector('div.f_time'); + const down = tabTitle.querySelector('div.f_down'); // 下载量 + return { + name: { + el: createButton(name, 'name'), + order: 'asc', + }, + size: { + el: createButton(size, 'size'), + order: 'asc', + }, + time: { + el: createButton(time, 'time'), + order: 'asc', + }, + down: { + el: createButton(down, 'down'), + order: 'asc', + } + }; + } + function createButton(element, by) { + // element.insertAdjacentHTML('beforeend', ''); + let button = frameDoc.createElement('a'); + button.className = 'col_sort_btn'; + button.href = 'javascript: void(0);'; + button.style.fontSize = '16px'; + button.style.float = 'right'; + button.textContent = '⇧'; + button.onclick = () => clickSortButton(by, button); + element.appendChild(button); + return button; + } + + + function getFiles() { + const list = frameDoc.querySelector('#filelist'); + const files = list.childNodes; + const filesInfo = []; + const now = new Date(); + for (const file of files) { + const name = file.querySelector('.f_name > span.aspanlink > .f_name_title').textContent; + const size = parseByteSize(file.querySelector('.f_size').textContent); + const time = file.querySelector('.f_time').textContent; + const down = parseInt(file.querySelector('.f_down').textContent); + filesInfo.push({ + info: { + name: name, + size: size, + time: parseTime(now, time), + down: down + }, + node: file + }) + } + return filesInfo; + } + // 转换文件大小 + function parseByteSize(text) { + const unit = ['B', 'K', 'M', 'G', 'T']; + let size = 0; + for (let i = 0; i < unit.length; i++) { + if (text.indexOf(unit[i]) > -1) { + size = parseFloat(text.replace(unit[i], '')) * Math.pow(1024, i); + break; + } + } + return size; + } + // 转换时间格式 + function parseTime(now, text) { + if (text.indexOf('秒前') > -1) { + return now - parseInt(text.replace('秒前', '')) * 1000; + } else if (text.indexOf('分钟前') > -1) { + return now - parseInt(text.replace('分钟前', '')) * 60 * 1000; + } else if (text.indexOf('小时前') > -1) { + return now - parseInt(text.replace('小时前', '')) * 60 * 60 * 1000; + } else if (text.indexOf('天前') > -1) { + return now - parseInt(text.replace('天前', '')) * 24 * 60 * 60 * 1000; // 我不知道有没有以下几种情况,暂时先写上 + } else if (text.indexOf('月前') > -1) { + return now - parseInt(text.replace('月前', '')) * 30 * 24 * 60 * 60 * 1000; + } else if (text.indexOf('年前') > -1) { + return now - parseInt(text.replace('年前', '')) * 365 * 24 * 60 * 60 * 1000; + } + return Date.parse(text); + } + + // 排序 + function sortFiles(files, by, order) { + let compareFunc; + if (by === 'name') { + compareFunc = (a, b) => { + return (order == 'asc' ? 1 : -1) * sortByName(a.info.name, b.info.name); + } + } else { + compareFunc = (a, b) => { + const result = a.info[by] > b.info[by] ? 1 : -1; + return (order == 'asc' ? 1 : -1) * result; + } + } + files.sort(compareFunc); + } + + function sortFileList() { + const files = getFiles(); + if (files.length > 0) { + sortFiles(files, currentStatus.by, currentStatus.order); + // console.log(files) + const fileList = frameDoc.querySelector('#filelist'); + for (let i = 0; i < files.length; i++) { + fileList.appendChild(files[i].node); + } + } + } + + function fileListCallback(records) { + if (!menu_value('menu_fileSort')) return; + for (const event of records) { + // 自己修改的时候不排序 + if (event.removedNodes.length > 0) return; + } + + sortFileList(); + + } + + function clickSortButton(by, button) { + if (currentStatus.by === by) { + currentStatus.order = button.textContent === '⬆' ? 'desc' : 'asc'; + } else { + currentStatus.order = button.textContent === '⇧' ? 'asc' : 'desc'; + for (const key in allButtons) { + if (key === by) continue; + if (Object.hasOwnProperty.call(allButtons, key)) { + const btnItem = allButtons[key]; + if (btnItem.el.textContent === '⬆') { + btnItem.el.textContent = '⇧'; + } else if (btnItem.el.textContent === '⬇') { + btnItem.el.textContent = '⇩'; + } + } + } + } + button.textContent = currentStatus.order === 'asc' ? '⬆' : '⬇'; + currentStatus.by = by; + sortFileList(); + + } + + // create buttons + setTimeout(() => { + if (allButtons === undefined) { + allButtons = createAllButtons(); + allButtons[currentStatus.by].el.textContent = '⬆'; + } + }, 500); + + // sort files + const fileList = frameDoc.querySelector('#filelist'); + const observer = new MutationObserver(fileListCallback); + observer.observe(fileList, { childList: true, attributes: false }); + + + } })(); \ No newline at end of file From 62ca690301a45b6d252abf58243c0fffad509b84 Mon Sep 17 00:00:00 2001 From: ywxt Date: Sun, 21 Aug 2022 04:36:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lanzou-Enhanced.user.js | 71 ++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/Lanzou-Enhanced.user.js b/Lanzou-Enhanced.user.js index ecfee9c27..526867124 100644 --- a/Lanzou-Enhanced.user.js +++ b/Lanzou-Enhanced.user.js @@ -135,7 +135,7 @@ setTimeout(viewTop,1000); // 监听并修改右键菜单 [外链分享地址] 为 [复制并打开分享链接] / [复制分享链接] / [打开分享链接] 之一 setTimeout(copyAllfileSha, 500); // 一键复制所有分享链接 - setTimeout(filesSort, 200); // 文件排序 + setTimeout(filesSort, 300); // 文件排序 } } @@ -597,6 +597,7 @@ if (strA[i] === strB[i]) { continue; } + if (strB[i] === undefined) return 1; if (notChinese(strA[i]) && notChinese(strB[i])) { return strA[i] < strB[i] ? -1 : 1; } else if (notChinese(strA[i])) { @@ -610,9 +611,9 @@ if (strA.length === strB.length) { return 0; } else if (strA.length < strB.length) { - return 1; + return -1; } - return -1; + return 1; } function notChinese(char) { const charCode = char.charCodeAt(0) @@ -623,7 +624,6 @@ // 创建排序按鈕 function createAllButtons() { - if (!menu_value('menu_fileSort')) return; const tabTitle = frameDoc.querySelector('#container > div.n1 > div.f_th'); const name = tabTitle.querySelector('div.f_name'); const size = tabTitle.querySelector('div.f_size'); @@ -684,6 +684,22 @@ } return filesInfo; } + + function getFolders() { + const list = frameDoc.querySelector('#sub_folder_list'); + const folders = list.childNodes; + const foldersInfo = []; + for (const folder of folders) { + const name = folder.querySelector('.f_tb > .f_name2 > span.follink > span').textContent; + foldersInfo.push({ + info: { + name: name, + }, + node: folder + }) + } + return foldersInfo; + } // 转换文件大小 function parseByteSize(text) { const unit = ['B', 'K', 'M', 'G', 'T']; @@ -715,16 +731,16 @@ } // 排序 - function sortFiles(files, by, order) { + function sortItems(files, by, order) { let compareFunc; if (by === 'name') { compareFunc = (a, b) => { - return (order == 'asc' ? 1 : -1) * sortByName(a.info.name, b.info.name); + return (order === 'asc' ? 1 : -1) * sortByName(a.info.name, b.info.name); } } else { compareFunc = (a, b) => { const result = a.info[by] > b.info[by] ? 1 : -1; - return (order == 'asc' ? 1 : -1) * result; + return (order === 'asc' ? 1 : -1) * result; } } files.sort(compareFunc); @@ -733,7 +749,7 @@ function sortFileList() { const files = getFiles(); if (files.length > 0) { - sortFiles(files, currentStatus.by, currentStatus.order); + sortItems(files, currentStatus.by, currentStatus.order); // console.log(files) const fileList = frameDoc.querySelector('#filelist'); for (let i = 0; i < files.length; i++) { @@ -742,15 +758,39 @@ } } + function sortFolderList() { + const folders = getFolders(); + // console.log(folders); + if (folders.length > 0) { + // 文件夹只能按名称排序 + if (currentStatus.by === 'name') { + sortItems(folders, 'name', currentStatus.order); + } else { + sortItems(folders, 'name', 'asc'); // 其他情况,皆按名称升序 + } + const folderList = frameDoc.querySelector('#sub_folder_list'); + for (let i = 0; i < folders.length; i++) { + folderList.appendChild(folders[i].node); + } + } + } + function fileListCallback(records) { if (!menu_value('menu_fileSort')) return; for (const event of records) { // 自己修改的时候不排序 if (event.removedNodes.length > 0) return; } - sortFileList(); + } + function folderListCallback(records) { + if (!menu_value('menu_fileSort')) return; + for (const event of records) { + // 自己修改的时候不排序 + if (event.removedNodes.length > 0) return; + } + sortFolderList(); } function clickSortButton(by, button) { @@ -773,21 +813,28 @@ button.textContent = currentStatus.order === 'asc' ? '⬆' : '⬇'; currentStatus.by = by; sortFileList(); + sortFolderList(); } // create buttons setTimeout(() => { - if (allButtons === undefined) { + if (allButtons === undefined && menu_value('menu_fileSort')) { allButtons = createAllButtons(); + // console.log(allButtons); allButtons[currentStatus.by].el.textContent = '⬆'; } }, 500); // sort files const fileList = frameDoc.querySelector('#filelist'); - const observer = new MutationObserver(fileListCallback); - observer.observe(fileList, { childList: true, attributes: false }); + const fileObserver = new MutationObserver(fileListCallback); + fileObserver.observe(fileList, { childList: true, attributes: false }); + + // sort files + const folderList = frameDoc.querySelector('#sub_folder_list');; + const folderObserver = new MutationObserver(folderListCallback); + folderObserver.observe(folderList, { childList: true, attributes: false }); } From 28045234614b68ebc843495417cd479140630a23 Mon Sep 17 00:00:00 2001 From: ywxt Date: Sun, 21 Aug 2022 04:45:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lanzou-Enhanced.user.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lanzou-Enhanced.user.js b/Lanzou-Enhanced.user.js index 526867124..7422c228b 100644 --- a/Lanzou-Enhanced.user.js +++ b/Lanzou-Enhanced.user.js @@ -795,9 +795,12 @@ function clickSortButton(by, button) { if (currentStatus.by === by) { + // 当前选项,点击时,反转排序 currentStatus.order = button.textContent === '⬆' ? 'desc' : 'asc'; } else { + // 非当前选项,点击时,以图标所示的顺序排序 currentStatus.order = button.textContent === '⇧' ? 'asc' : 'desc'; + // 修改非当前选项按钮的图标 for (const key in allButtons) { if (key === by) continue; if (Object.hasOwnProperty.call(allButtons, key)) { @@ -831,7 +834,7 @@ const fileObserver = new MutationObserver(fileListCallback); fileObserver.observe(fileList, { childList: true, attributes: false }); - // sort files + // sort folders const folderList = frameDoc.querySelector('#sub_folder_list');; const folderObserver = new MutationObserver(folderListCallback); folderObserver.observe(folderList, { childList: true, attributes: false });