☰

Sankaku Subscription Banner Remove

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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();
})();