diff --git a/readthedocs/core/static-src/core/js/doc-embed/sponsorship.js b/readthedocs/core/static-src/core/js/doc-embed/sponsorship.js index 40b8be063fd..647ee0a3f44 100644 --- a/readthedocs/core/static-src/core/js/doc-embed/sponsorship.js +++ b/readthedocs/core/static-src/core/js/doc-embed/sponsorship.js @@ -163,6 +163,27 @@ Promo.prototype.post_promo_display = function () { } }; +function detect_adblock() { + // Status codes are not correctly reported on JSONP requests + // So we resort to different ways to detect adblockers + var detected = false; + + // Check if our ad element is blocked + $('
') + .attr('id', 'rtd-detection') + .attr('class', 'ethical-rtd') + .html(' ') + .appendTo('body'); + if ($('#rtd-detection').height() === 0) { + detected = true; + } + + // Remove the test element regardless + $('#rtd-detection').remove(); + + return detected; +} + function adblock_admonition() { console.log('---------------------------------------------------------------------------------------'); console.log('Read the Docs hosts documentation for tens of thousands of open source projects.'); @@ -256,10 +277,10 @@ function init() { promo.display(); } }, - error: function (xhr, textStatus, errorThrown) { + error: function () { console.error('Error loading Read the Docs promo'); - if (!rtddata.ad_free && xhr && xhr.status === 404 && rtd.api_host === 'https://readthedocs.org') { + if (!rtddata.ad_free && rtd.api_host === 'https://readthedocs.org' && detect_adblock()) { adblock_admonition(); adblock_nag(); }