OLX UA Specific AdCard Hider with Toggle

Hide specific divs on OLX UA with a toggle slider

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         OLX UA Specific AdCard Hider with Toggle
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Hide specific divs on OLX UA with a toggle slider
// @author       max5555
// @grant        none
// @license      MIT
// @match        https://www.olx.ua/uk/*
// ==/UserScript==

(function() {
    'use strict';

    // Function to hide divs with the specific ID pattern that contains the adCard-featured
    function hideSpecificAdCards() {
        if (!document.getElementById('adCardToggleSlider').checked) return;

        const allDivs = document.querySelectorAll('div[id]');
        allDivs.forEach(div => {
            const idPattern = /^[0-9]+$/; // Matches IDs that are purely numeric
            if (idPattern.test(div.id) && div.querySelector('div[data-testid="adCard-featured"]')) {
                div.style.display = 'none';
            }
        });
    }

    // Function to show divs again
    function showSpecificAdCards() {
        const allDivs = document.querySelectorAll('div[id]');
        allDivs.forEach(div => {
            const idPattern = /^[0-9]+$/; // Matches IDs that are purely numeric
            if (idPattern.test(div.id) && div.querySelector('div[data-testid="adCard-featured"]')) {
                div.style.display = 'block';
            }
        });
    }

    // Create a slider at the top right for toggling
    const sliderHTML = `
        <div style="position: fixed; top: 10px; right: 10px; z-index: 9999; background-color: white; padding: 5px; border: 1px solid #ccc;">
            <label>
                Hide Ad Card:
                <input id="adCardToggleSlider" type="checkbox" checked>
            </label>
        </div>
    `;
    document.body.insertAdjacentHTML('beforeend', sliderHTML);

    // Attach event listener to the slider
    document.getElementById('adCardToggleSlider').addEventListener('change', function() {
        if (this.checked) {
            hideSpecificAdCards();
        } else {
            showSpecificAdCards();
        }
    });

    // Initial hiding of the divs
    hideSpecificAdCards();

    // Using a MutationObserver to handle dynamic loading of content
    const observer = new MutationObserver(() => {
        if (document.getElementById('adCardToggleSlider').checked) {
            hideSpecificAdCards();
        }
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();