Skip to content

Commit 72dd895

Browse files
committed
Remove the element and note statuses can't be trusted
1 parent 0395d4a commit 72dd895

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

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

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

166-
function detect_adblock(xhr) {
167-
if (xhr && xhr.status === 404) {
168-
// Any other status (eg. 500, 502) implies that ads are down
169-
// for a reason other than ad-blocking
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-
return true;
179-
}
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;
180179
}
181180

182-
return false;
181+
// Remove the test element regardless
182+
$('#rtd-detection').remove();
183+
184+
return detected;
183185
}
184186

185187
function adblock_admonition() {
@@ -275,10 +277,10 @@ function init() {
275277
promo.display();
276278
}
277279
},
278-
error: function (xhr, textStatus, errorThrown) {
280+
error: function () {
279281
console.error('Error loading Read the Docs promo');
280282

281-
if (!rtddata.ad_free && rtd.api_host === 'https://readthedocs.org' && detect_adblock(xhr)) {
283+
if (!rtddata.ad_free && rtd.api_host === 'https://readthedocs.org' && detect_adblock()) {
282284
adblock_admonition();
283285
adblock_nag();
284286
}

0 commit comments

Comments
 (0)