Coomer Video Filter

Filters posts without videos on Coomer

Stan na 20-09-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Coomer Video Filter
// @namespace    http://coomer.su
// @version      1.4
// @description  Filters posts without videos on Coomer
// @match        https://*.coomer.party/*/user/*
// @match        https://*.kemono.party/*/user/*
// @match        https://*.coomer.su/*/user/*
// @match        https://*.kemono.su/*/user/*
// @grant        none
// ==/UserScript==

(async function() {
    'use strict';

    // Helper function to wait for elements that may load dynamically
    async function querySelectorAsync(selector) {
        return await new Promise(resolve => {
            let elm = document.querySelector(selector);
            if (elm) return resolve(elm);

            const observer = new MutationObserver(mutations => {
                elm = document.querySelector(selector);
                if (elm) {
                    resolve(elm);
                    observer.disconnect();
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
        });
    }

    // Function to check if a post contains a video
    async function hasVideo(postUrl) {
        return new Promise(resolve => {
            fetch(postUrl)
                .then(response => response.text())
                .then(html => {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(html, "text/html");
                    const videoExists = doc.querySelector("video") !== null;
                    resolve(videoExists);
                })
                .catch(err => {
                    console.error("Error fetching post:", postUrl, err);
                    resolve(false); // Assume no video if error occurs
                });
        });
    }

    // Function to filter out posts without videos
    async function filterVideos() {
        const posts = [...(await querySelectorAsync(".card-list__items")).children];
        for (let post of posts) {
            const postLink = post.querySelector("a")?.href;
            if (postLink) {
                const videoPresent = await hasVideo(postLink);
                if (!videoPresent) {
                    post.remove(); // Remove post if it doesn't have a video
                }
            }
        }
    }

    // Add a filter button to the page to trigger the video filtering
    async function addFilterButton() {
        const filterBtn = document.createElement("button");
        filterBtn.textContent = "Filter Videos";
        filterBtn.style = "position: fixed; top: 10px; right: 10px; z-index: 9999;";
        filterBtn.onclick = function() {
            filterBtn.remove(); // Remove button after it's clicked
            filterVideos(); // Start filtering videos
        };

        const form = await querySelectorAsync("form"); // Wait for a form element to append the button to
        form.appendChild(filterBtn);
    }

    // Add the filter button when the page loads
    addFilterButton();

})();