LZTThreadTypingUniq

Уник в статусе печатания в темах

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         LZTThreadTypingUniq
// @namespace    MeloniuM/LZT
// @version      1.2
// @description  Уник в статусе печатания в темах
// @author       MeloniuM
// @license      MIT
// @match        https://lolz.live/threads/*
// @match        https://zelenka.guru/threads/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zelenka.guru
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    //строка для вырезания ников из фраз
    let str = (XenForo.phrases.users_are_typing + ' ' + XenForo.phrases.count_more + ' ' + XenForo.phrases.user_is_typing).replaceAll(/{{(user[12]?|count)}}/g, ' ').replace(/\s+/g, ' ').trim();
    //удаляем дубликаты
    str = [...new Set(str.split(" "))];
    //массив уников
    let prioritizedACUsers;
    function getPrioritizedACUsers(){
        if (prioritizedACUsers) return prioritizedACUsers;

        prioritizedACUsers = $('textarea.LolzteamEditorSimple').data('options').prioritizedACUsers.reduce((acc, cur) => {
            if (cur.username == "Mellorium") return acc;
            acc[cur.username] = {
                avatar: cur.avatar,
                username: cur.usernameHtml,
                user: cur.username
            };
            return acc;
    }, {});
        return prioritizedACUsers;
    }

    let findProcess = [];

    const config = {
        subtree: true,
        childList: true
    };

    const callback = function (mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type !== "childList") return;

            $.each(mutation.addedNodes, function(index, node) {
                if ($(node).is('span.username') || $(node.parentElement).is('span.username')) return;

                //текст тайпинга
                let typing = node.textContent;
                //получаем ники
                let users = typing.trim().split(' ').filter(x => !str.includes(x));
                if (!users.length) return;
                let pUsers = getPrioritizedACUsers();
                $.each(users, function(index, user) {
                    if (pUsers[user]){
                        typing = typing.replace(user, pUsers[user].username);
                        return;
                    }
                    //чтобы дважды не запрашивало
                    if (findProcess.includes(user)) return;
                    findProcess.push(user);
                    XenForo.ajax('/members/find', {q: user}, resp => {
                        if (!XenForo.hasResponseError(resp)) {
                            let result = resp.results[user];
                            console.log(prioritizedACUsers)
                            prioritizedACUsers[user] = result;
                            delete findProcess[findProcess.indexOf(user)]
                            $.each($('#LiveTypingUsers .Users')[0].childNodes, function(index, node) {
                                if (node.nodeType !== Node.TEXT_NODE) return;
                                if (!node.textContent.includes(user)) return;
                                if ($(node).closest('.username').length) return;
                                $(node).replaceWith(node.textContent.replace(user, result.username));
                            });
                        }
                    })
                });
                if (node.textContent === typing) return;

                $(node).replaceWith(typing);
            })
        }
    };
    const observer = new MutationObserver(callback);
    observer.observe($('#LiveTypingUsers')[0], config);

})();