John John - 3 months ago 25
jQuery Question

How can I prevent an anti-adblock script from changing the 'src' attribute of an iframe element asychronously?

I site I visit has this script:

<script type="text/javascript">
(function () {
var ca = document.createElement('script');
ca.type = 'text/javascript';
ca.async = true;
var s = document.getElementsByTagName('script')[0];
ca.src = 'http://serve.popads.net/checkInventory.php';
ca.onerror = function () {
setTimeout(function () {
$('.embed-player').attr('src', "/adblock.html")
}, 8000);
}
s.parentNode.insertBefore(ca, s);
})();
</script>


and another one as follows that is probably related:

function adBlockDetected() {
$('#block').modal({
'backdrop': 'static',
'keyboard': false,
})
}
if (window.canRunAds === undefined) {
adBlockDetected()
}


I'm trying to see the content inside an iframe:

<iframe src="https://example.com" frameborder="0" class="embed-player" scrolling="no" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" rel="nofollow"></iframe>


but the
src
attribute keeps getting updated asynchronously by the first script above - with a warning message asking me to turn off my adblocker if want to use the site.

I wrote this Tampermonkey script to update the page document in my browser:

(function() {
'use strict';

var embedSrc = $('.embed-player').attr('src');

setTimeout(function () {
$('.embed-player').attr('src', embedSrc)
}, 16000);


})();


THIS WORKS! . . . BUT . . .

The problem is that I had to set a very high timeout value of 16000 ms to counteract the 8000 ms value used on the page - because sometimes the page's own async call comes back later than expected.

This leads to the annoyance of having to wait for both async calls to complete before I can view the content of the page.

Is there a more effective approach than the one I'm using?

BTW: I already have Reek's Anti-Adblock Killer Tampermonkey script and UBlock Origin filter installed - but for some reason the script on this site is bypassing that code (I've checked and it works on most other sites).

Answer

There are several ways to hijack that code.

For example, you can prevent setting onerror properties on script elements:

Object.defineProperty(HTMLScriptElement.prototype, 'onerror', {
  setter: function() {}
});

You could also hijack $.fn.attr to prevent it from changing src to "/adblock.html":

var $attr = $.fn.attr;
$.fn.attr = function(attr, value) {
  if(attr === "src" && value === "/adblock.html") {
    return this;
  }
  return $attr.apply(this, arguments);
}