Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved ad block detection #4532

Merged
merged 3 commits into from
Aug 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions readthedocs/core/static-src/core/js/doc-embed/sponsorship.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
$('<div />')
.attr('id', 'rtd-detection')
.attr('class', 'ethical-rtd')
.html('&nbsp;')
.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.');
Expand Down Expand Up @@ -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();
}
Expand Down