Skip to content

Instantly share code, notes, and snippets.

@laocoi
Last active October 2, 2023 05:03
Show Gist options
  • Save laocoi/b4d7cfbc43c86f6755e59bf922e63442 to your computer and use it in GitHub Desktop.
Save laocoi/b4d7cfbc43c86f6755e59bf922e63442 to your computer and use it in GitHub Desktop.
Remove empty Google Adsense Ads if it is unfilled
var ads = document.querySelectorAll('ins');
ads.forEach(ad => {
const observer = new MutationObserver( mutations =>
{
// console.log(mutations);
mutations.forEach( record =>
{
if(record.type === 'attributes')
{
const attrname = record.attributeName;
const attrvalue = record.target.getAttribute(attrname);
// console.log("attribute-name = ", attrname);
// console.log("attribute-value = ", attrvalue);
if(attrname === 'data-ad-status' && attrvalue === 'unfilled'){
$(ad).closest('.grid__item').remove()
}
}
});
});
observer.observe(ad, {
attributes: true,
attributeFilter: ['data-ad-status']
});
})
#Modified from source https://elomymelo.com/mutation-observer-to-customize-ads.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment