[银河奶牛]将身上材料的数量导出为json文件

将身上材料的数量导出为json文件

// ==UserScript==
// @name         [银河奶牛]将身上材料的数量导出为json文件
// @namespace    http://tampermonkey.net/
// @version      0.02
// @description  将身上材料的数量导出为json文件
// @author       Truth_Light
// @match        https://www.milkywayidle.com/*
// @match        https://test.milkywayidle.com/*
// @license      Truth_Light
// @icon         https://www.google.com/s2/favicons?sz=64&domain=milkywayidle.com
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';
    const itemList = {};
    let item_name_to_hrid, item_hrid_to_name;

    function hookWS() {
        const dataProperty = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
        const oriGet = dataProperty.get;

        dataProperty.get = hookedGet;
        Object.defineProperty(MessageEvent.prototype, "data", dataProperty);

        function hookedGet() {
            const socket = this.currentTarget;
            if (!(socket instanceof WebSocket)) {
                return oriGet.call(this);
            }
            if (socket.url.indexOf("api.milkywayidle.com/ws") <= -1 &&
                socket.url.indexOf("api-test.milkywayidle.com/ws") <= -1) {
                return oriGet.call(this);
            }

            const message = oriGet.call(this);
            Object.defineProperty(this, "data", { value: message });

            return handleMessage(message);
        }
    }

    function handleMessage(message) {
        try {
            let obj = JSON.parse(message);
            if (obj && obj.type === "init_client_data") {
                item_hrid_to_name = obj.itemDetailMap;
                for (const key in item_hrid_to_name) {
                    if (item_hrid_to_name[key] && typeof item_hrid_to_name[key] === 'object' && item_hrid_to_name[key].name) {
                        item_hrid_to_name[key] = item_hrid_to_name[key].name;
                    }
                }
                item_name_to_hrid = Object.fromEntries(
                    Object.entries(item_hrid_to_name).map(([key, value]) => [value, key])
                );
            } else if (obj && obj.type === "init_character_data") {
                let characterItems = obj.characterItems;
                characterItems.forEach(item => {
                    const itemName = item_hrid_to_name[item.itemHrid];
                    const itemCount = item.count;
                    if (itemList[itemName]) {
                        itemList[itemName] += itemCount;
                    } else {
                        itemList[itemName] = itemCount;
                    }
                });
                console.log(itemList);
            }
        } catch (error) {
            console.error("Error processing message:", error);
        }
        return message;
    }

    function downloadItemListAsFile() {
        const blob = new Blob([JSON.stringify(itemList, null, 2)], { type: "application/json" });
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = "item_list.json";
        a.click();
        URL.revokeObjectURL(url);
    }

    GM_registerMenuCommand("导出文件", downloadItemListAsFile);
    hookWS();
})();