From 9b3e7fe0fcd714aed4b1e671a91a11e5dd6561ac Mon Sep 17 00:00:00 2001 From: David Fischer Date: Thu, 16 Aug 2018 13:24:56 -0700 Subject: [PATCH 1/3] Improved ad block detection --- .../core/js/doc-embed/sponsorship.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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..c7c37aeec0d 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,25 @@ Promo.prototype.post_promo_display = function () { } }; +function detect_adblock(xhr) { + if (xhr && xhr.status === 404) { + // Any other status (eg. 500, 502) implies that ads are down + // for a reason other than ad-blocking + + // Check if our ad element is blocked + $('
') + .attr('id','rtd-detection') + .attr('class', 'ethical-rtd') + .html(' ') + .appendTo('body'); + if ($('#rtd-detection').height() === 0) { + return true; + } + } + + return false; +} + function adblock_admonition() { console.log('---------------------------------------------------------------------------------------'); console.log('Read the Docs hosts documentation for tens of thousands of open source projects.'); @@ -259,7 +278,7 @@ function init() { error: function (xhr, textStatus, errorThrown) { 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(xhr)) { adblock_admonition(); adblock_nag(); } From 0395d4ac997bb9e48c72a89dfbedda0520ed0ee3 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Thu, 16 Aug 2018 13:31:19 -0700 Subject: [PATCH 2/3] Linted --- readthedocs/core/static-src/core/js/doc-embed/sponsorship.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c7c37aeec0d..854b4174644 100644 --- a/readthedocs/core/static-src/core/js/doc-embed/sponsorship.js +++ b/readthedocs/core/static-src/core/js/doc-embed/sponsorship.js @@ -170,7 +170,7 @@ function detect_adblock(xhr) { // Check if our ad element is blocked $('
') - .attr('id','rtd-detection') + .attr('id', 'rtd-detection') .attr('class', 'ethical-rtd') .html(' ') .appendTo('body'); From 72dd89578381ce04a4e73c854f7676658b7f0c1e Mon Sep 17 00:00:00 2001 From: David Fischer Date: Fri, 17 Aug 2018 10:46:37 -0700 Subject: [PATCH 3/3] Remove the element and note statuses can't be trusted --- .../core/js/doc-embed/sponsorship.js | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) 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 854b4174644..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,23 +163,25 @@ Promo.prototype.post_promo_display = function () { } }; -function detect_adblock(xhr) { - if (xhr && xhr.status === 404) { - // Any other status (eg. 500, 502) implies that ads are down - // for a reason other than ad-blocking - - // Check if our ad element is blocked - $('
') - .attr('id', 'rtd-detection') - .attr('class', 'ethical-rtd') - .html(' ') - .appendTo('body'); - if ($('#rtd-detection').height() === 0) { - return true; - } +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; } - return false; + // Remove the test element regardless + $('#rtd-detection').remove(); + + return detected; } function adblock_admonition() { @@ -275,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 && rtd.api_host === 'https://readthedocs.org' && detect_adblock(xhr)) { + if (!rtddata.ad_free && rtd.api_host === 'https://readthedocs.org' && detect_adblock()) { adblock_admonition(); adblock_nag(); }