remove ads bookmarklet

by Laurens Maneschijn

HTML

<h1>
  JS bookmarklet: remove all ad iframes and containers.
</h1>
<p>
	Note the logic is fairly basic, and there will be false positives and misses.<br>
	Uses only native JS, no jQuery or other plugin required.
</p>
Removes:
<ul>
  <li>all iframe and video tags with a src domain different from the current page domain, but keeps whitelisted domains,</li>
  <li>all iframes that look like a flat bar, or slim and high (very low or very high width/height ratio),</li>
  <li>and a set of custom listed elements.</li>
</ul>

<p>
  See script panel for original (non minified) code.
</p>

<p>
  Drag link to bookmarks:<br>

  <a href="javascript:(function(window,document){var domain = (''+document.location.host).split('.').slice(-2).join('.');if(domain.length < 6){domain = (''+document.location.host).split('.').slice(-3).join('.');}function RegExpEscape(s){return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');}var log = window.console && window.console.log ? window.console.log : function(){};function removeElement(el){try{el.remove();}catch(e){log('Problem removing element:', el);log(e);}}function getsize(el){var w = parseInt(el.offsetWidth || el.width, 10);var h = parseInt(el.offsetHeight || el.height, 10);return {width:w, height:h};}var re = RegExp('^.{0,8}(\\w+\\.)*' + RegExpEscape(domain) + '/', 'i');[].slice.apply(document.querySelectorAll('iframe,video')).forEach(function(el){if( typeof el.src === 'string' && el.src && ! el.src.match( re )){removeElement(el);}});var q = `.adsbygoogle, .everyonelovesstackoverflow, .jpx-pa-wrapper, .jpx-pu-wrapper, .jpx-sa-wrapper, .jpx-as-wrapper, [class*=&quot;jpx-&quot;][class*=&quot;-wrapper&quot;], [class*=&quot;adzerk&quot;], [class$=&quot;_ads&quot;], [id*=&quot;adzerk&quot;], [id*=&quot;google_ad&quot;], ins[id*=&quot;aswift&quot;], [href*=&quot;buysellads.com&quot;], img[src*=&quot;buysellads.com&quot;], #TOP_LEADERBOARD_AB, iframe[src*=&quot;/ads/&quot;], iframe[id*=&quot;google_ads_iframe&quot;], [href*=&quot;adclick&quot;],...

CSS

h1,h2,h3,h4,h5,h6{font-size:1em;padding:0 0 0 0;margin:2px 0 2px 0;}
ul,li{margin:0 0 0 0;}

JavaScript

// TIP: I used my jsfiddle to minify js below to one line for bookmarklet: 
//      https://jsfiddle.net/ElMoonLite/ndg24dso/

// TODO: Clean up a bit.

// TODO: custom elements: purge, update, remove?
// The list of custom elements selector is very old, it needs purging and updating.
// Perhaps even remove it altogether, as the domain and size methods seems to catch most things.


// Intentional error to prevent running code below in this jsfiddle:
throw new Error();



(function(window,document){
	var domain = (''+document.location.host).split('.').slice(-2).join('.');
	if(domain.length < 6){
		domain = (''+document.location.host).split('.').slice(-3).join('.');
	}
	function RegExpEscape(s){
		return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
	}
	var log = window.console && window.console.log ? 
		window.console.log.bind(window.console) : function(){};
	function findSameSizeParentRoot(el, max_delta_width, max_delta_height){
		var el_size = getsize(el), p, p_size, target = el;
		if (!el_size.width || el_size.width < 10 || ! el_size.height || el_size.height < 10) {
			return target;
		}
		while (true) {
			p = target.parentNode;
			if (!p || p === el) {
				break;
			}
			p_size = getsize(p);
			if (!p_size.width || !p_size.height) {
				break;
			}
			if (Math.abs(el_size.width - p_size.width) > max_delta_width) {
				break;
			}
			if (Math.abs(el_size.height - p_size.height) > max_delta_height) {
				break;
			}
			target = p;
		}
		return target;
	}
	function removeElement(el){
		el = findSameSizeParentRoot(el, 30, 30) || el;
		try{
			el.remove();
		}catch(e){
			log('Problem removing element:', el);
			log(e);
		}
	}
	function getsize(el){
		var w = parseInt(el.offsetWidth || el.width, 10);
		var h = parseInt(el.offsetHeight || el.height, 10);
		return {width:w, height:h};
	}
	var whitelist_domains = ['youtube','youtu.be','youtube-nocookie.com','googlevideo.com','ytimg.com','apis.google.com'];
	function isWhitelistUrl(src){
		try {
			var...