Skip to content

Commit

Permalink
ability to block ads inside "sidebar" when image is opened full screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Kulp committed May 5, 2023
1 parent 781730e commit e7d25da
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,27 @@ function hideAd(ad) {
console.log('Twitter ads hidden: ', adsHidden.toString());
}

function getAndHideAds() {
getAds().forEach(hideAd)
}

// hide ads on page load
document.addEventListener('load', () => getAds().forEach(hideAd));
document.addEventListener('load', () => getAndHideAds());

// oftentimes, tweets render after onload. LCP should catch them.
new PerformanceObserver((entryList) => {
getAds().forEach(hideAd);
getAndHideAds();
}).observe({type: 'largest-contentful-paint', buffered: true});

// re-check as user scrolls
document.addEventListener('scroll', () => getAds().forEach(hideAd));
document.addEventListener('scroll', () => getAndHideAds());

// re-check as user scrolls tweet sidebar (exists when image is opened)
var sidebarExists = setInterval(function() {
let timelines = document.querySelectorAll("[aria-label='Timeline: Conversation']");

if (timelines.length == 2) {
let tweetSidebar = document.querySelectorAll("[aria-label='Timeline: Conversation']")[0].parentElement.parentElement;
tweetSidebar.addEventListener('scroll', () => getAndHideAds());
}
}, 500);

0 comments on commit e7d25da

Please sign in to comment.