Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty divs that used to contain ads #34

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/debug/adblocker.debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/debug/bookmarklet.debug.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/release/adblocker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/release/bookmarklet.min.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 31 additions & 5 deletions src/adblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
}
return false;
}
function isContainerElem(/** @type {HTMLElement} */elem) /** @type {boolean} */ {
// .tagName returns UPPERCASE for some reason
return ["DIV", "SPAN"].includes(elem.tagName);
}
var rm = {
elem(elem) {
elem(/** @type {HTMLElement} */elem) {
if(!shouldIgnore(elem)) {
removedElems.add([elem, elem.parentElement]);
elem.remove()
}
},
list(elems) {
list(/** @type {HTMLElement[]} */elems) {
Array.from(elems).forEach(v => rm.elem(v))
},
cls(name) {
cls(/**@type {string} */name) {
rm.list(document.getElementsByClassName(name))
},
selector(selector) {
selector(/** @type {string} */selector) {
rm.list(document.querySelectorAll(selector))
},
func({func, selector=null}) {
Expand All @@ -35,7 +40,8 @@
}
}
};

var /** @type {Set<[HTMLElement, HTMLElement]>} */ removedElems = new Set;
var handledElems /** @type {Set<HTMLElement>} */ = new Set;
for (let [name, args] of Object.entries(what)) {
// don't try to use the 'ignore' property as a thing to block
if(name != 'ignore') {
Expand All @@ -44,6 +50,26 @@
}
}
}
for(let [elem, parent] of removedElems) {
if(handledElems.has(elem)) {
continue; // already handled
}
handledElems.add(elem);
if(!parent.isConnected) {
// (indirect) parent has been deleted so don't do anything here,
// instead go from the parent (which will also be in the Set)
continue;
}
if(!isContainerElem(parent)) {
continue; // parent might be an image or similar so don't delete
}
if(parent.hasChildNodes()) {
continue; // don't delete parent - info of other children would be lost
}
// no children, no info in self, so safe to delete
// NOTE: This will add `parent` to the end of removedElems (if not ignored) so will check again from the parent
rm.elem(parent);
}
})({
cls: ['adsbygoogle', 'mod_ad_container', 'brn-ads-box','gpt-ad','ad-box','top-ads-container', 'adthrive-ad'],
selector: ['[aria-label="advertisement"]', '[class*="-ad "], [class*="-ad-"], [class$="-ad"], [class^="ad-"], [class^="adthrive"]', ':is(div,iframe)[id^="google_ads_iframe_"]', '#aipPrerollContainer'],
Expand Down