SCYS Content Modifier

修改API响应状态码和Token处理

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         SCYS Content Modifier
// @namespace    http://scys.com/
// @version      2.6
// @description  修改API响应状态码和Token处理
// @author       You
// @match        *://*.scys.com/*
// @grant        GM_xmlhttpRequest
// @connect      scys.com
// @run-at       document-start
// ==/UserScript==
// @license MIT

(function() {
    'use strict';

    const TARGET_APIS = [
        '/shengcai-web/client/fxb/listFxbSecondLabel',
        '/shengcai-web/client/fxb/listFxbZbUser',
        '/shengcai-web/client/fxb/listExhSecondLabel'
    ];

    const TOKEN_KEY = '__user_token.v3';

    function debug(msg) {
        console.log('[SCYS Debug]:', msg);
    }

    // 保持登录状态
    function maintainLogin() {
        const fakeToken = {
            token: 'fake_token_' + Date.now(),
            expires: Date.now() + 86400000
        };

        try {
            localStorage.setItem(TOKEN_KEY, JSON.stringify(fakeToken));
        } catch (e) {
            debug('保存token失败: ' + e.message);
        }

        // 定期检查token
        setInterval(() => {
            if (!localStorage.getItem(TOKEN_KEY)) {
                localStorage.setItem(TOKEN_KEY, JSON.stringify(fakeToken));
                debug('恢复token');
            }
        }, 1000);
    }

    // 拦截Promise.reject
    const originalReject = Promise.reject;
    Promise.reject = function(reason) {
        if (reason && reason.status === 401) {
            debug('拦截401 Promise.reject');
            return Promise.resolve({
                data: {
                    status: 200,
                    success: true,
                    message: "操作成功",
                    data: {}
                }
            });
        }
        return originalReject.call(this, reason);
    };

    // 拦截axios实例
    function interceptAxios() {
        const checkAndIntercept = () => {
            if (window._d) {
                debug('找到axios实例');

                // 拦截请求配置
                if (window._d.interceptors && window._d.interceptors.request) {
                    window._d.interceptors.request.use(
                        config => {
                            debug('拦截请求配置');
                            if (config.url && TARGET_APIS.some(api => config.url.includes(api))) {
                                config.validateStatus = function() {
                                    return true;
                                };
                            }
                            return config;
                        },
                        error => {
                            return Promise.reject(error);
                        }
                    );
                }

                // 拦截响应
                if (window._d.interceptors && window._d.interceptors.response) {
                    window._d.interceptors.response.use(
                        response => {
                            if (response.config && TARGET_APIS.some(api => response.config.url.includes(api))) {
                                debug('修改响应数据');
                                return {
                                    data: {
                                        status: 200,
                                        success: true,
                                        message: "操作成功",
                                        data: {}
                                    }
                                };
                            }
                            return response;
                        },
                        error => {
                            if (error.response && error.response.status === 401) {
                                debug('拦截401响应');
                                return Promise.resolve({
                                    data: {
                                        status: 200,
                                        success: true,
                                        message: "操作成功",
                                        data: {}
                                    }
                                });
                            }
                            return Promise.reject(error);
                        }
                    );
                }
            } else {
                setTimeout(checkAndIntercept, 100);
            }
        };

        checkAndIntercept();
    }

    // 拦截跳转
    function interceptRedirects() {
        // 拦截 setTimeout
        const originalSetTimeout = window.setTimeout;
        window.setTimeout = function(fn, delay) {
            if (typeof fn === 'string') {
                if (fn.includes('location') || fn.includes('/login') || fn.includes('/clean')) {
                    debug('阻止setTimeout中的跳转代码');
                    return;
                }
            } else if (typeof fn === 'function') {
                const wrappedFn = function() {
                    try {
                        fn.apply(this, arguments);
                    } catch (e) {
                        if (e.toString().includes('location')) {
                            debug('阻止setTimeout中的跳转');
                        } else {
                            throw e;
                        }
                    }
                };
                return originalSetTimeout.call(this, wrappedFn, delay);
            }
            return originalSetTimeout.apply(this, arguments);
        };

        // 拦截 history API
        const originalPushState = history.pushState;
        history.pushState = function() {
            if (arguments[2] && (arguments[2].includes('/login') || arguments[2].includes('/clean'))) {
                debug('阻止pushState跳转到: ' + arguments[2]);
                return;
            }
            return originalPushState.apply(this, arguments);
        };
    }

    // 初始化
    function init() {
        try {
            maintainLogin();
            interceptAxios();
            interceptRedirects();
            debug('脚本初始化完成');
        } catch (e) {
            debug('初始化失败: ' + e.message);
        }
    }

    // 确保最早执行
    init();

    // 监听页面加载完成
    window.addEventListener('load', () => {
        debug('页面加载完成,重新检查拦截');
        init();
    });
})();