Game Jolt Notification Filter Preserver

Preserves and restores selected filters on the notifications page of Game Jolt.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Game Jolt Notification Filter Preserver
// @name:ru        Сохранение фильтров уведомлений Game Jolt
// @namespace      http://tampermonkey.net/
// @version        0.1
// @icon           https://s.gjcdn.net/img/favicon.png
// @description    Preserves and restores selected filters on the notifications page of Game Jolt.
// @description:ru Восстанавливает выбранные фильтры на странице уведомлений Game Jolt.
// @author         Deflecta, GKProduction
// @match          https://gamejolt.com/*
// @match          https://*.gamejolt.com/*
// @grant          none
// @license        MIT
// ==/UserScript==

(function() {
    // Задаём переменные
    const notifPageUrl = 'https://gamejolt.com/notifications';

    // Сохраняем фильтры
    function saveFilters()
        {
            const urlParams = new URLSearchParams(window.location.search);
            const filters = urlParams.get('f');
            if (filters)
                {
                    localStorage.setItem('gjNotifFilters', filters);
                }
        }

    // Загружаем фильтры
    function loadFilters()
        {
            return localStorage.getItem('gjNotifFilters');
        }

    // Проверяем факт нажатия
    document.addEventListener('click', (event) =>
        {
            const viewAllButton = event.target.closest('a.button.-trans.-block');
            if (viewAllButton && viewAllButton.textContent.trim() === ('View all' || 'Посмотреть всё'))
                {
                    const filters = loadFilters();
                    if (filters)
                        {
                            event.preventDefault();
                            window.location.href = `${notifPageUrl}?f=${filters}`;
                        }
                }
        });

    // Запоминаем фильтры в нужный момент с MutationObserver
    const observer = new MutationObserver(() =>
        {
            if (window.location.href.startsWith(notifPageUrl))
                {
                    saveFilters();
                }
        });

    // Запускаем MutationObserver
    observer.observe(document.body,
        {
            childList: true, subtree: true 
        });

    // Сохраняем фильтры, если мы в уведомлениях
    if (window.location.href.startsWith(notifPageUrl))
        {
            saveFilters();
        }
})();