-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontent.js
42 lines (35 loc) · 1.18 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable no-undef */
const adSelectors = [
'iframe[src*="ads"]',
'div[class*="ad"]',
'div[id*="ad"]',
'ins.adsbygoogle',
'[data-ad]',
'.ad-banner',
];
// Normalize domain
const normalizeDomain = (domain) => domain.replace(/^www\./, '');
chrome.storage.local.get(
{ adBlockerEnabled: true, whitelist: [] },
({ adBlockerEnabled, whitelist }) => {
if (!adBlockerEnabled) return;
const currentSite = normalizeDomain(window.location.hostname);
const normalizedWhitelist = whitelist.map(normalizeDomain);
if (normalizedWhitelist.includes(currentSite)) {
console.log(`Whitelist active: Ads are allowed on ${currentSite}`);
return; // Skip ad blocking
}
// Ad blocking logic
const blockAds = () => {
adSelectors.forEach((selector) => {
const ads = document.querySelectorAll(selector);
console.log(`Found ${ads.length} ads for selector: ${selector}`);
ads.forEach((ad) => ad.remove());
});
};
blockAds(); // Initial blocking
// Observe dynamically loaded ads
const observer = new MutationObserver(blockAds);
observer.observe(document.body, { childList: true, subtree: true });
},
);