☰

Sankaku Subscription Banner Remove

Auto-close popup only on /posts/<id> pages

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey or Violentmonkey 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         Sankaku Subscription Banner Remove
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Auto-close popup only on /posts/<id> pages
// @icon         https://www.google.com/s2/favicons?sz=64&domain=sankakucomplex.com
// @match        https://www.sankakucomplex.com/*
// @match        https://sankaku.app/*
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const SHORT_RETRY_MS = [120, 360];
    let lastUrl = location.href;
    let observer = null;
    let active = false;

    function isMainPostPage(url = location.href) {
        const u = new URL(url);
        const parts = u.pathname.split('/').filter(Boolean);

        // Must start with "posts"
        if (parts[0] !== "posts") return false;

        // Exclude "/posts/changes"
        if (parts[1] === "changes") return false;

        // Exclude "/posts/<id>/something"
        if (parts.length >= 3) return false;

        // Valid page: /posts/<id>
        return parts.length === 2;
    }

    function closeDialog(dialog) {
        try {
            const btn = dialog.querySelector('button[aria-label="close"]');
            if (!btn) return false;

            btn.click();
            return true;

        } catch {
            return false;
        }
    }

    function closeAllDialogs() {
        document.querySelectorAll('.MuiDialog-root').forEach(dialog => {
            closeDialog(dialog);
        });
    }

    function startObserver() {
        if (observer) return;

        observer = new MutationObserver(closeAllDialogs);
        observer.observe(document.documentElement, { childList: true, subtree: true });
    }

    function stopObserver() {
        if (!observer) return;

        observer.disconnect();
        observer = null;
    }

    function enable() {
        if (active) return;
        active = true;

        startObserver();
        closeAllDialogs();

        SHORT_RETRY_MS.forEach(ms =>
            setTimeout(closeAllDialogs, ms)
        );
    }

    function disable() {
        if (!active) return;
        active = false;

        stopObserver();
    }

    function handleUrlChange() {
        const now = location.href;
        if (now === lastUrl) return;
        lastUrl = now;

        if (isMainPostPage(now)) enable();
        else disable();
    }

    const wrap = fn => function(...args) {
        const r = fn.apply(this, args);
        window.dispatchEvent(new Event('locationchange'));
        return r;
    };

    history.pushState = wrap(history.pushState);
    history.replaceState = wrap(history.replaceState);

    window.addEventListener('popstate', () => {
        window.dispatchEvent(new Event('locationchange'));
    });

    window.addEventListener('locationchange', handleUrlChange);

    if (isMainPostPage()) enable();
})();