AutoDL 实例列表排序

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AutoDL 实例列表排序
// @namespace    http://tampermonkey.net/
// @version      2025-05-19
// @description  Hook XMLHttpRequest 容器按时间倒序排列,并将运行中容器排在前面。
// @author       Ganlv
// @match        https://www.autodl.com/*
// @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) => {
                                    if (a.replica_num == 0 && b.replica_num == 0) {
                                        return naturalCompare(a.name, b.name);
                                    } else if (a.replica_num == 0) {
                                        return 1
                                    } else if (b.replica_num == 0) {
                                        return -1
                                    }
                                    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;
    };
})();