LZTRANDOMTHREAD

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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