Skip to content

Commit 8bf1afc

Browse files
committed
add the feature block the ads
1 parent b1b3925 commit 8bf1afc

File tree

10 files changed

+161
-0
lines changed

10 files changed

+161
-0
lines changed

Source-Code/AdsBlockerExtension/background.js

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const adSelectors = [
2+
'iframe[src*="ads"]',
3+
'div[class*="ad"]',
4+
'div[id*="ad"]',
5+
"ins.adsbygoogle",
6+
"[data-ad]",
7+
".ad-banner",
8+
];
9+
10+
// Normalize domain
11+
const normalizeDomain = (domain) => domain.replace(/^www\./, "");
12+
13+
chrome.storage.local.get(
14+
{ adBlockerEnabled: true, whitelist: [] },
15+
({ adBlockerEnabled, whitelist }) => {
16+
if (!adBlockerEnabled) return;
17+
18+
const currentSite = normalizeDomain(window.location.hostname);
19+
const normalizedWhitelist = whitelist.map(normalizeDomain);
20+
21+
if (normalizedWhitelist.includes(currentSite)) {
22+
console.log(`Whitelist active: Ads are allowed on ${currentSite}`);
23+
return; // Skip ad blocking
24+
}
25+
26+
// Ad blocking logic
27+
const blockAds = () => {
28+
adSelectors.forEach((selector) => {
29+
const ads = document.querySelectorAll(selector);
30+
console.log(`Found ${ads.length} ads for selector: ${selector}`);
31+
ads.forEach((ad) => ad.remove());
32+
});
33+
};
34+
35+
blockAds(); // Initial blocking
36+
37+
// Observe dynamically loaded ads
38+
const observer = new MutationObserver(blockAds);
39+
observer.observe(document.body, { childList: true, subtree: true });
40+
}
41+
);
Loading
1.04 KB
Loading
1.52 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"manifest_version": 2,
3+
"name": "Ad Blocker",
4+
"version": "1.0",
5+
"description": "A simple ad blocker to remove advertisements from websites.",
6+
"permissions": ["activeTab", "storage"],
7+
"content_scripts": [
8+
{
9+
"matches": ["<all_urls>"],
10+
"js": ["content.js"]
11+
}
12+
],
13+
"browser_action": {
14+
"default_popup": "popup.html",
15+
"default_icon": {
16+
"16": "./icons/icon16.png",
17+
"48": "./icons/icon48.png",
18+
"128": "./icons/icon128.png"
19+
}
20+
},
21+
"icons": {
22+
"16": "./icons/icon16.png",
23+
"48": "./icons/icon48.png",
24+
"128": "./icons/icon128.png"
25+
}
26+
}
27+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
margin: 10px;
4+
width: 250px;
5+
}
6+
7+
h1 {
8+
font-size: 1.5em;
9+
margin-bottom: 10px;
10+
}
11+
12+
label {
13+
display: block;
14+
margin-bottom: 20px;
15+
}
16+
17+
input {
18+
margin-right: 10px;
19+
}
20+
21+
ul {
22+
list-style-type: none;
23+
padding: 0;
24+
}
25+
26+
li {
27+
margin: 5px 0;
28+
display: flex;
29+
justify-content: space-between;
30+
}
31+
32+
button {
33+
cursor: pointer;
34+
}
35+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Ad Blocker</title>
7+
<link rel="stylesheet" href="popup.css">
8+
</head>
9+
<body>
10+
<h1>Ad Blocker</h1>
11+
<label>
12+
<input type="checkbox" id="toggle-ad-blocker" checked>
13+
Enable Ad Blocker
14+
</label>
15+
<div>
16+
<h2>Whitelist</h2>
17+
<input type="text" id="whitelist-input" placeholder="Enter website URL">
18+
<button id="add-to-whitelist">Add</button>
19+
<ul id="whitelist"></ul>
20+
</div>
21+
<p id="status"></p>
22+
<script src="popup.js"></script>
23+
</body>
24+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const whitelistInput = document.getElementById("whitelist-input");
2+
const addToWhitelist = document.getElementById("add-to-whitelist");
3+
const whitelist = document.getElementById("whitelist");
4+
let whitelistData = JSON.parse(localStorage.getItem("whitelist")) || [];
5+
6+
// Load whitelist
7+
function loadWhitelist() {
8+
whitelist.innerHTML = "";
9+
whitelistData.forEach((site) => {
10+
const li = document.createElement("li");
11+
li.textContent = site;
12+
const removeBtn = document.createElement("button");
13+
removeBtn.textContent = "Remove";
14+
removeBtn.addEventListener("click", () => {
15+
whitelistData = whitelistData.filter((item) => item !== site);
16+
localStorage.setItem("whitelist", JSON.stringify(whitelistData));
17+
loadWhitelist();
18+
});
19+
li.appendChild(removeBtn);
20+
whitelist.appendChild(li);
21+
});
22+
}
23+
24+
addToWhitelist.addEventListener("click", () => {
25+
const site = whitelistInput.value.trim();
26+
if (site && !whitelistData.includes(site)) {
27+
whitelistData.push(site);
28+
localStorage.setItem("whitelist", JSON.stringify(whitelistData));
29+
whitelistInput.value = "";
30+
loadWhitelist();
31+
}
32+
});
33+
34+
loadWhitelist();

Source-Code/AdsBlockerExtension/style.css

Whitespace-only changes.

0 commit comments

Comments
 (0)