[MWI] Guild Data Exporter

Export guild data to CSV

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         [MWI] Guild Data Exporter
// @namespace    http://tampermonkey.net/
// @version      1.09
// @description  Export guild data to CSV
// @author       WataFX
// @license MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=milkywayidle.com
// @match        https://www.milkywayidle.com/game?characterId=*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const csvHeader = "type,guild_name,guild_exp,guild_level,export_date,game_mode,player_name,member_exp";
    let guildDataRow = "";
    let guildCharactersRows = [];

    const now = new Date();
    const day = String(now.getDate()).padStart(2, '0');
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const year = String(now.getFullYear()).slice(-2);
    const currentDate = `${day}.${month}.${year}`;

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

      dataProperty.get = function hookedGet() {
        const socket = this.currentTarget;

        if (!(socket instanceof WebSocket)) {
          return originalGetter.call(this);
        }

        if (socket.url.indexOf("api.milkywayidle.com/ws") === -1 &&
            socket.url.indexOf("api-test.milkywayidle.com/ws") === -1) {
          return originalGetter.call(this);
        }

        const message = originalGetter.call(this);

        Object.defineProperty(this, "data", { value: message });

        return handleMessage(message);
      };

      Object.defineProperty(MessageEvent.prototype, "data", dataProperty);
    }

    function handleMessage(message) {
      try {
        const obj = JSON.parse(message);

        if (obj && obj.type === "guild_updated") {
          const guildExperience = Math.floor(obj.guild.experience);
          guildDataRow = [
            "guild",
            obj.guild.name,
            guildExperience,
            obj.guild.level,
            currentDate,
            "", "", ""
          ].map(cell => `"${String(cell).replace(/"/g, '""')}"`).join(',');

        } else if (obj && obj.type === "guild_characters_updated") {
          guildCharactersRows = [];

          for (const key in obj.guildSharableCharacterMap) {
            if (Object.prototype.hasOwnProperty.call(obj.guildSharableCharacterMap, key)) {
              const character = obj.guildSharableCharacterMap[key];
              const memberExperience = Math.floor(obj.guildCharacterMap[key].guildExperience);
              const row = [
                "member",
                "", // guild_name
                "", // guild_experience
                "", // level
                "", // export_date
                character.gameMode,
                character.name,
                memberExperience
              ].map(cell => `"${String(cell).replace(/"/g, '""')}"`).join(',');
              guildCharactersRows.push(row);
            }
          }
          addExportButton();
        }
      } catch (error) {
        console.error("Error processing message:", error);
      }
      return message;
    }

    function downloadCSV() {
      const csvContent = [csvHeader].concat(guildDataRow ? [guildDataRow] : [], guildCharactersRows).join('\n');
      const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });

      const filename = `guild_data_${currentDate.replace(/\./g, '_')}.csv`;
      const url = URL.createObjectURL(blob);
      const link = document.createElement('a');

      link.href = url;
      link.download = filename;
      link.click();

      URL.revokeObjectURL(url);
    }

    function addExportButton() {
      if (document.querySelector('.guild_data_export_button')) return;

      const exportButton = document.createElement('button');
      exportButton.textContent = '📥 Export data to CSV';
      exportButton.classList.add('Button_button__1Fe9z', 'guild_data_export_button');
      exportButton.addEventListener('click', downloadCSV);

      const targetElement = document.querySelector('.GuildPanel_overviewTab__v1IdE');
      if (targetElement) {
        targetElement.appendChild(exportButton);
      } else {
        console.error('Target object not found.');
      }
    }

    hookWS();

  })();