LZTRANDOMTHREAD

Открываем случайную тему в разделе

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         LZTRANDOMTHREAD
// @namespace    https://greasyfork.org/ru/users/1142494-llimonix
// @version      0.2
// @description  Открываем случайную тему в разделе
// @author       llimonix
// @match        https://lolz.live/*
// @icon         https://cdn-icons-png.flaticon.com/512/7601/7601730.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function getRandomThreadIds(html) {
        const parser = new DOMParser();
        const doc = parser.parseFromString(html, 'text/html');
        const threadElements = doc.querySelectorAll("[id^='thread-']");

        return Array.from(threadElements).map(el => el.id.match(/\d+/)[0]);
    }

    async function openRandomThread() {
        const forumID = $('form.DiscussionListOptions').attr('action');
        if (!forumID) return;

        const lastPageText = $('.PageNav a:last').text();
        const lastPageNumber = parseInt(lastPageText) || 1;

        const randomPage = Math.floor(Math.random() * lastPageNumber) + 1;

        const url = lastPageNumber > 1 ? `${forumID}page-${randomPage}` : forumID;

        try {
            const data = await XenForo.ajax(url, {});
            const threadIds = getRandomThreadIds(data.templateHtml);

            if (threadIds.length > 0) {
                const randomThreadId = threadIds[Math.floor(Math.random() * threadIds.length)];
                window.location.href = `/threads/${randomThreadId}`;
            }
        } catch (error) {
            console.error('Ошибка загрузки темы:', error);
        }
    }

    function addRandomThreadButton() {
        const buttonAdded = $('.pageNavLinkGroup a.RandomThread')
        if (buttonAdded.lenght === 0) return;

        const targetContainer = $('.pageNavLinkGroup .btn-group');
        if (targetContainer.length === 0) return;

        const button = $('<a class="button RandomThread">Случайная тема</a>');
        button.on('click', openRandomThread);
        targetContainer.prepend(button);
    }

    XenForo.register('.pageNavLinkGroup .btn-group', function() {
        addRandomThreadButton();
    })
})();