Greasy Fork

Greasy Fork is available in English.

chatv4

chatv4test

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

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

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

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

    createMainDiv: function () {
        var t = Tabs.Chat;
        var m = '<DIV class=divHeader align=center>' + tx('CHAT') + '</div>';

        // Create tab buttons similar to the game's interface
        m += '<div style="background-color:#e8d6b0; padding:5px; border-bottom:1px solid #886;">';
        m += '<button id="pbGlobalChatTab" class="tab selected" style="margin-right:5px;">Global Chat</button>';
        m += '<button id="pbAllianceChatTab" class="tab">Alliance Chat</button>';
        m += '</div>';

        // Chat content area
        m += '<div id="pbChatContent" style="height:450px; overflow-y:auto; background-color:#f9f2dd; padding:5px;"></div>';
        
        // Chat input area
        m += '<div id="pbChatInput" style="padding:5px; background-color:#e8d6b0;"></div>';

        t.myDiv.innerHTML = m;

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

        t.cloneGameChatInput();
    },

    setupChatTabs: function() {
        var t = Tabs.Chat;
        
        ById('pbGlobalChatTab').addEventListener('click', function() {
            t.switchChatType('global');
        });
        
        ById('pbAllianceChatTab').addEventListener('click', function() {
            t.switchChatType('alliance');
        });
        
        // Initialize with global chat
        t.switchChatType('global');
    },

    switchChatType: function(chatType) {
        var t = Tabs.Chat;
        t.currentChatType = chatType;
        
        // Update tab styling
        if (chatType === 'global') {
            ById('pbGlobalChatTab').className = 'tab selected';
            ById('pbAllianceChatTab').className = 'tab';
        } else {
            ById('pbGlobalChatTab').className = 'tab';
            ById('pbAllianceChatTab').className = 'tab selected';
        }
        
        // Switch to the appropriate chat tab in the game
        var gameChatTabs = document.querySelector('#mod_comm_tabs');
        if (gameChatTabs) {
            if (chatType === 'global') {
                gameChatTabs.selectedIndex = 0;
            } else {
                gameChatTabs.selectedIndex = 1;
            }
            // Trigger any events that might be needed
            var event = new Event('change');
            gameChatTabs.dispatchEvent(event);
        }
        
        // Update the chat content
        t.updateChat();
    },

    hookGameChat: function () {
        var t = Tabs.Chat;
        var globalChatContainer = document.querySelector('#mod_comm_list1');
        var allianceChatContainer = document.querySelector('#mod_comm_list2');

        if (globalChatContainer && allianceChatContainer) {
            // Set up observers for both chat types
            var observer = new MutationObserver(function(mutations) {
                t.updateChat();
            });

            observer.observe(globalChatContainer, { childList: true, subtree: true });
            observer.observe(allianceChatContainer, { childList: true, subtree: true });

            // Initial update
            t.updateChat();
        } else {
            console.error('Could not find game chat containers');
        }
    },

    updateChat: function () {
        var t = Tabs.Chat;
        var chatContainer = (t.currentChatType === 'global') ? 
            document.querySelector('#mod_comm_list1') : 
            document.querySelector('#mod_comm_list2');

        if (chatContainer && t.chatDiv) {
            // Copy the chat content
            t.chatDiv.innerHTML = chatContainer.innerHTML;
            
            // Add chat rules at the bottom if they exist
            var chatRules = document.querySelector('.chat_rules');
            if (chatRules) {
                var rulesClone = chatRules.cloneNode(true);
                t.chatDiv.appendChild(rulesClone);
            }
            
            // Scroll to bottom
            t.chatDiv.scrollTop = t.chatDiv.scrollHeight;
        }
    },

    cloneGameChatInput: function () {
        var t = Tabs.Chat;
        var gameChatInput = document.querySelector('#mod_comm_input');
        
        if (gameChatInput) {
            var inputClone = gameChatInput.cloneNode(true);
            
            // Style the input area to match the game
            inputClone.style.display = 'flex';
            inputClone.style.alignItems = 'center';
            
            var textarea = inputClone.querySelector('textarea');
            var sendButton = inputClone.querySelector('button');
            
            if (textarea && sendButton) {
                textarea.style.flexGrow = '1';
                textarea.style.marginRight = '5px';
                textarea.style.height = '40px';
                
                sendButton.style.height = '40px';
                
                // Clear any existing event listeners
                var newTextarea = textarea.cloneNode(true);
                var newSendButton = sendButton.cloneNode(true);
                
                textarea.parentNode.replaceChild(newTextarea, textarea);
                sendButton.parentNode.replaceChild(newSendButton, sendButton);
                
                // Add our event listeners
                newTextarea.addEventListener('keypress', function(e) {
                    if (e.key === 'Enter' && !e.shiftKey) {
                        e.preventDefault();
                        t.sendChat();
                    }
                });
                
                newSendButton.addEventListener('click', function() {
                    t.sendChat();
                });
            }
            
            t.inputDiv.innerHTML = '';
            t.inputDiv.appendChild(inputClone);
        } else {
            console.error('Could not find game chat input');
        }
    },

    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');
        var gameChatTabs = document.querySelector('#mod_comm_tabs');

        if (chatTextArea && gameChatTextArea && gameSendButton && gameChatTabs) {
            var message = chatTextArea.value.trim();
            if (message !== '') {
                // Make sure the correct chat tab is selected in the game
                if (t.currentChatType === 'global') {
                    gameChatTabs.selectedIndex = 0;
                } else {
                    gameChatTabs.selectedIndex = 1;
                }
                
                // Trigger any events that might be needed
                var event = new Event('change');
                gameChatTabs.dispatchEvent(event);
                
                // Send the message
                gameChatTextArea.value = message;
                gameSendButton.click();
                chatTextArea.value = '';
            }
        } else {
            console.error('Could not find necessary elements to send chat');
        }
    }
};