Greasy Fork

Greasy Fork is available in English.

allie test

tab

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

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

var Tabs = Tabs || {};

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

    init: function(div) {
        var t = this;
        t.myDiv = div;
        t.createMainDiv();
        t.hookGameChat();
        t.addStyles();
    },

    createMainDiv: function() {
        var t = this;
        var html = '<div class="divHeader" align="center">CHAT</div>';

        html += '<div id="pbChatTabs" class="chat-tabs">';
        html += '<button id="pbGlobalChatTab" class="chat-tab active">Global</button>';
        html += '<button id="pbAllianceChatTab" class="chat-tab">Alliance</button>';
        html += '</div>';
        html += '<div id="pbChatContent" class="chat-content"></div>';
        html += '<div id="pbChatInput" class="chat-input"></div>';

        t.myDiv.innerHTML = html;

        t.chatContent = document.getElementById('pbChatContent');
        t.inputDiv = document.getElementById('pbChatInput');

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

        t.createChatInput();
    },

    createChatInput: function() {
        var t = this;
        var inputHtml = '<textarea id="pbChatTextArea" rows="3"></textarea>';
        inputHtml += '<button id="pbChatSendButton">Send</button>';

        t.inputDiv.innerHTML = inputHtml;

        var chatTextArea = document.getElementById('pbChatTextArea');
        var sendButton = document.getElementById('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 = this;
        var gameChatContainers = {
            global: document.querySelector('#mod_comm_list1'),
            alliance: document.querySelector('#mod_comm_list2')
        };

        if (gameChatContainers.global && gameChatContainers.alliance) {
            Object.keys(gameChatContainers).forEach(function(chatType) {
                new MutationObserver(function() { t.updateChat(chatType); })
                    .observe(gameChatContainers[chatType], { childList: true, subtree: true });
            });

            t.updateChat('global');
            t.updateChat('alliance');
        } else {
            console.error('Could not find game chat containers');
        }
    },

    updateChat: function(chatType) {
        var t = this;
        var gameChatContainer = document.querySelector(chatType === 'global' ? '#mod_comm_list1' : '#mod_comm_list2');

        if (gameChatContainer && t.chatContent) {
            var messages = Array.from(gameChatContainer.querySelectorAll('.comm-div'))
                .map(t.formatChatMessage)
                .join('');

            t.chatContent.innerHTML = messages;
            t.chatContent.scrollTop = t.chatContent.scrollHeight;
        }
    },

    formatChatMessage: function(messageDiv) {
        var avatar = messageDiv.querySelector('img');
        var username = messageDiv.querySelector('.comm-span1');
        var message = messageDiv.querySelector('.comm-span2');

        return '<div class="pb-chat-message">' +
            (avatar ? '<img src="' + avatar.src + '" class="chat-avatar" alt="User Avatar">' : '') +
            '<div class="chat-content">' +
            (username ? '<div class="chat-username">' + username.textContent + '</div>' : '') +
            (message ? '<div class="chat-text">' + message.textContent + '</div>' : '') +
            '</div></div>';
    },

    sendChat: function() {
        var t = this;
        var chatTextArea = document.getElementById('pbChatTextArea');
        var gameChatTextArea = document.querySelector('#mod_comm_input textarea');
        var gameSendButton = document.querySelector('#mod_comm_input button');
        var gameChatTypeSelector = document.querySelector('#mod_comm_tabs');

        if (chatTextArea && gameChatTextArea && gameSendButton && gameChatTypeSelector) {
            var message = chatTextArea.value.trim();
            if (message !== '') {
                gameChatTypeSelector.selectedIndex = t.currentChatType === 'global' ? 0 : 1;
                gameChatTextArea.value = message;
                gameSendButton.click();
                chatTextArea.value = '';
            }
        } else {
            console.error('Could not find necessary elements to send chat');
        }
    },

    switchChatType: function(chatType) {
        var t = this;
        t.currentChatType = chatType;
        t.updateChat(chatType);

        document.getElementById('pbGlobalChatTab').classList.toggle('active', chatType === 'global');
        document.getElementById('pbAllianceChatTab').classList.toggle('active', chatType === 'alliance');
    },

    addStyles: function() {
        var css = `
            .chat-tabs {
                display: flex;
                margin-bottom: 10px;
            }
            .chat-tab {
                flex: 1;
                padding: 10px;
                background: #f0f0f0;
                border: none;
                cursor: pointer;
            }
            .chat-tab.active {
                background: #007bff;
                color: white;
            }
            .chat-content {
                height: 400px;
                max-height: 400px;
                overflow-y: auto;
                border: 1px solid #ccc;
                padding: 10px;
            }
            .chat-input {
                margin-top: 10px;
            }
            .chat-input textarea {
                width: 100%;
                resize: vertical;
            }
            .chat-input button {
                width: 100%;
                margin-top: 5px;
                padding: 5px;
            }
            .pb-chat-message {
                display: flex;
                align-items: flex-start;
                margin-bottom: 10px;
                border-bottom: 1px solid #eee;
                padding-bottom: 5px;
            }
            .pb-chat-message:last-child {
                border-bottom: none;
            }
            .chat-avatar {
                width: 40px;
                height: 40px;
                margin-right: 10px;
                border-radius: 50%;
            }
            .chat-content {
                flex-grow: 1;
            }
            .chat-username {
                font-weight: bold;
                margin-bottom: 3px;
            }
            .chat-text {
                word-break: break-word;
            }
        `;

        var style = document.createElement('style');
        style.type = 'text/css';
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }
        document.head.appendChild(style);
    }
};

// Initialize the Chat tab
(function() {
    var chatDiv = document.createElement('div');
    chatDiv.id = 'pbChatTab';
    document.body.appendChild(chatDiv);
    Tabs.Chat.init(chatDiv);
})();var Tabs = Tabs || {};

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

    init: function(div) {
        var t = this;
        t.myDiv = div;
        t.createMainDiv();
        t.hookGameChat();
        t.addStyles();
    },

    createMainDiv: function() {
        var t = this;
        var html = '<div class="divHeader" align="center">CHAT</div>';

        html += '<div id="pbChatTabs" class="chat-tabs">';
        html += '<button id="pbGlobalChatTab" class="chat-tab active">Global</button>';
        html += '<button id="pbAllianceChatTab" class="chat-tab">Alliance</button>';
        html += '</div>';
        html += '<div id="pbChatContent" class="chat-content"></div>';
        html += '<div id="pbChatInput" class="chat-input"></div>';

        t.myDiv.innerHTML = html;

        t.chatContent = document.getElementById('pbChatContent');
        t.inputDiv = document.getElementById('pbChatInput');

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

        t.createChatInput();
    },

    createChatInput: function() {
        var t = this;
        var inputHtml = '<textarea id="pbChatTextArea" rows="3"></textarea>';
        inputHtml += '<button id="pbChatSendButton">Send</button>';

        t.inputDiv.innerHTML = inputHtml;

        var chatTextArea = document.getElementById('pbChatTextArea');
        var sendButton = document.getElementById('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 = this;
        var gameChatContainers = {
            global: document.querySelector('#mod_comm_list1'),
            alliance: document.querySelector('#mod_comm_list2')
        };

        if (gameChatContainers.global && gameChatContainers.alliance) {
            Object.keys(gameChatContainers).forEach(function(chatType) {
                new MutationObserver(function() { t.updateChat(chatType); })
                    .observe(gameChatContainers[chatType], { childList: true, subtree: true });
            });

            t.updateChat('global');
            t.updateChat('alliance');
        } else {
            console.error('Could not find game chat containers');
        }
    },

    updateChat: function(chatType) {
        var t = this;
        var gameChatContainer = document.querySelector(chatType === 'global' ? '#mod_comm_list1' : '#mod_comm_list2');

        if (gameChatContainer && t.chatContent) {
            var messages = Array.from(gameChatContainer.querySelectorAll('.comm-div'))
                .map(t.formatChatMessage)
                .join('');

            t.chatContent.innerHTML = messages;
            t.chatContent.scrollTop = t.chatContent.scrollHeight;
        }
    },

    formatChatMessage: function(messageDiv) {
        var avatar = messageDiv.querySelector('img');
        var username = messageDiv.querySelector('.comm-span1');
        var message = messageDiv.querySelector('.comm-span2');

        return '<div class="pb-chat-message">' +
            (avatar ? '<img src="' + avatar.src + '" class="chat-avatar" alt="User Avatar">' : '') +
            '<div class="chat-content">' +
            (username ? '<div class="chat-username">' + username.textContent + '</div>' : '') +
            (message ? '<div class="chat-text">' + message.textContent + '</div>' : '') +
            '</div></div>';
    },

    sendChat: function() {
        var t = this;
        var chatTextArea = document.getElementById('pbChatTextArea');
        var gameChatTextArea = document.querySelector('#mod_comm_input textarea');
        var gameSendButton = document.querySelector('#mod_comm_input button');
        var gameChatTypeSelector = document.querySelector('#mod_comm_tabs');

        if (chatTextArea && gameChatTextArea && gameSendButton && gameChatTypeSelector) {
            var message = chatTextArea.value.trim();
            if (message !== '') {
                gameChatTypeSelector.selectedIndex = t.currentChatType === 'global' ? 0 : 1;
                gameChatTextArea.value = message;
                gameSendButton.click();
                chatTextArea.value = '';
            }
        } else {
            console.error('Could not find necessary elements to send chat');
        }
    },

    switchChatType: function(chatType) {
        var t = this;
        t.currentChatType = chatType;
        t.updateChat(chatType);

        document.getElementById('pbGlobalChatTab').classList.toggle('active', chatType === 'global');
        document.getElementById('pbAllianceChatTab').classList.toggle('active', chatType === 'alliance');
    },

    addStyles: function() {
        var css = `
            .chat-tabs {
                display: flex;
                margin-bottom: 10px;
            }
            .chat-tab {
                flex: 1;
                padding: 10px;
                background: #f0f0f0;
                border: none;
                cursor: pointer;
            }
            .chat-tab.active {
                background: #007bff;
                color: white;
            }
            .chat-content {
                height: 400px;
                max-height: 400px;
                overflow-y: auto;
                border: 1px solid #ccc;
                padding: 10px;
            }
            .chat-input {
                margin-top: 10px;
            }
            .chat-input textarea {
                width: 100%;
                resize: vertical;
            }
            .chat-input button {
                width: 100%;
                margin-top: 5px;
                padding: 5px;
            }
            .pb-chat-message {
                display: flex;
                align-items: flex-start;
                margin-bottom: 10px;
                border-bottom: 1px solid #eee;
                padding-bottom: 5px;
            }
            .pb-chat-message:last-child {
                border-bottom: none;
            }
            .chat-avatar {
                width: 40px;
                height: 40px;
                margin-right: 10px;
                border-radius: 50%;
            }
            .chat-content {
                flex-grow: 1;
            }
            .chat-username {
                font-weight: bold;
                margin-bottom: 3px;
            }
            .chat-text {
                word-break: break-word;
            }
        `;

        var style = document.createElement('style');
        style.type = 'text/css';
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }
        document.head.appendChild(style);
    }
};

// Initialize the Chat tab
(function() {
    var chatDiv = document.createElement('div');
    chatDiv.id = 'pbChatTab';
    document.body.appendChild(chatDiv);
    Tabs.Chat.init(chatDiv);
})();