Wallapop Filtrar articulos de un vendedor por palabras

Añade un campo de texto siempre visible en la parte lateral inferior de la página de perfil de usuario de Wallapop y filtra artículos según el texto ingresado.

// ==UserScript==
// @name         Wallapop Filtrar articulos de un vendedor por palabras
// @namespace    http://tampermonkey.net/
// @author       Sergi0
// @version      1.0
// @description  Añade un campo de texto siempre visible en la parte lateral inferior de la página de perfil de usuario de Wallapop y filtra artículos según el texto ingresado.
// @match        https://es.wallapop.com/*
// @grant        none
// @icon         https://es.wallapop.com/favicon.ico
// @language     es
// @grant        none
// @license MIT
// @homepageURL  https://greasyfork.org/es/scripts/500126-wallapop-filtrar-articulos-de-un-vendedor-por-palabras
// @supportURL   https://greasyfork.org/es/scripts/500126-wallapop-filtrar-articulos-de-un-vendedor-por-palabras/feedback
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener('load', function() {
        let currentURL = window.location.href;
        let cargarProductosInterval = null;

        function crearCampoDeTexto() {
            if (document.getElementById('customNoteContainer')) return;

            const container = document.createElement('div');
            container.id = 'customNoteContainer';
            container.style.position = 'fixed';
            container.style.bottom = '10px';
            container.style.right = '-310px';
            container.style.width = '300px';
            container.style.backgroundColor = '#1abc9c';
            container.style.opacity = '0';
            container.style.color = 'white';
            container.style.padding = '10px';
            container.style.textAlign = 'left';
            container.style.zIndex = '9999';
            container.style.fontWeight = 'bold';
            container.style.transition = 'opacity 2s ease, right 2s ease';
            container.style.borderRadius = '10px';
            container.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';

            const textArea = document.createElement('textarea');
            textArea.style.width = '100%';
            textArea.style.height = '100%';
            textArea.style.backgroundColor = 'inherit';
            textArea.style.color = 'inherit';
            textArea.style.border = 'none';
            textArea.style.outline = 'none';
            textArea.style.fontFamily = 'inherit';
            textArea.style.fontSize = 'inherit';
            textArea.style.resize = 'none';
            textArea.style.fontWeight = 'bold';
            textArea.style.color = '#ffffff';
            textArea.placeholder = 'Escribe aquí para filtrar artículos...';

            const style = document.createElement('style');
            style.textContent = `
                #customNoteContainer textarea::placeholder {
                    color: #ffffff;
                    font-weight: bold;
                }
            `;
            document.head.appendChild(style);

            textArea.addEventListener('input', function() {
                const searchText = textArea.value.toLowerCase();
                const items = document.querySelectorAll('.ItemCardList__item');
                items.forEach(item => {
                    const titleElement = item.querySelector('.ItemCard__title.my-1');
                    if (titleElement) {
                        const titleText = titleElement.textContent.toLowerCase();
                        item.style.display = titleText.includes(searchText) ? '' : 'none';
                    }
                });
            });

            container.appendChild(textArea);
            document.body.appendChild(container);

            setTimeout(() => {
                container.style.opacity = '0.7';
                container.style.right = '10px';
            }, 100);
        }

        function ocultarCampoDeTexto() {
            const container = document.getElementById('customNoteContainer');
            if (container) {
                container.style.opacity = '0';
                container.style.right = '-310px';
                setTimeout(() => {
                    if (container.parentNode) {
                        container.parentNode.removeChild(container);
                    }
                }, 2000);
            }
        }

        function checkURLChange() {
            if (currentURL !== window.location.href) {
                currentURL = window.location.href;
                if (currentURL.startsWith('https://es.wallapop.com/app/user/')) {
                    crearCampoDeTexto();
                    iniciarCargaProductos();
                } else {
                    ocultarCampoDeTexto();
                    detenerCargaProductos();
                }
            }
        }

        function iniciarCargaProductos() {
            cargarProductosInterval = setInterval(() => {
                const botonVerMas = document.evaluate('//button[contains(text(), "Ver más productos")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                if (botonVerMas && isVisible(botonVerMas)) {
                    botonVerMas.click();
                }
            }, 1000);
        }

        function detenerCargaProductos() {
            clearInterval(cargarProductosInterval);
        }

        function isVisible(element) {
            return element.offsetWidth > 0 || element.offsetHeight > 0;
        }

        const observer = new MutationObserver(checkURLChange);
        observer.observe(document.body, { childList: true, subtree: true });

        setInterval(checkURLChange, 1000);

        if (currentURL.startsWith('https://es.wallapop.com/app/user/')) {
            crearCampoDeTexto();
            iniciarCargaProductos();
        }
    });
})();