LZTRANDOMTHREAD

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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();
    })
})();