Gelbooru Bulk Unfavorite

Automatically remove all favorites from gelbooru using AJAX.

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

You will need to install an extension such as Tampermonkey 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         Gelbooru Bulk Unfavorite
// @version      1.6
// @description  Automatically remove all favorites from gelbooru using AJAX.
// @author       https://github.com/binge-coder
// @match        *://gelbooru.com/index.php?page=favorites*
// @run-at       document-end
// @namespace https://greasyfork.org/users/1575858
// ==/UserScript==

(function() {
    'use strict';

    const removalDelay = 1000;
    const refreshDelay = 2000;
    const autoRun = true;

    function processFavoritesBatch() {
        // Find all <b>Remove</b> tags and get their parent <a> links
        let removeButtons = Array.from(document.querySelectorAll('b'))
            .filter(b => b.textContent.trim().toLowerCase() === "remove")
            .map(b => b.closest('a'))
            .filter(a => a !== null && a.getAttribute("href"));

        console.log("Found", removeButtons.length, "remove buttons.");

        if(removeButtons.length === 0) {
            console.log("No favorites found. Process complete.");
            return;
        }

        let index = 0;
        function removeNext() {
            if(index >= removeButtons.length) {
                console.log("Batch complete. Refreshing...");
                setTimeout(() => location.reload(), refreshDelay);
                return;
            }

            let button = removeButtons[index];
            let url = button.getAttribute("href"); // Direct access to href

            if(url) {
                console.log("Removing favorite via URL:", url);
                fetch(url, { credentials: 'include' })
                    .then(() => {
                        console.log("Removed index", index);
                        index++;
                        setTimeout(removeNext, removalDelay);
                    })
                    .catch(err => {
                        console.error("Error at index", index, err);
                        index++;
                        setTimeout(removeNext, removalDelay);
                    });
            } else {
                index++;
                removeNext();
            }
        }
        removeNext();
    }

    function addManualButton() {
        let btn = document.createElement("button");
        btn.innerText = "Remove All Favorites";
        btn.style = "position:fixed;top:10px;right:10px;z-index:1000;padding:10px;background:red;color:white;border:none;cursor:pointer;";
        btn.onclick = processFavoritesBatch;
        document.body.appendChild(btn);
    }

    addManualButton();

    if(autoRun) {
        // Short delay to ensure page is ready
        setTimeout(() => {
            const hasFavs = Array.from(document.querySelectorAll('b')).some(b => b.textContent.trim().toLowerCase() === "remove");
            if(hasFavs) processFavoritesBatch();
        }, 500);
    }
})();