AutoDL 实例列表排序

Hook XMLHttpRequest 容器按时间倒序排列,并将运行中容器排在前面。

// ==UserScript==
// @name         AutoDL 实例列表排序
// @namespace    http://tampermonkey.net/
// @version      2025-03-29
// @description  Hook XMLHttpRequest 容器按时间倒序排列,并将运行中容器排在前面。
// @author       Ganlv
// @match        https://www.autodl.com/login*
// @match        https://www.autodl.com/subAccountLogin*
// @match        https://www.autodl.com/console*
// @match        https://www.autodl.com/deploy*
// @icon         https://www.autodl.com/favicon.png
// @require      https://unpkg.com/[email protected]/index.js
// @grant        unsafeWindow
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const originalXHR = unsafeWindow.XMLHttpRequest;
    unsafeWindow.XMLHttpRequest = function() {
        const xhr = new originalXHR();
        const originalOpen = xhr.open;
        const originalSend = xhr.send;
        xhr.open = function() {
            this.url = arguments[1];
            return originalOpen.apply(this, arguments);
        };
        xhr.send = function() {
            const originalOnReadyStateChange = xhr.onreadystatechange;
            xhr.onreadystatechange = function() {
                if (this.readyState === 4 && this.status === 200) {
                    if (this.url && (this.url.includes('/api/v1/sub_user/instance') || this.url.includes('/api/v1/instance'))) {
                        try {
                            const response = JSON.parse(this.responseText);
                            if (response.data && Array.isArray(response.data.list)) {
                                response.data.list.sort((a, b) => {
                                    if (a.status === 'shutdown' && b.status !== 'shutdown') {
                                        return 1;
                                    }
                                    if (a.status !== 'shutdown' && b.status === 'shutdown') {
                                        return -1;
                                    }
                                    return b.started_at.Time.localeCompare(a.started_at.Time);
                                });
                                Object.defineProperty(this, 'responseText', {
                                    get: function() {
                                        return JSON.stringify(response);
                                    }
                                });
                            }
                        } catch (e) {
                            console.error('Error processing response:', e);
                        }
                    }
                    if (this.url && (this.url.includes('/api/v1/sub_user/deployment/list') || this.url.includes('/api/v1/deployment/list'))) {
                        try {
                            const response = JSON.parse(this.responseText);
                            if (response.data && Array.isArray(response.data.list)) {
                                response.data.list.sort((a, b) => {
                                    return naturalCompare(a.name, b.name);
                                });
                                Object.defineProperty(this, 'responseText', {
                                    get: function() {
                                        return JSON.stringify(response);
                                    }
                                });
                            }
                        } catch (e) {
                            console.error('Error processing response:', e);
                        }
                    }
                }
                if (originalOnReadyStateChange) {
                    originalOnReadyStateChange.apply(this, arguments);
                }
            };
            return originalSend.apply(this, arguments);
        };
        return xhr;
    };
})();

QingJ © 2025

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