IdleScape - Chat.0

Better chat for IdleScape

اعتبارا من 29-01-2021. شاهد أحدث إصدار.

    // ==UserScript==
    // @name         IdleScape - Chat.0
    // @namespace    D4IS
    // @version      1.0
    // @description  Better chat for IdleScape
    // @author       D4M4G3X
    // @match        http*://*idlescape.com/*
    // @match        https://idlescape.com/*
    // @match        https://www.idlescape.com/*
    // @grant        none
    // @require http://code.jquery.com/jquery-3.4.1.min.js
    // @run-at document-start
    // ==/UserScript==

    (function() {
        'use strict';
        let isSetup = false;
        let chatHeight = 250;

        $('head').append('<link rel="stylesheet" href="https://digimol.net/chat.0/chat.css" type="text/css" />');
        let mainInterval = setInterval(()=> {
            if ($('.status-bar').length) {
                if(!isSetup) {
                    isSetup = true;
                    if (window.localStorage.getItem(getUsername() + '-ChatHeight')) {
                        chatHeight = parseInt(window.localStorage.getItem(getUsername() + '-ChatHeight'));
                    }
                    $('.play-area-chat-container').css('flex', '0 0 '+chatHeight+'px');
                }
                setupChannels();
            }
        }, 1000);

        let checkInterval = setInterval(()=> {
            setupUsernames();
            checkPrivates();
            checkMessageCount();
        }, 100);

        function gotoTab(tab, first = false) {
            $('.chat-tab').removeClass('selected-tab');
            $('.chat-tab-'+tab.toLowerCase()).addClass('selected-tab');
            $('.chat-interface-container').hide();
            $('.chat-interface-container.'+tab.toLowerCase()).show();
            if(first) {
                $('.chat-interface-container.'+tab.toLowerCase()).find('.chat-tabs > div:first-child').click();
            }
        }

        function setupUsernames() {
            $('.message-username:not(.user-cloned)').each(function() {
                $(this).addClass('user-cloned');
                let $user = $('<div>', {
                    'class': $(this).attr('class') + ' user-menu'
                }).html($(this).html());
                $user.insertAfter($(this));
                $user.click(function(e) {
                    openPopup($(this), e.pageX);
                });
                $(this).addClass('user-original');
                $(this).hide();
            });
        }

        function openPopup($obj, posX) {
            $('.chat-user-options').remove();
            let pos = $obj.offset();
            let $popup = $('<div>', {
                'class': 'chat-user-options'
            }).css({
                'left': posX - 45,
                'top': pos.top - 90
            }).appendTo($('body'));

            popupButton('Whisper', function() {
                $obj.parent().find('.user-original').click();
                gotoTab('private');
            }).appendTo($popup);

            popupButton('Whois', function() {
                $obj.parents('.chat-message-container').find('input').val('/whois ' + $obj.text());
                $obj.parents('.chat-message-container').find('input').focus();
            }).appendTo($popup);

            popupButton('Throw', function() {
                $obj.parents('.chat-message-container').find('input').val('/throw ' + $obj.text());
                $obj.parents('.chat-message-container').find('input').focus();
            }).appendTo($popup);

            popupButton('Close').appendTo($popup);
        }

        function popupButton(text, cb = function(){}) {
            let $btn = $('<div>', {
                'class': 'nav-tab no-select chat-user-option-' + text.toLowerCase()
            }).text(text);
            $btn.hover(function() {
                $(this).addClass('selected-tab');
            }, function() {
                $(this).removeClass('selected-tab');
            });
            $btn.click(function() {
                $(this).parent().remove();
                cb();
            });
            return $btn;
        }

        function setupChannels() {
            if(!$('.chat-tab-wrap').length) {
                let $tabWrap = $('<ul>', {
                    'class': 'chat-tab-wrap'
                }).insertAfter($('.chat-buttons'));
                $tabWrap.css({
                    'display': 'flex',
                    'margin': '0',
                    'background':'#2a343e',
                    'border': '1px solid #222'
                });
                addChatTab('Public').appendTo($tabWrap);
                addChatTab('Private').appendTo($tabWrap);

                $('.chat-buttons').remove();

                let $options = $('<div>', {
                    'class': 'chat-option-wrap'
                }).appendTo($tabWrap);

                let $option = [];
                $option[0] = $('<div>', {
                 'class': 'chat-button chat-option noselect'
                }).text('▲');
                $option[0].click(function() {
                    if (chatHeight < 550) {
                        chatHeight += 100;
                        if (window.localStorage) {
                            window.localStorage.setItem(getUsername() + '-ChatHeight', chatHeight);
                        }
                        $('.play-area-chat-container').css('flex', '0 0 '+chatHeight+'px');
                    }
                });
                $option[1] = $('<div>', {
                 'class': 'chat-button chat-option noselect'
                }).text('▼');
                $option[1].click(function() {
                    if (chatHeight > 150) {
                        chatHeight -= 100;
                        if (window.localStorage) {
                            window.localStorage.setItem(getUsername() + '-ChatHeight', chatHeight);
                        }
                        $('.play-area-chat-container').css('flex', '0 0 '+chatHeight+'px');
                    }
                });

                $option[2] = $('<div>', {
                 'class': 'chat-button chat-option noselect'
                }).text('-');
                $option[2].click(function() {
                    $.each($option, function() {
                        $(this).hide();
                    });
                    $option[3].show();
                    $('.play-area-chat-container').css('flex', '0 0 31px');
                });

                $option[3] = $('<div>', {
                    'class': 'chat-button chat-option noselect'
                }).text('+').hide();
                $option[3].click(function() {
                    $.each($option, function() {
                        $(this).show();
                    });
                    $(this).hide();
                    $('.play-area-chat-container').css('flex', '0 0 '+chatHeight+'px');
                });

                $.each($option, function() {
                   $(this).appendTo($options);
                });
            }

            if(!$('.chat-interface-container.private').length) {
                $('.chat-message-container-box').css({
                    'background': '#222323',
                });
                let $publicChat = $('.chat-interface-container');
                let $privateChat = $publicChat.clone();
                $publicChat.addClass('public');
                $privateChat.addClass('private').insertAfter($publicChat);
                $privateChat.find('.chat-tab-channel').remove();
                $privateChat.find('.chat-message-container').remove();
                $privateChat.find('.chat-functions-item').remove();
                $privateChat.hide();
            }
        }

        function checkPrivates() {
            $('.chat-interface-container.public').find('.chat-tabs > div:not(.closed)').each(function() {
                if ($(this).hasClass('chat-tab-whisper')) {
                    let $publicChat = $('.chat-interface-container.public');
                    let $privateChat = $('.chat-interface-container.private');
                    let $whisperTab = $(this);
                    let $chatBox = $publicChat.find('.chat-message-container-box > .chat-message-container:last-child');

                    $whisperTab.addClass('whisper-'+$(this).text().split(' ')[1]);
                    $whisperTab.appendTo($privateChat.find('.chat-tabs'));
                    $whisperTab.find('a').hide();

                    $chatBox.addClass('chatBox-'+$(this).text().split(' ')[1]);
                    $chatBox.appendTo($privateChat.find('.chat-message-container-box'));
                }
            });
        }

        function checkMessageCount() {
            let tabs = ['public', 'private']
            $.each(tabs, function(k, tab) {
                let count = 0;
                $('.chat-interface-container.' + tab).find('.chat-tabs > div').each(function() {
                    if (~$(this).text().indexOf('(') && ~$(this).text().indexOf(')')) {
                        count +=  parseInt($(this).text().split('(')[1].split(')')[0]);
                    }
                });
                $('.chat-tab-'+tab).find('span').text('('+count+')');
            })
        }

        function addChatTab(label) {
            let $chatTab = $('<li>', {
                'class': 'nav-tab chat-tab chat-tab-' + label.toLowerCase()
            }).html(label+' <span>(0)</span>');
            if (label === 'Public') {
                $chatTab.addClass('selected-tab');
            }
            $chatTab.click(function() {
                gotoTab(label, true);
            });
            return $chatTab;
        }

        function getUsername() {
            return $('.navbar1-box').text().split(' ')[1];
        }

    })();