make the ui minimizable



// ==UserScript==
// @name         FurryChat Chat Reload Speed Controller
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Adds a UI to control chat reload speed on furrychat.net
// @author
// @match        https://furrychat.net/chat/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to inject code into the page context
    function injectCode(fn) {
        var script = document.createElement('script');
        script.textContent = '(' + fn + ')();';
        document.body.appendChild(script);
    }

    // The actual code that will run in page context
    function pageFunction() {
        // Create UI elements
        var controllerDiv = document.createElement('div');
        controllerDiv.id = 'chat-speed-controller';
        controllerDiv.style.position = 'fixed';
        controllerDiv.style.bottom = '10px';
        controllerDiv.style.right = '10px';
        controllerDiv.style.backgroundColor = 'rgba(0,0,0,0.7)';
        controllerDiv.style.padding = '10px';
        controllerDiv.style.borderRadius = '5px';
        controllerDiv.style.color = 'white';
        controllerDiv.style.zIndex = '1000';

        var speedLabel = document.createElement('label');
        speedLabel.textContent = 'Chat Reload Speed (ms): ';
        controllerDiv.appendChild(speedLabel);

        var speedInput = document.createElement('input');
        speedInput.type = 'number';
        speedInput.min = '100';
        speedInput.value = '3000'; // Default speed
        speedInput.style.width = '80px';
        controllerDiv.appendChild(speedInput);

        var speedButton = document.createElement('button');
        speedButton.textContent = 'Set Speed';
        speedButton.style.marginLeft = '5px';
        controllerDiv.appendChild(speedButton);

        controllerDiv.appendChild(document.createElement('br'));
        controllerDiv.appendChild(document.createElement('br'));

        var pauseButton = document.createElement('button');
        pauseButton.textContent = 'Pause';
        controllerDiv.appendChild(pauseButton);

        document.body.appendChild(controllerDiv);

        // The desired speed variable
        window.desiredSpeed = parseInt(speedInput.value);

        // Function to start or reset the chat reload interval
        function startChatReloadInterval(newSpeed) {
            window.desiredSpeed = newSpeed;

            // Overwrite the speed variable
            window.speed = newSpeed;

            // Ensure the speed does not get changed by the chatReload function
            // We can override the success function
            if (!window.originalChatReload) {
                window.originalChatReload = window.chatReload;
            }

            // Redefine chatReload to prevent speed from being changed
            window.chatReload = function() {
                var cPosted = Date.now();
                var priv = $('#get_private').attr('value');
                logsControl();
                $.ajax({
                    url: "system/chat_log.php",
                    type: "post",
                    cache: false,
                    timeout: window.desiredSpeed,
                    dataType: 'json',
                    data: {
                        fload: fload,
                        caction: cAction,
                        last: lastPost,
                        snum: snum,
                        preload: privReload,
                        priv: priv,
                        lastp: lastPriv,
                        pcount: pCount,
                        room: user_room,
                        notify: globNotify,
                        token: utk
                    },
                    success: function(response) {
                        if ('check' in response) {
                            if (response.check == 99) {
                                location.reload();
                                return false;
                            } else if (response.check == 199) {
                                return false;
                            }
                        } else {
                            var mLogs = response.mlogs;
                            var mLast = response.mlast;
                            var cact = response.cact;
                            var pLogs = response.plogs;
                            var pLast = response.plast;
                            var getPcount = response.pcount;
                            // Do not reset speed here
                            // speed = response.spd;
                            inOut = response.acd;
                            if (response.act != userAction) {
                                location.reload();
                            } else if (response.ses != sesid) {
                                overWrite();
                            } else {
                                if (mLogs.indexOf("system__clear") >= 1) {
                                    $("#show_chat ul").html(mLogs);
                                    if (fload == 1) {
                                        clearPlay();
                                    }
                                    fload = 1;
                                } else {
                                    $("#show_chat ul").append(mLogs);
                                    if (fload == 1) {
                                        if (mLogs.indexOf("my_notice") >= 1) {
                                            usernamePlay();
                                        }
                                        if (mLogs.indexOf("system__join") >= 1) {
                                            joinPlay();
                                        }
                                        if (mLogs.indexOf("system__leave") >= 1) {
                                            leavePlay();
                                        }
                                        if (mLogs.indexOf("system__action") >= 1) {
                                            actionPlay();
                                        }
                                        if (mLogs.indexOf("public__message") >= 1) {
                                            messagePlay();
                                            tabNotify();
                                        }
                                    }
                                    scrollIt(fload);
                                    fload = 1;
                                }
                                cAction = cact;
                                lastPost = mLast;
                                beautyLogs();
                                if ('del' in response) {
                                    var getDel = response.del;
                                    for (var i = 0; i < getDel.length; i++) {
                                        $("#log" + getDel[i]).remove();
                                    }
                                }
                                if (response.curp == $('#get_private').attr('value')) {
                                    if (privReload == 1) {
                                        if (pLogs == '') {
                                            $('#private_content ul').html('');
                                        } else {
                                            $('#private_content ul').html(pLogs);
                                        }
                                        scrollPriv(privReload);
                                        lastPriv = pLast;
                                        privReload = 0;
                                        morePriv = 1;
                                    } else {
                                        if (pLogs == '' || lastPriv == pLast) {
                                            scrollPriv(privReload);
                                        } else {
                                            if (response.curp == priv) {
                                                $("#private_content ul").append(pLogs);
                                                privDown($(pLogs).find('.target_private').length);
                                            }
                                            scrollPriv(privReload);
                                        }
                                        if (getPcount !== pCount) {
                                            privatePlay();
                                            pCount = getPcount;
                                            tabNotify();
                                        } else {
                                            pCount = getPcount;
                                        }
                                        lastPriv = pLast;
                                    }
                                }
                                if ('top' in response) {
                                    var newTopic = response.top;
                                    if (newTopic != '' && newTopic != actualTopic) {
                                        $("#show_chat ul").append(newTopic);
                                        actualTopic = newTopic;
                                        scrollIt(fload);
                                    }
                                }
                                if (response.pico != 0) {
                                    $("#notify_private").text(response.pico);
                                    $('#notify_private').show();
                                } else {
                                    $('#notify_private').hide();
                                }
                                if ('use' in response) {
                                    var friendsCount = response.friends;
                                    var newsCount = response.news;
                                    var noticeCount = response.notify;
                                    var reportCount = response.report;
                                    var newNotify = response.nnotif;
                                    if (newsCount > 0) {
                                        $('#news_notify').text(newsCount).show();
                                        if (!$('#chat_left:visible').length) {
                                            $('#bottom_news_notify').text(newsCount).show();
                                        }
                                        if (notifyLoad > 0) {
                                            if (newsCount > curNews) {
                                                newsPlay();
                                            }
                                        }
                                    } else {
                                        $('#news_notify').hide();
                                        $('#bottom_news_notify').hide();
                                    }
                                    if (reportCount > 0) {
                                        $('#report_notify').text(reportCount).show();
                                    } else {
                                        $('#report_notify').hide();
                                    }
                                    if (friendsCount > 0) {
                                        $("#notify_friends").text(friendsCount).show();
                                    } else {
                                        $("#notify_friends").hide();
                                    }
                                    if (noticeCount > 0) {
                                        $("#notify_notify").text(noticeCount).show();
                                    } else {
                                        $("#notify_notify").hide();
                                    }
                                    if (notifyLoad > 0) {
                                        if (noticeCount > curNotify || friendsCount > curFriends || reportCount > curReport) {
                                            notifyPlay();
                                        }
                                    }
                                    curNotify = noticeCount;
                                    curFriends = friendsCount;
                                    curReport = reportCount;
                                    curNews = newsCount;
                                    globNotify = newNotify;
                                    notifyLoad = 1;
                                }
                                if ('rset' in response) {
                                    grantRoom();
                                } else {
                                    ungrantRoom();
                                }
                                if ('role' in response) {
                                    roomRank = response.role;
                                } else {
                                    roomRank = 0;
                                }
                                if ('rm' in response) {
                                    checkRm(response.rm);
                                } else {
                                    checkRm(0);
                                }
                                innactiveControl(cPosted);
                                systemLoaded = 1;
                            }
                        }
                    },
                    error: function() {
                        return false;
                    }
                });
            };

            // Restart the chat reload interval
            if (window.chatLog) {
                clearInterval(window.chatLog);
            }
            window.chatLog = setInterval(function() {
                if (window.chatReload) window.chatReload();
            }, window.desiredSpeed);
        }

        // Replace the existing chat reload interval
        if (window.chatLog) {
            clearInterval(window.chatLog);
        }

        // Get the initial speed
        var chatReloadSpeed = parseInt(speedInput.value);

        // Start the chat reload interval
        startChatReloadInterval(chatReloadSpeed);

        // Set up event listener for speed button
        speedButton.addEventListener('click', function() {
            var newSpeed = parseInt(speedInput.value);
            if (newSpeed >= 100) {
                startChatReloadInterval(newSpeed);
                if (isPaused) {
                    pauseButton.textContent = 'Pause';
                    isPaused = false;
                }
            }
        });

        // Set up event listener for pause button
        var isPaused = false;
        pauseButton.addEventListener('click', function() {
            if (isPaused) {
                var currentSpeed = parseInt(speedInput.value);
                startChatReloadInterval(currentSpeed);
                pauseButton.textContent = 'Pause';
            } else {
                if (window.chatLog) {
                    clearInterval(window.chatLog);
                }
                pauseButton.textContent = 'Resume';
            }
            isPaused = !isPaused;
        });
    }

    // Inject the code into the page
    injectCode(pageFunction);

})();