Greasy Fork

Greasy Fork is available in English.

allie test

tab

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/538683/1605109/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;
        }
    },

    switchChatType: function(chatType) {
        var t = Tabs.Chat;
        t.currentChatType = chatType;
        
        // Update tab buttons
        ById('pbGlobalChatTab').className = (chatType === 'global') ? 'tabActive' : '';
        ById('pbAllianceChatTab').className = (chatType === 'alliance') ? 'tabActive' : '';
        
        // Show/hide chat content
        t.globalChatDiv.style.display = (chatType === 'global') ? 'block' : 'none';
        t.allianceChatDiv.style.display = (chatType === 'alliance') ? 'block' : 'none';
        
        // Update user list
        t.updateUserList();
    },

    updateUserList: function() {
        var t = Tabs.Chat;
        // This would normally populate the user list from the game data
        // For now, we'll just add some placeholder content
        var html = '<div style="padding:10px; font-weight:bold;">Online Users</div>';
        html += '<div style="padding:5px 10px;">User 1</div>';
        html += '<div style="padding:5px 10px;">User 2</div>';
        html += '<div style="padding:5px 10px;">User 3</div>';
        t.userListDiv.innerHTML = html;
    },

    sendChat: function() {
        var t = Tabs.Chat;
        var chatTextArea = ById('pbChatTextArea');
        var message = chatTextArea.value.trim();
        
        if (message === '') return;
        
        console.log("Attempting to send message: " + message);
        
        // Try multiple methods to ensure the message gets sent
        
        // Method 1: Direct access to game's chat function if available
        if (typeof Chat !== 'undefined' && typeof Chat.sendMessage === 'function') {
            console.log("Using Chat.sendMessage");
            var channel = (t.currentChatType === 'global') ? 'global' : 'alliance';
            Chat.sendMessage(channel, message);
            chatTextArea.value = '';
            return;
        }
        
        // Method 2: Find and use the game's chat form
        var chatForm = document.querySelector('#mod_comm_form');
        var gameInput = document.querySelector('#mod_comm_input textarea');
        
        if (chatForm && gameInput) {
            console.log("Using game's chat form");
            
            // First select the correct tab in the game
            var gameTabSelector = document.querySelector('#mod_comm_tabs');
            if (gameTabSelector) {
                var tabIndex = (t.currentChatType === 'global') ? 0 : 1;
                var tabToClick = gameTabSelector.querySelectorAll('li')[tabIndex];
                if (tabToClick) {
                    console.log("Clicking tab: " + tabIndex);
                    tabToClick.click();
                }
            }
            
            // Set the message in the game's input
            gameInput.value = message;
            
            // Trigger input event to make sure the game recognizes the text
            gameInput.dispatchEvent(new Event('input', { bubbles: true }));
            
            // Submit the form directly
            chatForm.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
            
            // Clear our input
            chatTextArea.value = '';
            
            // Update the chat display after a short delay
            setTimeout(function() {
                t.updateChat(t.currentChatType);
            }, 500);
            
            return;
        }
        
        // Method 3: Find and click the send button
        var gameSendButton = document.querySelector('#mod_comm_input button');
        
        if (gameInput && gameSendButton) {
            console.log("Using game's send button");
            
            // First select the correct tab in the game
            var gameTabSelector = document.querySelector('#mod_comm_tabs');
            if (gameTabSelector) {
                var tabIndex = (t.currentChatType === 'global') ? 0 : 1;
                var tabToClick = gameTabSelector.querySelectorAll('li')[tabIndex];
                if (tabToClick) {
                    console.log("Clicking tab: " + tabIndex);
                    tabToClick.click();
                }
            }
            
            // Wait a moment for the tab switch
            setTimeout(function() {
                // Set the message in the game's input
                gameInput.value = message;
                
                // Trigger input event
                gameInput.dispatchEvent(new Event('input', { bubbles: true }));
                
                // Click the send button
                console.log("Clicking send button");
                gameSendButton.click();
                
                // Clear our input
                chatTextArea.value = '';
                
                // Update the chat display after a short delay
                setTimeout(function() {
                    t.updateChat(t.currentChatType);
                }, 500);
            }, 200);
            
            return;
        }
        
        // Method 4: Last resort - try to add the message directly to the chat display
        console.log("Using fallback method - direct message insertion");
        
        // Format the message similar to how the game would
        var now = new Date();
        var timeStr = now.getHours().toString().padStart(2, '0') + ':' + 
                     now.getMinutes().toString().padStart(2, '0') + ':' + 
                     now.getSeconds().toString().padStart(2, '0');
        
        var playerName = unsafeWindow.Seed.player.name || 'You';
        var formattedMsg = '<div class="msg"><span class="time">[' + timeStr + ']</span> <span class="from">' + playerName + ':</span> <span class="text">' + message + '</span></div>';
        
        // Add to our chat display
        var targetDiv = (t.currentChatType === 'global') ? t.globalChatDiv : t.allianceChatDiv;
        targetDiv.innerHTML += formattedMsg;
        targetDiv.scrollTop = targetDiv.scrollHeight;
        
        // Clear our input
        chatTextArea.value = '';
        
        // Try to add to game's chat display too
        var gameChatContainer = (t.currentChatType === 'global') ? 
            document.querySelector('#mod_comm_list1') : 
            document.querySelector('#mod_comm_list2');
        
        if (gameChatContainer) {
            gameChatContainer.innerHTML += formattedMsg;
            gameChatContainer.scrollTop = gameChatContainer.scrollHeight;
        }
    }
};