Greasy Fork

Greasy Fork is available in English.

EPIC游戏库存导出

自动点击“显示更多”按钮,并捕获 AJAX 响应数据,支持手动启动导出操作。

// ==UserScript==
// @name         EPIC游戏库存导出
// @namespace    http://tampermonkey.net/
// @version      1.2
// @license PaperTiger
// @description  自动点击“显示更多”按钮,并捕获 AJAX 响应数据,支持手动启动导出操作。
// @author       Paper Tiger
// @match         *://*.epicgames.com/account/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js
// @grant        none
// ==/UserScript==

/* global XLSX */

(function() {
    'use strict';

    const responses = [];

    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        return originalFetch(...args).then(response => {
            if (response.url.includes('ajaxGetOrderHistory')) {
                response.clone().json().then(data => {
                    responses.push(data);
                    console.log('获得响应:', data);
                });
            }
            return response;
        });
    };

    const originalXhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, ...rest) {
        this.addEventListener('load', function() {
            if (url.includes('ajaxGetOrderHistory')) {
                const data = JSON.parse(this.responseText);
                responses.push(data);
                console.log('获得响应:', data);
            }
        });
        originalXhrOpen.call(this, method, url, ...rest);
    };

    function createExportButton() {
        const button = document.createElement('button');
        button.textContent = '开始导出';
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.right = '400px';
        button.style.zIndex = '99999';
        button.style.padding = '10px';
        button.style.backgroundColor = 'rgba(40, 167, 69, 1)';
        button.style.color = 'white';
        button.style.border = '2px solid red';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        document.body.appendChild(button);

        button.addEventListener('click', function() {
            clickTransactions(); // 点击交易按钮
        });
    }

    function clickTransactions() {
        const transactionsButton = document.querySelector('#nav-link-transactions');
        if (transactionsButton) {
            transactionsButton.click();
            console.log('点击了交易按钮');
            // 等待页面加载后开始点击更多
            setTimeout(clickMore, 1000);
        } else {
            console.log("找不到交易按钮。");
        }
    }

    function clickMore() {
        const moreButton = document.querySelector('#payment-history-show-more-button');
        if (moreButton) {
            moreButton.click();
            console.log('点击了显示更多按钮');
            setTimeout(clickMore, 1000);
        } else {
            console.log("没有更多内容可以加载。");
            exportResponses();
        }
    }

    function exportResponses() {
        const exportData = responses.flatMap(response =>
            response.orders.flatMap(order =>
                order.items.map(item => ({
                    '游戏名称': item.description,
                    '付款金额': order.presentmentTotal
                }))
            )
        );

        const workbook = XLSX.utils.book_new();
        const worksheet = XLSX.utils.json_to_sheet(exportData);
        XLSX.utils.book_append_sheet(workbook, worksheet, '订单历史');
        XLSX.writeFile(workbook, '游戏订单历史.xlsx');
    }

    createExportButton();
})();