Greasy Fork

Greasy Fork is available in English.

POE2 trade 繁体

POE2 trade 繁体化

当前为 2024-12-09 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         POE2 trade 繁体
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  POE2 trade 繁体化
// @author       放课后
// @match        https://www.pathofexile.com/trade2/*
// @grant        GM_xmlhttpRequest
// @license MIT
// ==/UserScript==


(async () => {

    'use strict';

const dataMap = {}
    const urls = [
        'https://pathofexile.tw/api/trade2/data/stats',
        'https://pathofexile.tw/api/trade2/data/static',
        // 'https://pathofexile.tw/api/trade2/data/items',
        'https://pathofexile.tw/api/trade2/data/filters'
    ];

    const getResult = (url) => {
        return new Promise(resolve => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: url,
                onload: function(response) {
                    if (response.status === 200) {
                        try {
                            const data = JSON.parse(response.responseText);
                            console.log(`Data from ${url} stored successfully.`);
                            resolve(data);
                        } catch (e) {
                            console.error(`Error parsing data from ${url}:`, e);
                            resolve(null);
                        }
                    } else {
                        console.error(`Failed to fetch data from ${url}:`, response.status);
                        resolve(null);
                    }
                },
                onerror: function(error) {
                    console.error(`Request to ${url} failed:`, error);
                    resolve(null);
                }
            });
        });
    };

    for (const url of urls) {
        const {result} = await getResult(url);
        const key = url.split('/').pop();
        if (result && result.length) {
            if (key === 'static') {
                localStorage.setItem('lscache-trade2data', JSON.stringify(result));
            } else {
                localStorage.setItem('lscache-trade2' + key, JSON.stringify(result));
            }
            dataMap[key] = result
        }
    }
    const enTradeItems2Tw = await getResult('https://p2.710421059.xyz/enTradeItems2Tw.json');
    const {result} = await getResult('https://www.pathofexile.com/api/trade2/data/items');
    if (result) {
        for (let i = 0; i < result.length; i++) {
            try {
                result[i].label = enTradeItems2Tw[i].label;
            } catch (e) {
                console.error(`Error processing item ${i}:`, e);
            }
            if (result[i].entries) {
                for (let j = 0; j < result[i].entries.length; j++) {
                    const find = enTradeItems2Tw[i].entries.find(a => a.type === result[i].entries[j].type && (result[i].entries[j].name ? a.name === result[i].entries[j].name : true));
                    if (find) {
                        result[i].entries[j].text = find.text;
                    }
                }
            }
        }
        localStorage.setItem('lscache-trade2items', JSON.stringify(result));
    }

    const originalOpen = XMLHttpRequest.prototype.open;
    const originalSend = XMLHttpRequest.prototype.send;

    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        this._url = url; // 存储请求的 URL
        return originalOpen.apply(this, arguments);
    };

    XMLHttpRequest.prototype.send = function(body) {
        this.addEventListener('readystatechange', function() {
            if (this.readyState === 4 && this.status === 200) {
                try {
                    const response = JSON.parse(this.responseText);

                    if (this._url.includes('api/trade2/fetch') && dataMap['stats'] && dataMap['stats'].length) {
                        response.result.forEach(item => {
                            if(item.item.extended.hashes){
                                const keys = Object.keys(item.item.extended.hashes)

                                keys.forEach(key => {
                                    const mods = item.item.extended.hashes[key]
                                    const entry = dataMap['stats'].find(a => a.id == key)
                                    const modTexts = item.item[key+'Mods']

                                    if(entry && entry.entries && modTexts){
                                        let index = 0
                                        const newModTexts = mods.map(m => {
                                            const oldText =  modTexts[index++]
                                            const mod = entry.entries.find(a => a.id == m[0])
                                            if(mod){
                                                let newModText = mod.text
                                                const values = oldText.match(/\d*\.\d+|\d+/g)
                                                if(values){
                                                    let i = 0
                                                    values.forEach(v => {
                                                        newModText = newModText.replace(/#/,values[i++])

                                                    })
                                                }
                                                return `${newModText}(${ oldText})`
                                                console.log(values)
                                            }
                                            return oldText
                                        })
                                        item.item[key+'Mods'] = newModTexts
                                    }

                                })
                            }

                        })
                    }

                    Object.defineProperty(this, 'responseText', {
                        get: function() {
                            return JSON.stringify(response);
                        }
                    });
                } catch (e) {
                    console.error('Error modifying response:', e);
                }
            }
        });

        return originalSend.apply(this, arguments);
    };
})();