Kick.com Language Filter Memory

Automatically remembers and restores your selected language filter on Kick.com browse page

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         Kick.com Language Filter Memory
// @version      1.1
// @description  Automatically remembers and restores your selected language filter on Kick.com browse page
// @description:pl Automatycznie zapamiętuje i przywraca wybrany filtr języka na stronie przeglądania Kick.com
// @author       Premiumsmart
// @match        https://kick.com/*
// @icon         https://kick.com/favicon.ico
// @license      MIT
// @grant        none
// @namespace    https://greasyfork.org/users/1353836
// ==/UserScript==

(function() {
    'use strict';

    const STORAGE_KEY = 'kick_language_filter';
    const DEFAULT_SORT = 'viewers_high_to_low';

    // Tylko główna lista /browse (nie /browse/categories itd.)
    const isBrowseRoot = () => /^\/browse\/?$/.test(location.pathname);

    // Kick używa comma-separated (?languages=pl lub ?languages=pl,en) — get() zwraca całość.
    const getUrlLang = () => new URLSearchParams(location.search).get('languages');

    const getSaved = () => {
        try { return localStorage.getItem(STORAGE_KEY); } catch (e) { return null; }
    };
    const save = (lang) => {
        try {
            if (lang) localStorage.setItem(STORAGE_KEY, lang);
            else localStorage.removeItem(STORAGE_KEY);
        } catch (e) { /* localStorage niedostępny — ignoruj */ }
    };

    // Pamiętamy poprzednią lokalizację, by ODRÓŻNIĆ dwa scenariusze,
    // które w URL wyglądają identycznie (/browse bez ?languages):
    //   A) użytkownik przyszedł z zewnątrz (home, kanał, F5) → PRZYWRÓĆ zapisany filtr
    //   B) użytkownik był na /browse z filtrem i go WYCZYŚCIŁ → uszanuj, zapomnij
    let prev = { path: '', lang: null };

    function apply() {
        const path = location.pathname;
        const cur = getUrlLang();

        if (isBrowseRoot()) {
            if (cur) {
                // Filtr obecny w URL → zapamiętaj (URL zawsze wygrywa)
                if (cur !== getSaved()) {
                    save(cur);
                    console.log(`[Kick Filter] Zapisano język: ${cur}`);
                }
            } else {
                const saved = getSaved();
                const clearedByUser = /^\/browse\/?$/.test(prev.path) && !!prev.lang;

                if (clearedByUser) {
                    // Scenariusz B — nie walcz z użytkownikiem
                    save(null);
                    console.log('[Kick Filter] Filtr wyczyszczony przez użytkownika — usunięto pamięć');
                } else if (saved) {
                    // Scenariusz A — przywróć zapisany filtr (jednorazowy redirect)
                    const params = new URLSearchParams(location.search);
                    params.set('languages', saved);
                    if (!params.has('sort')) params.set('sort', DEFAULT_SORT);
                    console.log(`[Kick Filter] Przywracam zapisany język: ${saved}`);
                    prev = { path, lang: saved };
                    location.replace(path + '?' + params.toString());
                    return; // strona się przeładuje
                }
            }
        }

        prev = { path, lang: cur };
    }

    // --- Detekcja zmian URL w SPA bez ciężkiego MutationObservera ---
    // Podpinamy się pod History API (pushState/replaceState) + popstate.
    const notify = () => window.dispatchEvent(new Event('kick:locationchange'));
    ['pushState', 'replaceState'].forEach((method) => {
        const original = history[method];
        history[method] = function(...args) {
            const result = original.apply(this, args);
            notify();
            return result;
        };
    });
    window.addEventListener('popstate', notify);
    window.addEventListener('kick:locationchange', apply);

    // Uruchom na starcie
    apply();

})();