Last active
July 12, 2020 05:16
-
-
Save L422Y/6f3577d4051d021af4c28b211fc23f55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// adapted from https://stackoverflow.com/a/61121577/887164 | |
// replace `adsbygoogle` in your ad unit class name with `ADSENSE` | |
let _rsztmr; | |
// call to updateAds after resize (debounced) | |
window.onresize = function () { | |
clearTimeout(_rsztmr); | |
_rsztmr = setTimeout(updateAds, 100); | |
} | |
// finds visible google ads, swap the class, | |
function updateAds() { | |
let matches = document.querySelectorAll("ins.ADSENSE"); | |
Array.from(matches).forEach((element) => { | |
// jQuery checks parent elements as well | |
if (!jQuery(element).is(":visible")) { | |
// element.remove(); | |
} else { | |
element.classList.remove("ADSENSE"); | |
element.classList.add("adsbygoogle"); | |
// touch the queue to process newly visible ad units | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
} | |
}); | |
} | |
// queue ads visible on load | |
window.addEventListener('load',updateAds); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment