Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c526b1c

Browse files
Merge pull request #55 from tajulafreen/Add_Blocker_Extension
50Projects-HTML-CSS-JavaScript : Ads blocker extension
2 parents 59c14c5 + 351e3a2 commit c526b1c

File tree

9 files changed

+172
-0
lines changed

9 files changed

+172
-0
lines changed

‎README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,17 @@ In order to run this project you need:
584584
</details>
585585
</li>
586586

587+
<li>
588+
<details>
589+
<summary>Ads Blocker Extension</summary>
590+
<p>This is a simple Ad Blocker Extension. AdBlocker is a lightweight browser extension designed to block intrusive advertisements and enhance your browsing experience.</p>
591+
<ul>
592+
<li><a href="https://tajulafreen.github.io/50Projects-HTML-CSS-JavaScript/Source-Code/AdsBlockerExtension/">Live Demo</a></li>
593+
<li><a href="https://github.com/tajulafreen/50Projects-HTML-CSS-JavaScript/tree/main/Source-Code/AdsBlockerExtension">Source</a></li>
594+
</ul>
595+
</details>
596+
</li>
597+
587598
</ol>
588599

589600
<p align="right">(<a href="#readme-top">back to top</a>)</p>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable no-undef */
2+
const adSelectors = [
3+
'iframe[src*="ads"]',
4+
'div[class*="ad"]',
5+
'div[id*="ad"]',
6+
'ins.adsbygoogle',
7+
'[data-ad]',
8+
'.ad-banner',
9+
];
10+
11+
// Normalize domain
12+
const normalizeDomain = (domain) => domain.replace(/^www\./, '');
13+
14+
chrome.storage.local.get(
15+
{ adBlockerEnabled: true, whitelist: [] },
16+
({ adBlockerEnabled, whitelist }) => {
17+
if (!adBlockerEnabled) return;
18+
19+
const currentSite = normalizeDomain(window.location.hostname);
20+
const normalizedWhitelist = whitelist.map(normalizeDomain);
21+
22+
if (normalizedWhitelist.includes(currentSite)) {
23+
console.log(`Whitelist active: Ads are allowed on ${currentSite}`);
24+
return; // Skip ad blocking
25+
}
26+
27+
// Ad blocking logic
28+
const blockAds = () => {
29+
adSelectors.forEach((selector) => {
30+
const ads = document.querySelectorAll(selector);
31+
console.log(`Found ${ads.length} ads for selector: ${selector}`);
32+
ads.forEach((ad) => ad.remove());
33+
});
34+
};
35+
36+
blockAds(); // Initial blocking
37+
38+
// Observe dynamically loaded ads
39+
const observer = new MutationObserver(blockAds);
40+
observer.observe(document.body, { childList: true, subtree: true });
41+
},
42+
);
2.05 KB
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
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();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /