Torn Chat Copy Fix

Fixes copy to preserve line breaks in chat messages

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==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);
    });
})();