-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from tajulafreen/Add_Blocker_Extension
50Projects-HTML-CSS-JavaScript : Ads blocker extension
- Loading branch information
Showing
9 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 }); | ||
}, | ||
); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "Ad Blocker", | ||
"version": "1.0", | ||
"description": "A simple ad blocker to remove advertisements from websites.", | ||
"permissions": ["activeTab", "storage"], | ||
"content_scripts": [ | ||
{ | ||
"matches": ["<all_urls>"], | ||
"js": ["content.js"] | ||
} | ||
], | ||
"browser_action": { | ||
"default_popup": "popup.html", | ||
"default_icon": { | ||
"16": "./icons/icon16.png", | ||
"48": "./icons/icon48.png", | ||
"128": "./icons/icon128.png" | ||
} | ||
}, | ||
"icons": { | ||
"16": "./icons/icon16.png", | ||
"48": "./icons/icon48.png", | ||
"128": "./icons/icon128.png" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 10px; | ||
width: 250px; | ||
} | ||
|
||
h1 { | ||
font-size: 1.5em; | ||
margin-bottom: 10px; | ||
} | ||
|
||
label { | ||
display: block; | ||
margin-bottom: 20px; | ||
} | ||
|
||
input { | ||
margin-right: 10px; | ||
} | ||
|
||
ul { | ||
list-style-type: none; | ||
padding: 0; | ||
} | ||
|
||
li { | ||
margin: 5px 0; | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
|
||
button { | ||
cursor: pointer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Ad Blocker</title> | ||
<link rel="stylesheet" href="popup.css"> | ||
</head> | ||
<body> | ||
<h1>Ad Blocker</h1> | ||
<label> | ||
<input type="checkbox" id="toggle-ad-blocker" checked> | ||
Enable Ad Blocker | ||
</label> | ||
<div> | ||
<h2>Whitelist</h2> | ||
<input type="text" id="whitelist-input" placeholder="Enter website URL"> | ||
<button id="add-to-whitelist">Add</button> | ||
<ul id="whitelist"></ul> | ||
</div> | ||
<p id="status"></p> | ||
<script src="popup.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const whitelistInput = document.getElementById('whitelist-input'); | ||
const addToWhitelist = document.getElementById('add-to-whitelist'); | ||
const whitelist = document.getElementById('whitelist'); | ||
let whitelistData = JSON.parse(localStorage.getItem('whitelist')) || []; | ||
|
||
// Load whitelist | ||
function loadWhitelist() { | ||
whitelist.innerHTML = ''; | ||
whitelistData.forEach((site) => { | ||
const li = document.createElement('li'); | ||
li.textContent = site; | ||
const removeBtn = document.createElement('button'); | ||
removeBtn.textContent = 'Remove'; | ||
removeBtn.addEventListener('click', () => { | ||
whitelistData = whitelistData.filter((item) => item !== site); | ||
localStorage.setItem('whitelist', JSON.stringify(whitelistData)); | ||
loadWhitelist(); | ||
}); | ||
li.appendChild(removeBtn); | ||
whitelist.appendChild(li); | ||
}); | ||
} | ||
|
||
addToWhitelist.addEventListener('click', () => { | ||
const site = whitelistInput.value.trim(); | ||
if (site && !whitelistData.includes(site)) { | ||
whitelistData.push(site); | ||
localStorage.setItem('whitelist', JSON.stringify(whitelistData)); | ||
whitelistInput.value = ''; | ||
loadWhitelist(); | ||
} | ||
}); | ||
|
||
loadWhitelist(); |