Skip to content

Commit

Permalink
Merge pull request #34 from MarcellPerger1/remove-empty-divs
Browse files Browse the repository at this point in the history
Remove empty `div`s that used to contain ads
  • Loading branch information
MarcellPerger1 authored Feb 26, 2024
2 parents 510cb7d + e344443 commit 9685465
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
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

0 comments on commit 9685465

Please sign in to comment.