Coomer Video Filter

Filters posts without videos on Coomer

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==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();

})();