Greasy Fork

Greasy Fork is available in English.

allie test

tab

当前为 2025-06-08 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/538683/1603878/allie%20test.js

Tabs.Chat = {
    tabOrder: 900,
    tabLabel: 'Chat',
    tabDisabled: false,
    myDiv: null,
    chatDiv: null,
    inputDiv: null,
    observer: null,

    init: function(div){
        var t = Tabs.Chat;
        t.myDiv = div;
        t.createMainDiv();
        t.applyCustomStyles();
        t.hookGameChat();
    },

    createMainDiv: function(){
        var t = Tabs.Chat;
        var m = '<DIV class=divHeader align=center>'+tx('CHAT')+'</div>';
        
        m += '<div id="pbChatContent" style="height:450px; max-height:450px; overflow-y:auto;"></div>';
        m += '<div id="pbChatInput" style="margin-top:10px;"></div>';

        t.myDiv.innerHTML = m;
        
        t.chatDiv = ById('pbChatContent');
        t.inputDiv = ById('pbChatInput');

        t.cloneGameChatInput();
    },

    applyCustomStyles: function(){
        var styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.innerHTML = `
            #pbChatContent .chat-message {
                display: flex;
                flex-direction: row-reverse;
                justify-content: flex-start;
                align-items: baseline;
                margin-bottom: 5px;
                text-align: right;
            }
            #pbChatContent .chat-time {
                margin-left: 5px;
                color: #888;
                font-size: 0.8em;
            }
            #pbChatContent .chat-user {
                margin-left: 5px;
                font-weight: bold;
            }
            #pbChatContent .chat-text {
                word-wrap: break-word;
                max-width: 80%;
            }
        `;
        document.head.appendChild(styleElement);
    },

    hookGameChat: function(){
        var t = Tabs.Chat;
        var gameChatContainer = document.querySelector('#mod_comm_list1');
        if (gameChatContainer) {
            t.observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                        mutation.addedNodes.forEach(function(node) {
                            if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('chat-message')) {
                                var clonedNode = node.cloneNode(true);
                                t.formatChatMessage(clonedNode);
                                t.chatDiv.appendChild(clonedNode);
                                t.chatDiv.scrollTop = t.chatDiv.scrollHeight;
                            }
                        });
                    }
                });
            });

            t.observer.observe(gameChatContainer, { childList: true, subtree: true });

            // Initial population of chat messages
            gameChatContainer.querySelectorAll('.chat-message').forEach(function(message) {
                var clonedMessage = message.cloneNode(true);
                t.formatChatMessage(clonedMessage);
                t.chatDiv.appendChild(clonedMessage);
            });
            t.chatDiv.scrollTop = t.chatDiv.scrollHeight;
        } else {
            console.error('Could not find game chat container');
        }
    },

    cloneGameChatInput: function(){
        var t = Tabs.Chat;
        var gameChatInput = document.querySelector('#mod_comm_input');
        if (gameChatInput) {
            var inputClone = gameChatInput.cloneNode(true);
            t.inputDiv.appendChild(inputClone);

            var chatTextArea = t.inputDiv.querySelector('textarea');
            var sendButton = t.inputDiv.querySelector('button');
            
            if (chatTextArea && sendButton) {
                chatTextArea.addEventListener('keypress', function(e) {
                    if (e.key === 'Enter' && !e.shiftKey) {
                        e.preventDefault();
                        t.sendChat();
                    }
                });

                sendButton.addEventListener('click', function() {
                    t.sendChat();
                });
            }
        } else {
            console.error('Could not find game chat input');
        }
    },

    formatChatMessage: function(messageElement) {
        var timeElement = messageElement.querySelector('.chat-time');
        var userElement = messageElement.querySelector('.chat-user');
        var textElement = messageElement.querySelector('.chat-text');

        if (timeElement) timeElement.textContent = '[' + timeElement.textContent + ']';
        if (userElement) userElement.textContent = userElement.textContent + ':';

        // Ensure elements are in the correct order
        if (textElement) messageElement.appendChild(textElement);
        if (userElement) messageElement.appendChild(userElement);
        if (timeElement) messageElement.appendChild(timeElement);
    },

    sendChat: function(){
        var t = Tabs.Chat;
        var chatTextArea = t.inputDiv.querySelector('textarea');
        var gameChatTextArea = document.querySelector('#mod_comm_input textarea');
        var gameSendButton = document.querySelector('#mod_comm_input button');

        if (chatTextArea && gameChatTextArea && gameSendButton) {
            var message = chatTextArea.value.trim();
            if (message !== '') {
                gameChatTextArea.value = message;
                gameSendButton.click();
                chatTextArea.value = '';
            }
        } else {
            console.error('Could not find necessary elements to send chat');
        }
    }
};