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.1
// @description  Открываем случайную тему в разделе
// @author       llimonix
// @match        https://zelenka.guru/*
// @icon         https://cdn-icons-png.flaticon.com/512/7601/7601730.png
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    function check(){
        let currentPage = window.location.href;

        if (currentPage.startsWith('https://zelenka.guru/forums/')) {

            let RandomThread = $('.RandomThread');
            if (RandomThread.length == 0) {
                $('.linkGroup').prepend(`<a class="button RandomThread">Случайная тема</a>`);
                $('.RandomThread').click(function() {
                    let lastPage = $('.PageNav a:last').text();
                    let lastPageNumber = parseInt(lastPage);
                    let randomNum = Math.floor(Math.random() * lastPageNumber) + 1;
                    if (randomNum > 500) {
                        randomNum = Math.floor(Math.random() * 500) + 1;
                    }
                    let forumID = $('form.DiscussionListOptions').attr('action');
                    if (lastPage.length > 0) {
                        XenForo.ajax(`https://zelenka.guru/${forumID}page-${randomNum}`, {}).then(data => {
                            infoPage = data.templateHtml;
                            let parser = new DOMParser();

                            infoPage = parser.parseFromString(infoPage, 'text/html');

                            let threadIds = $(infoPage).find("[id^='thread-']").toArray();
                            let threads = [];

                            threadIds.forEach(function(threadId) {
                                let thread = threadId.id.match(/\d+/)[0];
                                threads.push(thread);
                            });

                            let randomThreadId = threads[Math.floor(Math.random() * threads.length)];

                            window.location.href = "https://zelenka.guru/threads/" + randomThreadId;
                        });
                    } else {
                        XenForo.ajax(`https://zelenka.guru/${forumID}`, {}).then(data => {
                            infoPage = data.templateHtml;
                            let parser = new DOMParser();

                            infoPage = parser.parseFromString(infoPage, 'text/html');

                            let threadIds = $(infoPage).find("[id^='thread-']").toArray();
                            let threads = [];

                            threadIds.forEach(function(threadId) {
                                let thread = threadId.id.match(/\d+/)[0];
                                threads.push(thread);
                            });

                            let randomThreadId = threads[Math.floor(Math.random() * threads.length)];

                            window.location.href = "https://zelenka.guru/threads/" + randomThreadId;
                        });
                    };
                });
            };
        };
        requestAnimationFrame(check);
    };
    requestAnimationFrame(check);
})();