Skip to content

Instantly share code, notes, and snippets.

@money4honey
Last active October 26, 2018 07:14
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save money4honey/492cf2fe47398541c6731e2ad0c36638 to your computer and use it in GitHub Desktop.
// WARNING: jquery required!
// jquery cdn: https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js
function detectAdblock(selector) {
return ($($(selector)[0]).css('display') == 'none');
}
function preventAdblockStyles(selector) {
$(selector).attr('style', 'display: block!important;visibility: visible!important;text-decoration: none!important;');
}
function antiAdblock(opt) {
opt = opt || {};
opt.target = opt.target || '_blank';
var rand_id = Math.random().toString(36).substring(7);
var banner = '<div id="' + rand_id + '" style="width:' + opt.w + 'px!important;height:' + opt.h + 'px!important;background-image:url(\'' + opt.img + '\') !important;background-repeat:repeat repeat!important;background-position:center center!important;cursor:pointer!important;" onclick="window.open(\'' + opt.link + '\', \'' + opt.target + '\')" title="' + opt.title + '">' + (opt.fuck_adblock ? '<span style="background:white;float: right;font-size: 10px;font-weight: bold;opacity: 0.8;" title="Мы оставляем за собой право показывать рекламу на сайтах где Вы используете AdBlock"><img src="http://savepic.ru/9710734.png" style="width: 25px;">Реклама от AdBlock</span>' : '') + '</div>';
$(opt.selector).append(banner);
return rand_id;
}
// usage example
// when page fully loaded
$(window).load(function() {
console.info('page load');
var adblock_detected = false;
adblock_detected = detectAdblock('ins.adsbygoogle'); // google ads
console.info('adblock detected: ' + adblock_detected);
// if adblock detected
if (adblock_detected) {
preventAdblockStyles('ins.adsbygoogle');
// create aliexpress ad
antiAdblock({
selector: 'body', // ad selector (where ad will be appended)
w: 728, // ad width
h: 90, // ad height
link: 'https://alitems.com/g/b611e78a21c46540cbbd16525dc3e8/?i=4', // ad link
img: 'https://alitems.com/b/b611e78a21c46540cbbd16525dc3e8/', // ad img
title: 'С любовью от Aliexpress', // ad title
fuck_adblock: true // show 'Реклама от AdBlock'
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment