OLX Sponsored Begone

Hides sponsored listings on olx.pl

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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             OLX Sponsored Begone
// @name:pl          Ukrywacz ofert sponsorowanych na OLX
// @namespace        https://github.com/b13kjack/OLX-Sponsored-Begone-Script
// @version          1.1.5
// @description      Hides sponsored listings on olx.pl
// @description:pl   Ukrywa oferty sponsorowane na olx.pl
// @author           b13kjack
// @license          MIT
// @supportURL       https://github.com/b13kjack/OLX-Sponsored-Begone-Script/issues
// @match            https://olx.pl/*
// @match            https://*.olx.pl/*
// @match            https://olx.pl*
// @match            https://*.olx.pl*
// @icon             https://www.google.com/s2/favicons?sz=64&domain=olx.pl
// @grant            none
// ==/UserScript==
(function () {
    'use strict';

    const SPONSORED_TEXT = 'Wyróżnione';
    const LISTINGS_CONTAINER_SELECTOR = '[data-testid="listing-grid"]';
    const LISTING_CARD_SELECTOR = '[data-cy="l-card"]';
    const GRID_ICON_SELECTOR = '[data-testid="grid-icon"]';
    const TOGGLE_BUTTON_TESTID = 'grid-icon-duplicate';

    let areSponsoredListingsHidden = true;

    function ensureToggleButtonExists() {
        let toggleButton = document.querySelector(`[data-testid="${TOGGLE_BUTTON_TESTID}"]`);
        if (!toggleButton) {
            const referenceButton = document.querySelector(GRID_ICON_SELECTOR);
            if (referenceButton) {
                toggleButton = document.createElement('button');
                toggleButton.setAttribute('data-testid', TOGGLE_BUTTON_TESTID);
                toggleButton.textContent = areSponsoredListingsHidden ? 'Pokaż Oferty Sponsorowane' : 'Ukryj Oferty Sponsorowane';
                toggleButton.style.cssText = window.getComputedStyle(referenceButton).cssText;
                referenceButton.insertAdjacentElement('afterend', toggleButton);
                toggleButton.addEventListener('click', () => {
                    areSponsoredListingsHidden = !areSponsoredListingsHidden;
                    toggleButton.textContent = areSponsoredListingsHidden ? 'Pokaż Oferty Sponsorowane' : 'Ukryj Oferty Sponsorowane';
                    updateSponsoredListingsVisibility();
                });
            } else {
                console.warn('Grid icon not found. Cannot create toggle button.');
            }
        }
    }

    function updateSponsoredListingsVisibility() {
        const listingsContainer = document.querySelector(LISTINGS_CONTAINER_SELECTOR);
        if (!listingsContainer) {
            console.warn('Listings container not found.');
            return;
        }

        const listingCards = listingsContainer.querySelectorAll(LISTING_CARD_SELECTOR);
        if (listingCards.length === 0) {
            console.warn('No listing cards found.');
            return;
        }

        listingCards.forEach(card => {
            if (card.textContent.includes(SPONSORED_TEXT)) {
                card.style.display = areSponsoredListingsHidden ? 'none' : '';
            }
        });
    }

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

    ensureToggleButtonExists();
    updateSponsoredListingsVisibility();

    const intervalId = setInterval(() => {
        ensureToggleButtonExists();
        if (document.querySelector(`[data-testid="${TOGGLE_BUTTON_TESTID}"]`)) {
            clearInterval(intervalId);
        }
    }, 2000);
})();