LZTRANDOMTHREAD

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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