// ==UserScript== // @name Keylol Table Sort // @namespace http://tampermonkey.net/ // @version 0.1 // @description 对keylol琪露诺折扣贴表格按价格或折扣排序 // @author 冰雪聪明琪露诺 // @match https://keylol.com/t* // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; window.addEventListener('load', function() { const table = document.querySelector('.t_fsz table.t_table'); if (!table) return; const priceHeaderCell = Array.from(table.querySelector('tr').cells).find(cell => cell.textContent.includes('商店价格')); if (!priceHeaderCell) return; const optionsContainer = document.createElement('div'); optionsContainer.style.position = 'absolute'; optionsContainer.style.display = 'none'; optionsContainer.style.backgroundColor = 'white'; optionsContainer.style.border = '1px solid #ccc'; optionsContainer.style.padding = '5px'; optionsContainer.style.zIndex = '100'; const sortOptions = [ '按价格排序(升序)', '按价格排序(降序)', '按折扣排序(升序)', '按折扣排序(降序)' ]; sortOptions.forEach(optionText => { const option = document.createElement('div'); option.textContent = optionText; option.style.cursor = 'pointer'; option.style.padding = '5px'; option.addEventListener('click', () => { sortTable(table, optionText); optionsContainer.style.display = 'none'; }); optionsContainer.appendChild(option); }); priceHeaderCell.addEventListener('click', function() { const rect = this.getBoundingClientRect(); const scrollTop = window.pageYOffset || document.documentElement.scrollTop; const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft; optionsContainer.style.left = rect.left + scrollLeft + 'px'; optionsContainer.style.top = rect.bottom + scrollTop + 'px'; optionsContainer.style.display = optionsContainer.style.display === 'none' ? 'block' : 'none'; }); document.body.appendChild(optionsContainer); }); function sortTable(table, optionText) { const rows = Array.from(table.rows).slice(1); // 排除表头 const priceIndex = Array.from(table.rows[0].cells).findIndex(cell => cell.textContent.includes('商店价格')); let sortFunction; if (optionText === '按价格排序(升序)') { sortFunction = (a, b) => { const priceA = parseFloat(a.cells[priceIndex].textContent.match(/¥(\d+\.\d+)/)[1]); const priceB = parseFloat(b.cells[priceIndex].textContent.match(/¥(\d+\.\d+)/)[1]); return priceA - priceB; }; } else if (optionText === '按价格排序(降序)') { sortFunction = (a, b) => { const priceA = parseFloat(a.cells[priceIndex].textContent.match(/¥(\d+\.\d+)/)[1]); const priceB = parseFloat(b.cells[priceIndex].textContent.match(/¥(\d+\.\d+)/)[1]); return priceB - priceA; }; } else if (optionText === '按折扣排序(升序)') { sortFunction = (a, b) => { const discountA = parseFloat(a.cells[priceIndex].textContent.match(/-(\d+)%/)[1]); const discountB = parseFloat(b.cells[priceIndex].textContent.match(/-(\d+)%/)[1]); return discountA - discountB; }; } else if (optionText === '按折扣排序(降序)') { sortFunction = (a, b) => { const discountA = parseFloat(a.cells[priceIndex].textContent.match(/-(\d+)%/)[1]); const discountB = parseFloat(b.cells[priceIndex].textContent.match(/-(\d+)%/)[1]); return discountB - discountA; }; } rows.sort(sortFunction); while (table.rows.length > 1) { table.deleteRow(1); } rows.forEach(row => table.appendChild(row)); } })();