Skip to content

Instantly share code, notes, and snippets.

@L422Y
Last active July 12, 2020 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L422Y/6f3577d4051d021af4c28b211fc23f55 to your computer and use it in GitHub Desktop.
Save L422Y/6f3577d4051d021af4c28b211fc23f55 to your computer and use it in GitHub Desktop.
// 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