Torn Chat Copy Fix

Fixes copy to preserve line breaks in chat messages

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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         Torn Chat Copy Fix
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Fixes copy to preserve line breaks in chat messages
// @author       You
// @match        https://www.torn.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('copy', (e) => {
        const selection = window.getSelection();
        if (!selection.rangeCount) return;

        // Check if selection is within a chat message
        const range = selection.getRangeAt(0);
        const container = range.commonAncestorContainer;
        const chatBox = container.nodeType === 1
            ? container.closest('.box___MhvsJ')
            : container.parentElement?.closest('.box___MhvsJ');

        if (!chatBox) return;

        // Create a clone of the selected content
        const clone = range.cloneContents();
        const tempDiv = document.createElement('div');
        tempDiv.appendChild(clone);

        // Replace <br> with newlines
        tempDiv.querySelectorAll('br').forEach(br => {
            br.replaceWith('\n');
        });

        const textContent = tempDiv.textContent;

        // Override the clipboard data
        e.preventDefault();
        e.clipboardData.setData('text/plain', textContent);
    });
})();