Bilibili 综合搜索按时间排序

Sort Bilibili search results by date and display in a new table

目前為 2024-06-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Bilibili 综合搜索按时间排序
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Sort Bilibili search results by date and display in a new table
// @author       Zola
// @match        https://search.bilibili.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to convert date strings to Date objects
    function parseDate(dateStr) {
        const parts = dateStr.split('-').map(Number);
        if (parts.length === 2) { // Current year date format
            const currentYear = new Date().getFullYear();
            return new Date(currentYear, parts[0] - 1, parts[1]);
        } else if (parts.length === 3) { // Full date format with year
            return new Date(parts[0], parts[1] - 1, parts[2]);
        }
        return new Date(NaN); // Invalid date
    }

    // Function to sort search results by date
    function sortResults() {
        const items = Array.from(document.querySelectorAll('.bili-video-card'));
        const videoData = items.map(item => {
            const title = item.querySelector('.bili-video-card__info--tit').innerText.trim();
            const dateStr = item.querySelector('.bili-video-card__info--date').innerText.trim().replace('· ', '');
            const date = parseDate(dateStr);
            const link = item.querySelector('a').href;
            const thumbnail = item.querySelector('.bili-video-card__cover img').src;
            const author = item.querySelector('.bili-video-card__info--author').innerText.trim();
            return { title, date, link, thumbnail, author };
        });

        // Sort video data by date, latest first
        videoData.sort((a, b) => b.date - a.date);

        // Create new table to display sorted results
        const newTable = document.createElement('table');
        newTable.style.width = '100%';
        newTable.style.borderCollapse = 'collapse';
        newTable.innerHTML = `
            <thead>
                <tr>
                    <th style="border: 1px solid #ddd; padding: 8px;">Thumbnail</th>
                    <th style="border: 1px solid #ddd; padding: 8px;">Title</th>
                    <th style="border: 1px solid #ddd; padding: 8px;">Date</th>
                    <th style="border: 1px solid #ddd; padding: 8px;">Author</th>
                </tr>
            </thead>
            <tbody>
                ${videoData.map(video => `
                    <tr>
                        <td style="border: 1px solid #ddd; padding: 8px;"><img src="${video.thumbnail}" alt="Thumbnail" style="width: 100px;"></td>
                        <td style="border: 1px solid #ddd; padding: 8px;"><a href="${video.link}" target="_blank">${video.title}</a></td>
                        <td style="border: 1px solid #ddd; padding: 8px;">${video.date.toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit' })}</td>
                        <td style="border: 1px solid #ddd; padding: 8px;">${video.author}</td>
                    </tr>
                `).join('')}
            </tbody>
        `;

        // Replace original content with new table
        const container = document.querySelector('.video-list');
        if (container) {
            container.innerHTML = '';
            container.appendChild(newTable);
        }
    }

    // Execute the sort function after the page loads
    window.addEventListener('load', sortResults);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址