百度网盘默认按修改日期排序

在百度网盘页面自动按修改日期排序

目前为 2025-01-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         百度网盘默认按修改日期排序
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  在百度网盘页面自动按修改日期排序
// @author       15d23
// @match        *://pan.baidu.com/s/*
// @match        *://pan.baidu.com/share/*
// @description 2025/1/15 23:00:19
// @run-at       document-end
// @license      GPL
// ==/UserScript==

(function() {
    'use strict';

    // 等待页面加载完成
    function waitForElement(selector, callback, maxAttempts = 20) {
        let attempts = 0;

        function tryFind() {
            attempts++;
            if (document.querySelector(selector)) {
                console.log('找到元素:', selector);
                callback();
            } else {
                console.log('尝试次数:', attempts, '未找到元素:', selector);
                if (attempts < maxAttempts) {
                    setTimeout(tryFind, 500);
                } else {
                    console.log('达到最大尝试次数,未能找到元素:', selector);
                }
            }
        }

        tryFind();
    }


// 点击排序按钮
function clickSortButton() {
    console.log('开始查找排序按钮');

    // 使用 data-key="time" 作为选择器
    waitForElement('li[data-key="time"]', function() {
        // 找到时间排序按钮并点击
        let timeBtn = document.querySelector('li[data-key="time"]');
        if(timeBtn) {
            console.log('找到排序按钮,执行第一次点击');
            timeBtn.click();

            // 等待一下再次点击以确保降序排列(最新的在前面)
            setTimeout(() => {
                console.log('执行第二次点击');
                //timeBtn.click();
            }, 500);
        } else {
            console.log('未找到排序按钮');
        }
    });
}

    // 直接执行脚本,因为已经在document-end运行
    console.log('页面加载完成,开始执行脚本');
    clickSortButton();

    // 也监听URL变化,因为百度网盘是单页应用
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            console.log('检测到URL变化,重新执行排序');
            clickSortButton();
        }
    }).observe(document, {subtree: true, childList: true});

})();

QingJ © 2025

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