Silkonomy

Redirect milkonomy URL

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Silkonomy
// @version      2.1
// @description  Redirect milkonomy URL
// @author       AlphB
// @match        https://milkonomy.pages.dev/*
// @match        https://milkonomy.cnnewnew.top/*
// @match        https://hyhfish.github.io/milkonomy/*
// @connect      www.milkywayidle.com
// @connect      test.milkywayidle.com
// @connect      www.milkywayidlecn.com
// @connect      test.milkywayidlecn.com
// @connect      alphb.cn
// @grant        GM.xmlHttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @icon         https://tupian.li/images/2025/09/30/68dae3cf1fa7e.png
// @license      CC-BY-NC-SA-4.0
// @namespace    http://tampermonkey.net/
// @run-at       document-start
// ==/UserScript==

(function () {
    if (!GM_getValue('URL')) {
        GM_setValue('URL', prompt('输入新URL', 'https://www.milkywayidlecn.com/game_data/marketplace.json'));
        alert('设置完成,请手动刷新页面以生效!');
        return;
    }
    const targetUrl = GM_getValue('URL');
    const originalFetch = window.fetch;
    let lastData;

    window.fetch = function (...args) {
        let input = args[0];
        let options = args[1] || {};

        let requestUrl;
        if (typeof input === 'string') {
            requestUrl = input;
        } else if (input instanceof Request) {
            requestUrl = input.url;
        } else {
            return originalFetch.apply(this, arguments);
        }

        if (requestUrl !== 'https://www.milkywayidle.com/game_data/marketplace.json' && requestUrl !== 'https://www.milkywayidlecn.com/game_data/marketplace.json') {
            return originalFetch.apply(this, arguments);
        }

        console.log(`Redirecting request: ${requestUrl} -> ${targetUrl}`);

        return new Promise((resolve) => {
            const requestData = {
                method: options.method || 'GET',
                url: targetUrl,
                headers: options.headers || {},
                data: options.body,
                responseType: 'json',
                timeout: options.timeout || 0
            };

            requestData.onload = function (response) {
                delete localStorage["game-market-data"];
                const responseData = response.response;
                responseData.timestamp = new Date().getTime() / 1000;
                lastData = JSON.stringify(responseData);
                const fetchResponse = new Response(
                    response.responseText ? JSON.stringify(responseData) : null,
                    {
                        status: response.status,
                        statusText: response.statusText
                    }
                );
                resolve(fetchResponse);
            };

            GM.xmlHttpRequest(requestData);
        });
    };
    unsafeWindow.fetch = window.fetch;

    (function showButton() {
        const button1 = document.createElement('button');

        button1.textContent = '打印当前API内容';

        button1.style.position = 'fixed';
        button1.style.bottom = '20px';
        button1.style.right = '20px';
        button1.style.zIndex = '9999';

        button1.addEventListener('click', () => {
            alert('数据源:' + targetUrl);
            alert(lastData);
        });

        const button2 = document.createElement('button');

        button2.textContent = '重设URL';

        button2.style.position = 'fixed';
        button2.style.bottom = '40px';
        button2.style.right = '20px';
        button2.style.zIndex = '9999';

        button2.addEventListener('click', () => {
            GM_setValue('URL', prompt('输入新URL', 'https://www.milkywayidlecn.com/game_data/marketplace.json'));
            alert('设置完成,请手动刷新页面以生效!');
        });

        window.addEventListener('load', () => {
            document.body.appendChild(button1);
            document.body.appendChild(button2);
        });
    })();
})();