Lolz Chat Message Copier

Копирует текст и медиа. Умное восстановление времени для групп сообщений.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         Lolz Chat Message Copier
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Копирует текст и медиа. Умное восстановление времени для групп сообщений.
// @author       GodlikeGL
// @match        *://lolz.live/*
// @match        *://zelenka.guru/*
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Стили для кликабельности времени
    GM_addStyle(`
        .chat2-message-date {
            cursor: pointer !important;
            transition: color 0.2s ease, opacity 0.2s ease;
        }
        .chat2-message-date:hover {
            color: #99ff99 !important;
        }
        /* Делаем восстановленное время чуть прозрачным, чтобы не перегружать интерфейс */
        .injected-date {
            margin-left: 6px;
            opacity: 0.3; 
        }
        .chat2-message:hover .injected-date {
            opacity: 1; /* При наведении на сообщение время становится ярким */
        }
    `);

    // --- БЛОК 1: Восстановление пропавшего времени ---
    function restoreMissingDates() {
        const messageBlocks = document.querySelectorAll('.chat2-message:not(.date-checked)');
        
        messageBlocks.forEach(block => {
            block.classList.add('date-checked'); 
            
            const textContainer = block.querySelector('.chat2-message-text');
            if (!textContainer) return;

            // Если оригинального времени внутри нет
            if (!textContainer.querySelector('.chat2-message-date')) {
                let timeText = null;
                let titleText = 'Скопировать (восстановлено)';

                // Попытка 1: Ищем в data-info (если сообщение одиночное или первое)
                const infoStr = block.getAttribute('data-info');
                if (infoStr) {
                    try {
                        const info = JSON.parse(infoStr);
                        if (info && info.time) {
                            const date = new Date(info.time);
                            const hours = String(date.getHours()).padStart(2, '0');
                            const minutes = String(date.getMinutes()).padStart(2, '0');
                            timeText = `${hours}:${minutes}`;
                        }
                    } catch (e) {}
                }

                // Попытка 2: Ищем оригинальное время у соседей по группе
                if (!timeText) {
                    const group = block.closest('.chat2-message-group');
                    if (group) {
                        // Ищем дату, которую сгенерировал сам сайт (без класса injected-date)
                        const existingDate = group.querySelector('.chat2-message-date:not(.injected-date)');
                        if (existingDate) {
                            timeText = existingDate.innerText;
                            titleText = existingDate.getAttribute('title') || titleText;
                        }
                    }
                }

                // Если время найдено любым из способов - вставляем
                if (timeText) {
                    const dateEl = document.createElement('span');
                    dateEl.className = 'chat2-message-date lztng-133npr8 chat2-message-date-muted injected-date';
                    dateEl.title = titleText;
                    dateEl.innerText = timeText;
                    textContainer.appendChild(dateEl);
                }
            }
        });
    }

    const observer = new MutationObserver(() => {
        restoreMissingDates();
    });
    observer.observe(document.body, { childList: true, subtree: true });
    restoreMissingDates();


    // --- БЛОК 2: Парсинг и вставка через буфер ---
    function parseContent(node) {
        let html = '';
        let plain = '';
        
        for (let child of node.childNodes) {
            if (child.nodeType === Node.TEXT_NODE) {
                const text = child.textContent;
                html += text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
                plain += text;
            } else if (child.nodeType === Node.ELEMENT_NODE) {
                if (child.classList.contains('mceSmilie')) {
                    const alt = child.alt || '';
                    html += alt;
                    plain += alt;
                } else if (child.classList.contains('media-embedded') || child.tagName === 'IMG') {
                    const img = child.tagName === 'IMG' ? child : child.querySelector('img');
                    if (img && img.src) {
                        html += `<img src="${img.src}">`;
                        plain += `[URL]${img.src}[/URL]`;
                    }
                } else if (child.tagName === 'BR') {
                    html += '<br>';
                    plain += '\n';
                } else if (child.tagName === 'P') {
                    const inner = parseContent(child);
                    html += '<p>' + inner.html + '</p>';
                    plain += inner.plain + '\n';
                } else {
                    const inner = parseContent(child);
                    html += inner.html;
                    plain += inner.plain;
                }
            }
        }
        return { html, plain };
    }

    document.addEventListener('click', function(e) {
        const timeElement = e.target.closest('.chat2-message-date');

        if (timeElement) {
            e.preventDefault();
            e.stopPropagation();

            const textContainer = timeElement.parentElement.querySelector('.chat2-message-text-inner');

            if (textContainer) {
                const content = parseContent(textContainer);
                const finalHtml = content.html.trim() + '&nbsp;';
                const finalPlain = content.plain.trim() + ' ';

                const inputField = document.querySelector('.tiptap.ProseMirror[contenteditable="true"]');

                if (inputField) {
                    inputField.focus();

                    const dataTransfer = new DataTransfer();
                    dataTransfer.setData('text/html', finalHtml);
                    dataTransfer.setData('text/plain', finalPlain);

                    const pasteEvent = new ClipboardEvent('paste', {
                        clipboardData: dataTransfer,
                        bubbles: true,
                        cancelable: true
                    });

                    inputField.dispatchEvent(pasteEvent);
                }
            }
        }
    });
})();