Krysti.Engineer Official Chat
You are viewing an inferior version of the site because your browser does not support WebP. Do upgrade to something like Chrome or Firefox. Loading websites like this causes them to require a fallback set of images and they are almost always lower quality and larger in size.

Collapse Unfilled Google AdSense Blocks

Alright.. so I made this. It's slightly dirty in my opinion as it's just a 3 second page load check to see the status of the placed ads. I wanted something MAYBE a little more connected to loading process and decided against adding more timers to handle the function. So with that, I am welcoming some input with this method I came up with here.
2022-07-27T:00:00:00+00:00 July 27 2022 00:00:00

<script>
var adStop = 0;
function googleDone() {
	var g = document.getElementsByClassName('adsbygoogle');
	console.log("Loaded Ads",g.length);
	if((g.length==0)&&(adStop<2)) {
		setTimeout(function() {
			console.log("Trying to find ads again");
			googleDone();
			adStop = adStop+1;
		},1000);
	} else {
		for(a = 0; a < g.length; a++) {
			console.log("Ad", a, g[a].dataset);
			if((g[a].dataset.adsbygoogleStatus=='done')&&(g[a].dataset.adStatus == 'unfilled')) {
				if(g[a].parentNode.localName!='body') {
					console.log("Closing ad", a);
					g[a].parentNode.remove();
				} // Add custom conditions below this bracket.
			}
		}
	}
}
window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');	
	setTimeout(function() {
		googleDone();
	},3000)
});
</script>

Here we wait for DOMContentLoaded to grab the status of the ads after a 3 second waiting period. Here I figure this MIGHT be good enough to clean these unfilled blocks up and an acceptable period to not have too much UI screwery in your way here. If for whatever reason this thing can't "find" the ads it gives up after 3 attempts. Also this will not remove an ad that has the parent node of 'body'. This is likely your auto ad banner. All ads should be contained in a div or something so you do not return the parent node as 'body'. If the ad is unfilled that div will also be removed. You can see useful functionality of this on the main page article cubes when an ad doesn't load. It will remove the empty box (if detected proper).

Contact Krysti

@itskrystibitch Twitter Photo
Twitter:
@KrystiOnline
Facebook:
@KrystiOnline
IRC:
Official Chat
Direct.Me:
@Krysti
Ko-fi:
@Krysti
Photos:
Virtual Krysti
E-Mail / Notify of errors:
coder [@] krysti.engineer
Please be patient contacting me, I don't really check much of social media or anything. If you use the IRC be sure to stick around because that's how IRC works, silly. :P