Usuwanie reklam Allegro

Usuwa wszystkie reklamy na Allegro.pl, które nie są blokowane przez zwykłe blokery reklam, np. "Sponsorowane produkty" i "sponsorowane" boksy.

// ==UserScript==
// @name            Usuwanie reklam Allegro
// @name:en         Remove Allegro ads
// @namespace       http://tampermonkey.net/
// @version         0.7.0.0
// @description     Usuwa wszystkie reklamy na Allegro.pl, które nie są blokowane przez zwykłe blokery reklam, np. "Sponsorowane produkty" i "sponsorowane" boksy.
// @description:en  Aims to remove all ads on Allegro.pl not blocked by regular ad blockers, like "Sponsorowane produkty" and "sponsorowane" ad boxes.
// @author          adamaru
// @match           *://allegro.pl/*
// @icon            https://www.google.com/s2/favicons?sz=64&domain=allegro.pl
// @grant           none
// @license         MIT
// @run-at			document-start
// ==/UserScript==

(function() {
	'use strict';
	var running = false;
	var logging = false;

	var shadowHosts = new Set();
	const original = Element.prototype.attachShadow;
	Element.prototype.attachShadow = function attachShadow(...args) {
		const result = original.apply(this, args);
		shadowHosts.add(this);
		setTimeout(removeAllegroAds, 100);
		return result;
	}

	function removeAllegroAds(mutations){
		if(mutations){
			var ignoreMutation = false;
			// skip removing ads when changes were made only to countdowns
			mutations.forEach(function(mutation) {
				var i, node;

				for (i = 0; i < mutation.addedNodes.length; i++) {
					node = mutation.addedNodes[i];
					if(!node.className || !node.className.indexOf || node.className.indexOf('countdown') !== -1){
						ignoreMutation = true;
						return;
					}
				}

				for (i = 0; i < mutation.removedNodes.length; i++) {
					node = mutation.removedNodes[i];
					if(!node.className || !node.className.indexOf || node.className.indexOf('countdown') !== -1){
						ignoreMutation = true;
						return;
					}
				}
			});
			if(ignoreMutation){
				// only countdown changes, skip
				return;
			}
		}

		if(running){
			// already running, skip
			return;
		}
		running = true;

		log("triggered");

		removeAllegroPremiumAds();
		removeAllegroOfferAds();
		removeAllegroSponsoredAds();
		removeAllegroAdArticles();
		removeSponsoredProductListings();

		running = false;
	}

	function log(...args){
		if(logging){
			console.log(...args);
		}
	}

	function removeAllegroOfferAds(){
		var adsBoxes = document.querySelectorAll('div[data-box-name*="_ads"],div[data-box-name="seoLazyBelowFilters"]');
		for(var i = 0; i < adsBoxes.length; ++i){
			if(adsBoxes[i].style.display === "none"){
				continue;
			}
			log("hiding offer ad", adsBoxes[i]);
			adsBoxes[i].style.display = "none";
		}
	}

	function removeAllegroSponsoredAds(){
		var spansToCheck = document.querySelectorAll('section[class^="_"] > div[class]');
		for(var i = 0; i < spansToCheck.length; ++i){
			if(spansToCheck[i].innerHTML.indexOf('sponsorowane') !== -1){
				if(spansToCheck[i].parentNode.style.display === "none"){
					continue;
				}
				log("hiding sponsored ad", spansToCheck[i]);
				spansToCheck[i].parentNode.style.display = "none";
			}
		}

		var sponsoredAdsV3 = document.querySelectorAll('div[data-box-name="items-v3"] article[data-analytics-view-custom-context="SPONSORED"]');
		for(var j = 0; j < sponsoredAdsV3.length; ++j){
			if(sponsoredAdsV3[j].style.display === "none"){
				continue;
			}
			log("hiding sponsored ad v3", sponsoredAdsV3[j]);
			sponsoredAdsV3[j].style.display = "none";
		}
	}

	function removeAllegroAdArticles(){
		var adArticles = document.querySelectorAll('article[data-analytics-view-label="showSponsoredItems"]');
		for(var i = 0; i < adArticles.length; ++i){
			if(adArticles[i].style.display === "none"){
				continue;
			}
			log("hiding ad article", adArticles[i]);
			adArticles[i].style.display = "none";
		}
	}

	function removeAllegroPremiumAds(){
		var premiumAds = document.querySelectorAll('div[data-box-name^="premium.with.dfp"],div[data-box-name="layout.advertisement"],div[data-box-name="ads.dss.listing.bottom.container"]');
		for(var i = 0; i < premiumAds.length; ++i){
			if(premiumAds[i].style.display === "none"){
				continue;
			}
			log("hiding premium ad", premiumAds[i]);
			premiumAds[i].style.display = "none";
		}
	}

	function removeSponsoredProductListings() {
		const thresholdElement = document.querySelector('.opbox-listing > * > h2:first-of-type');

		shadowHosts.forEach(element => {
			const closestArticle = element.closest('article');
			if (closestArticle) {
				if(closestArticle.style.display === "none"){
					return;
				}
				const parentOfParentOfArticle = closestArticle.parentElement.parentElement;
				if (parentOfParentOfArticle && parentOfParentOfArticle.classList.contains('opbox-listing')) {
					if (thresholdElement.compareDocumentPosition(closestArticle) & Node.DOCUMENT_POSITION_PRECEDING) {
						log("hiding sponsored product listing", closestArticle);
						closestArticle.style.display = "none";
					} else {
					  	shadowHosts.delete(element);
					}
				} else {
					log('No matching parent of article found for:', element);
				}
			} else {
				log('No article element found for:', element);
			}
		});
	}


	removeAllegroAds(null);
	//setInterval(removeAllegroAds, 10000);

	document.addEventListener("DOMContentLoaded", () => {
		log('starting mutation observer');
		var observer = new MutationObserver(removeAllegroAds);
		var config = {childList: true, subtree: true, attributes: true};
		observer.observe(document.body, config);
	});

})();