Yodayo Chat Message Downloader

download logs

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Yodayo Chat Message Downloader
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  download logs
// @match        https://yodayo.com/tavern/chat/*
// @grant        none
// @license      unlicense
// ==/UserScript==

(function() {
    'use strict';

    function downloadString(text, fileType, fileName) {
        const blob = new Blob([text], { type: fileType });

        const a = document.createElement('a');
        a.download = fileName;
        a.href = URL.createObjectURL(blob);
        a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
        a.style.display = "none";
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        setTimeout(() => { URL.revokeObjectURL(a.href); }, 1500);
    }

    function createDownloadButton(targetDiv) {
        const button = document.createElement('button');
        button.innerHTML = '⬇️';
        button.style.marginLeft = '10px';
        button.style.cursor = 'pointer';

        targetDiv.appendChild(button);

        button.addEventListener('click', () => {

            const url = window.location.href;

            const id = url.split('/').slice(-2, -1)[0];

            const apiUrl = `https://api.yodayo.com/v1/chats/${id}/messages?limit=99999`;
            fetch(apiUrl, {
                method: 'GET',
                credentials: 'include'
            })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.json();
                })
                .then(data => {
                    const format = confirm("Do you want to download the messages in JSON format? Click 'Cancel' for plain text.");

                    if (format) {
                        const jsonString = JSON.stringify(data, null, 2);
                        const fileName = `${id}_messages.json`;
                        downloadString(jsonString, 'application/json', fileName);
                    } else {
                        const messages = data.messages.map(message => `${message.message_source}: ${message.message}`).join('\n\n\n');
                        const fileName = `${id}_messages.txt`;
                        downloadString(messages, 'text/plain', fileName);
                    }
                })
                .catch(error => {
                    console.error('Error fetching messages:', error);
                });
        });
    }

    const observer = new MutationObserver((mutations) => {
        const targetDiv = document.getElementsByClassName('flex portrait:hidden')[0];
        if (targetDiv) {
            createDownloadButton(targetDiv);
            observer.disconnect();
        }
    });

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