PUBG网页兑换代码优化

使PUBG网页兑换代码错误不会出现倒计时,并添加详细信息

// ==UserScript==
// @name         PUBG网页兑换代码优化
// @namespace    http://tampermonkey.net/
// @version      2.5
// @description  使PUBG网页兑换代码错误不会出现倒计时,并添加详细信息
// @author       L
// @icon         https://wstatic-prod.pubg.com/web/live/static/favicons/apple-icon-57x57.png
// @match        https://pubg.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener('load', () => {
        const originalFetch = window.fetch;
        window.fetch = function(...args) {
            if (args[0].includes('https://api-foc.krafton.com/redeem/v3/register') && args[1]?.method === 'POST') {
                return originalFetch(...args).then(response => {
                    if (response.status !== 200) {
                        return response.json().then(data => {
                            const errorCode = data.code || '未知错误';
                            const errorMessage = data.message || '发生未知错误';

                            const reason = getErrorReason(errorCode);

                            modifyAlertContent(`兑换失败,${reason}。原始信息: ${errorCode}; ${errorMessage}`);

                            return response;
                        });
                    }
                    return response;
                });
            }
            return originalFetch(...args);
        };

        const originalXhrOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function(method, url) {
            this._url = url;
            return originalXhrOpen.apply(this, arguments);
        };

        const originalXhrSend = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function() {
            const xhr = this;
            const checkResponse = function() {
                if (xhr.readyState === 4 && xhr.status !== 200) {
                    const responseData = JSON.parse(xhr.responseText);
                    const errorCode = responseData.code || '未知错误';
                    const errorMessage = responseData.message || '发生未知错误';

                    const reason = getErrorReason(errorCode);

                    Object.defineProperty(xhr, 'status', { value: 200 });
                    Object.defineProperty(xhr, 'responseText', { value: JSON.stringify({ message: '发生错误,请查看详细信息' }) });

                    modifyAlertContent(`${reason}! 原始信息: ${errorCode}; ${errorMessage}`);
                }
            };

            this.addEventListener('readystatechange', checkResponse);
            return originalXhrSend.apply(this, arguments);
        };
    });

    function getErrorReason(errorCode) {
        switch (errorCode) {
            case 'INVALID_CODE':
                return '无效代码';
            case 'EXPIRED':
                return '代码过期';
            case 'LIMIT_OVER':
                return '代码兑换上限';
            case 'ALREADY_ACTIVATED':
                return '代码已被使用';
            case 'SERVER_NETWORK_ERROR':
                return '服务器网络错误';
            default:
                return '未知错误';
        }
    }

    function modifyAlertContent(message) {
        const alertModal = document.querySelector('.alert.modal__component.only-title');
        if (alertModal) {
            const titleElement = alertModal.querySelector('.alert__title span');
            if (titleElement) {
                titleElement.textContent = message;
            }
        } else {
            const observer = new MutationObserver(() => {
                const newAlertModal = document.querySelector('.alert.modal__component.only-title');
                if (newAlertModal) {
                    modifyAlertContent(message);
                    observer.disconnect();
                }
            });

            observer.observe(document.body, { childList: true, subtree: true });
        }
    }
})();