- Notifications
You must be signed in to change notification settings - Fork17
50Projects-HTML-CSS-JavaScript : Ads blocker extension#55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletionsREADME.md
This file contains hidden or 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
42 changes: 42 additions & 0 deletionsSource-Code/AdsBlockerExtension/content.js
This file contains hidden or 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 }); | ||
}, | ||
); |
Binary file addedSource-Code/AdsBlockerExtension/icons/icon128.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedSource-Code/AdsBlockerExtension/icons/icon16.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedSource-Code/AdsBlockerExtension/icons/icon48.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletionsSource-Code/AdsBlockerExtension/manifest.json
This file contains hidden or 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" | ||
} | ||
} | ||
34 changes: 34 additions & 0 deletionsSource-Code/AdsBlockerExtension/popup.css
This file contains hidden or 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; | ||
} |
24 changes: 24 additions & 0 deletionsSource-Code/AdsBlockerExtension/popup.html
This file contains hidden or 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> |
34 changes: 34 additions & 0 deletionsSource-Code/AdsBlockerExtension/popup.js
This file contains hidden or 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(); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.