Greasy Fork

Greasy Fork is available in English.

GGn Group Expansion

Adds quick buttons for expanding all groups and filelist on GGn pages.

// ==UserScript==
// @name         GGn Group Expansion
// @version      1.3.1
// @author       SleepingGiant
// @description  Adds quick buttons for expanding all groups and filelist on GGn pages.
// @namespace    http://greasyfork.icu/users/1395131
// @match        https://gazellegames.net/torrents.php?id=*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function insertExpandAllButton() {
        const torrentHeaderTd = Array.from(document.querySelectorAll('td')).find(td =>
            td.textContent.trim() === 'Torrents' &&
            td.querySelector('strong')?.textContent.trim() === 'Torrents'
        );
        if (torrentHeaderTd) {
            const expandButton = document.createElement('button');
            expandButton.textContent = 'Expand All';
            expandButton.type = 'button';
            expandButton.style.marginLeft = '10px';
            expandButton.style.fontSize = '0.9em';
            expandButton.id = 'expand-all-button';
            expandButton.addEventListener('click', (e) => {
                e.stopPropagation(); // Prevents the onclick from firing which would hide all the content
                expandAll();
            });
            torrentHeaderTd.appendChild(expandButton);
        }
    }

    function insertInlineEditionButtons() {
        const editions = document.querySelectorAll('.edition_info');
        editions.forEach(edition => {
            const btn = document.createElement('button');
            btn.textContent = 'Expand Edition';
            btn.style.marginLeft = '10px';
            btn.type = 'button';
            btn.classList.add('expand-edition-button');
            btn.onclick = (e) => {
                e.stopPropagation();
                expandSingleEdition(edition);
            };
            edition.appendChild(btn);
        });
    }

    function expandAll() {
        document.querySelectorAll('.edition_info').forEach(edition => {
            const editionIdMatch = edition.getAttribute('onclick')?.match(/edition_(\d+)/);
            if (editionIdMatch) {
                const tbody = document.getElementById(`edition_${editionIdMatch[1]}`);
                if (tbody && tbody.style.display === 'none') {
                    edition.click();
                }
            }
        });

        document.querySelectorAll('a[onclick], a[href*="torrentid="]').forEach(a => {
            let torrentId = null;

            const onclick = a.getAttribute('onclick') || '';
            const href = a.getAttribute('href') || '';

            // Try matching from onclick first
            const onclickMatch = onclick.match(/torrent_(\d+)/);
            if (onclickMatch) {
                torrentId = onclickMatch[1];
            }

            // Fallback: try extracting from href if not found
            if (!torrentId) {
                const hrefMatch = href.match(/torrentid=(\d+)/);
                if (hrefMatch) {
                    torrentId = hrefMatch[1];
                }
            }

            if (torrentId) {
                const torrentRow = document.getElementById(`torrent_${torrentId}`);
                if (torrentRow && torrentRow.classList.contains('hidden')) {
                    jQuery(`#torrent_${torrentId}`).toggleClass('hidden');
                }
            }
        });

        document.querySelectorAll('a[onclick^="show_files"]').forEach(a => {
            const fileId = a.getAttribute('onclick')?.match(/\d+/)?.[0];
            if (fileId) {
                const filesDiv = document.getElementById(`files_${fileId}`);
                if (filesDiv && filesDiv.classList.contains('hidden')) {
                    show_files(fileId);
                }
            }
        });
    }

    function expandSingleEdition(edition) {
        const editionIdMatch = edition.getAttribute('onclick')?.match(/edition_(\d+)/);
        if (editionIdMatch) {
            const tbody = document.getElementById(`edition_${editionIdMatch[1]}`);
            if (tbody && tbody.style.display === 'none') {
                edition.click();
            }

            if (tbody) {
                tbody.querySelectorAll('a[onclick], a[href*="torrentid="]').forEach(a => {
                    let torrentId = null;
                    const onclick = a.getAttribute('onclick') || '';
                    const href = a.getAttribute('href') || '';

                    const onclickMatch = onclick.match(/torrent_(\d+)/);
                    if (onclickMatch) {
                        torrentId = onclickMatch[1];
                    }

                    if (!torrentId) {
                        const hrefMatch = href.match(/torrentid=(\d+)/);
                        if (hrefMatch) {
                            torrentId = hrefMatch[1];
                        }
                    }

                    if (torrentId) {
                        const torrentRow = document.getElementById(`torrent_${torrentId}`);
                        if (torrentRow && torrentRow.classList.contains('hidden')) {
                            jQuery(`#torrent_${torrentId}`).toggleClass('hidden');
                        }
                    }
                });

                tbody.querySelectorAll('a[onclick^="show_files"]').forEach(a => {
                    const fileId = a.getAttribute('onclick')?.match(/\d+/)?.[0];
                    if (fileId) {
                        const filesDiv = document.getElementById(`files_${fileId}`);
                        if (filesDiv && filesDiv.classList.contains('hidden')) {
                            show_files(fileId);
                        }
                    }
                });
            }
        }
    }

    const loader = setInterval(() => {
        if (document.getElementById('expand-all-button') || document.querySelector('.expand-edition-button')) {
            clearInterval(loader);
        } else {
            insertExpandAllButton();
            insertInlineEditionButtons();
        }
    }, 200);
})();