Greasy Fork

AB - Improved Search Categories

Highlights the current categories. And preserves your search, along side any filters you might have set, when switching between "Anime" and "Music" or their subcategories on AB search.

目前为 2022-12-02 提交的版本。查看 最新版本

// ==UserScript==
// @name          AB - Improved Search Categories
// @namespace     [email protected]
// @version       1.0.0
// @author        TalkingJello
// @description   Highlights the current categories. And preserves your search, along side any filters you might have set, when switching between "Anime" and "Music" or their subcategories on AB search.
// @icon          http://animebytes.tv/favicon.ico
// @license       MIT
// @match         *://animebytes.tv/torrents.php*
// @match         *://animebytes.tv/torrents2.php*
// ==/UserScript==

function categoryKeyFromLink(link) {
    for (const key of [...(new URL(link).searchParams.keys())]) {
        if (key.startsWith('filter_cat[')) {
            return key;
        }
    }

    return false;
}

(function() {
    'use strict';

    // Prep work
    const currentCategory = categoryKeyFromLink(window.location.href);

    // Move inside Anime or inside Music between categories
    $('#categories > li > a').each(function () {
        const thisLinkCategory = categoryKeyFromLink($(this).prop('href'));

        // Make url without category filter
        const targetUrl = new URL(window.location.href);
        if(currentCategory) {
            targetUrl.searchParams.delete(currentCategory);
        }

        // Uncategory search
        if (thisLinkCategory === currentCategory) {
            $(this).css('color', 'pink');
            $(this).prop('href', targetUrl.toString())
            return;
        }

        // intentionally not editing search params to avoid encoding the "[]"
        if (targetUrl.search) {
            targetUrl.search += `&${thisLinkCategory}=1`;
        } else {
            targetUrl.search = `?${thisLinkCategory}=1`;
        }
        $(this).prop('href', targetUrl.toString());
    });

    // Move between Anime and Music
    const animeLink = $('#browse_nav_sections > h2 > a[href="/torrents.php"]');
    const musicLink = $('#browse_nav_sections > h2 > a[href="/torrents2.php"]');

    // highlight active
    const isMusic = window.location.pathname.startsWith('/torrents2.php');
    const activeLink = isMusic ? musicLink : animeLink;
    activeLink.css('color', '#fe2a73');
    activeLink.css('cursor', 'default');
    activeLink.attr('href', 'javascript:void(0);');

    // Patch href
    const ANIME_MUSIC_SHARED_PARAMS = ['year', 'year2', 'tags', 'sort', 'way', 'showhidden', 'freeleech'];
    const params = new URL(window.location.href).searchParams;
    for (const [key, value] of [...params.entries()]) {
        if (isMusic && key === 'groupname') {
            params.set('searchstr', value)
        }
        if (!isMusic && key === 'searchstr') {
            params.set('groupname', value)
        }

        if (!ANIME_MUSIC_SHARED_PARAMS.includes(key)) {
            params.delete(key);
        }
    }

    if (isMusic) {
        animeLink.attr('href', `/torrents.php?${params.toString()}`);
    } else {
        musicLink.attr('href', `/torrents2.php?${params.toString()}`)
    }
})();