Skip to content

Commit 5311ef3

Browse files
authored
Merge pull request #4532 from rtfd/davidfischer/better-ad-block-detection
Improved ad block detection
2 parents 59d0435 + 72dd895 commit 5311ef3

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

readthedocs/core/static-src/core/js/doc-embed/sponsorship.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ Promo.prototype.post_promo_display = function () {
163163
}
164164
};
165165

166+
function detect_adblock() {
167+
// Status codes are not correctly reported on JSONP requests
168+
// So we resort to different ways to detect adblockers
169+
var detected = false;
170+
171+
// Check if our ad element is blocked
172+
$('<div />')
173+
.attr('id', 'rtd-detection')
174+
.attr('class', 'ethical-rtd')
175+
.html('&nbsp;')
176+
.appendTo('body');
177+
if ($('#rtd-detection').height() === 0) {
178+
detected = true;
179+
}
180+
181+
// Remove the test element regardless
182+
$('#rtd-detection').remove();
183+
184+
return detected;
185+
}
186+
166187
function adblock_admonition() {
167188
console.log('---------------------------------------------------------------------------------------');
168189
console.log('Read the Docs hosts documentation for tens of thousands of open source projects.');
@@ -256,10 +277,10 @@ function init() {
256277
promo.display();
257278
}
258279
},
259-
error: function (xhr, textStatus, errorThrown) {
280+
error: function () {
260281
console.error('Error loading Read the Docs promo');
261282

262-
if (!rtddata.ad_free && xhr && xhr.status === 404 && rtd.api_host === 'https://readthedocs.org') {
283+
if (!rtddata.ad_free && rtd.api_host === 'https://readthedocs.org' && detect_adblock()) {
263284
adblock_admonition();
264285
adblock_nag();
265286
}

0 commit comments

Comments
 (0)