Skip to content

Improved ad block detection #4532

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

Merged
merged 3 commits into from
Aug 17, 2018
Merged
Changes from 2 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
21 changes: 20 additions & 1 deletion 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,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
$('<div />')
.attr('id', 'rtd-detection')
.attr('class', 'ethical-rtd')
.html('&nbsp;')
.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.');
Expand Down Expand Up @@ -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();
}
Expand Down