Greasy Fork

Greasy Fork is available in English.

allie test

tab

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

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

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

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

    createMainDiv: function() {
        var t = Tabs.Chat;
        var m = '<DIV class=divHeader align=center>' + tx('CHAT') + '</div>';
        
        m += '<div id="pbChatTabs" style="margin-bottom:10px;">';
        m += '<button id="pbGlobalChatTab" class="tabActive">Global Chat</button>';
        m += '<button id="pbAllianceChatTab">Alliance Chat</button>';
        m += '</div>';
        
        m += '<div id="pbChatRoom" style="display:flex; height:500px; border:1px solid #888;">';
        m += '<div id="pbUserList" style="width:150px; border-right:1px solid #888; overflow-y:auto;"></div>';
        m += '<div style="flex-grow:1; display:flex; flex-direction:column;">';
        m += '<div id="pbGlobalChatContent" style="flex-grow:1; overflow-y:auto; padding:10px;"></div>';
        m += '<div id="pbAllianceChatContent" style="flex-grow:1; overflow-y:auto; padding:10px; display:none;"></div>';
        m += '<div id="pbChatInput" style="border-top:1px solid #888; padding:10px;">';
        
        // Custom chat input area
        m += '<textarea id="pbChatTextArea" style="width:100%; height:60px; margin-bottom:5px; padding:5px; resize:none;"></textarea>';
        m += '<button id="pbChatSendButton" style="width:100%; padding:5px; cursor:pointer;">Send</button>';
        
        m += '</div>';
        m += '</div>';
        m += '</div>';

        t.myDiv.innerHTML = m;
        
        t.globalChatDiv = ById('pbGlobalChatContent');
        t.allianceChatDiv = ById('pbAllianceChatContent');
        t.userListDiv = ById('pbUserList');
        t.inputDiv = ById('pbChatInput');

        ById('pbGlobalChatTab').addEventListener('click', function() { t.switchChatType('global'); });
        ById('pbAllianceChatTab').addEventListener('click', function() { t.switchChatType('alliance'); });

        // Add event listeners for the custom chat input
        var chatTextArea = ById('pbChatTextArea');
        var sendButton = ById('pbChatSendButton');
        
        chatTextArea.addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                t.sendChat();
            }
        });
        
        sendButton.addEventListener('click', function() {
            t.sendChat();
        });
    },

    hookGameChat: function() {
        var t = Tabs.Chat;
        var gameChatContainer = document.querySelector('#mod_comm_list1');
        var gameAllianceChatContainer = document.querySelector('#mod_comm_list2');
        
        if (gameChatContainer && gameAllianceChatContainer) {
            var observerGlobal = new MutationObserver(function(mutations) {
                t.updateChat('global');
            });

            var observerAlliance = new MutationObserver(function(mutations) {
                t.updateChat('alliance');
            });

            observerGlobal.observe(gameChatContainer, { childList: true, subtree: true });
            observerAlliance.observe(gameAllianceChatContainer, { childList: true, subtree: true });
            
            t.updateChat('global');
            t.updateChat('alliance');
        } else {
            console.error('Could not find game chat containers');
        }
    },

    updateChat: function(chatType) {
        var t = Tabs.Chat;
        var gameChatContainer = (chatType === 'global') ? 
            document.querySelector('#mod_comm_list1') : 
            document.querySelector('#mod_comm_list2');
        var targetDiv = (chatType === 'global') ? t.globalChatDiv : t.allianceChatDiv;
        
        if (gameChatContainer && targetDiv) {
            targetDiv.innerHTML = gameChatContainer.innerHTML;
            targetDiv.scrollTop = targetDiv.scrollHeight;
        }
    },

    sendChat: function() {
        var t = Tabs.Chat;
        var chatTextArea = ById('pbChatTextArea');
        var message = chatTextArea.value.trim();
        
        if (message === '') return;
        
        console.log("Attempting to send chat: " + message);
        
        // Get the game's chat elements
        var gameTextArea = document.querySelector('#mod_comm_input textarea');
        var gameSendButton = document.querySelector('#mod_comm_input button');
        
        if (!gameTextArea || !gameSendButton) {
            console.error("Game chat elements not found");
            return;
        }
        
        // Select the correct chat tab in the game
        try {
            // First, find the tab selector
            var chatTabs = document.querySelector('#mod_comm_tabs');
            if (chatTabs) {
                // Determine which tab to click based on our current chat type
                var tabIndex = (t.currentChatType === 'global') ? 0 : 1;
                
                // Try to find and click the tab
                var allTabs = chatTabs.querySelectorAll('li');
                if (allTabs && allTabs.length > tabIndex) {
                    console.log("Clicking on tab: " + tabIndex);
                    allTabs[tabIndex].click();
                }
            }
        } catch (e) {
            console.error("Error selecting chat tab:", e);
        }
        
        // Wait a moment for the tab switch to take effect
        setTimeout(function() {
            try {
                // Set the message text
                gameTextArea.value = message;
                
                // Trigger events to make sure the game recognizes the input
                gameTextArea.dispatchEvent(new Event('focus'));
                gameTextArea.dispatchEvent(new Event('input', { bubbles: true }));
                gameTextArea.dispatchEvent(new Event('change', { bubbles: true }));
                
                // Click the send button
                console.log("Clicking send button");
                gameSendButton.click();
                
                // Clear our text area
                chatTextArea.value = '';
                
                // Update our chat display
                setTimeout(function() {
                    t.updateChat(t.currentChatType);
                }, 500);
            } catch (e) {
                console.error("Error sending chat:", e);
            }
        }, 200);
    },

    switchChatType: function(chatType) {
        var t = Tabs.Chat;
        t.currentChatType = chatType;
        
        if (chatType === 'global') {
            t.globalChatDiv.style.display = 'block';
            t.allianceChatDiv.style.display = 'none';
            ById('pbGlobalChatTab').className = 'tabActive';
            ById('pbAllianceChatTab').className = '';
        } else {
            t.globalChatDiv.style.display = 'none';
            t.allianceChatDiv.style.display = 'block';
            ById('pbGlobalChatTab').className = '';
            ById('pbAllianceChatTab').className = 'tabActive';
        }
    },

    updateUserList: function() {
        var t = Tabs.Chat;
        // This is a placeholder function. You'll need to implement the actual user list retrieval
        // based on how your game manages online users.
        var onlineUsers = ['User1', 'User2', 'User3']; // Replace with actual online users
        
        var userListHTML = '<div class="divHeader">Online Users</div>';
        onlineUsers.forEach(function(user) {
            userListHTML += '<div class="chatUser">' + user + '</div>';
        });
        
        t.userListDiv.innerHTML = userListHTML;

        // Update the user list periodically
        setTimeout(function() { t.updateUserList(); }, 60000); // Update every minute
    }
};