Skip to content

Instantly share code, notes, and snippets.

@nemanjan00 nemanjan00/filter.js
Last active Dec 15, 2018

Embed
What would you like to do?
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
normalizer = (pattern) => {
result = {};
pattern.split("").forEach((character) => result[character] = true);
return result;
}
comparator = (pattern, toFind) => {
toFind = Object.keys(toFind);
found = [];
notFound = false;
toFind.forEach((element) => {
if(pattern[element] === undefined){
notFound = true;
}
});
if(!notFound && toFind.length == Object.keys(pattern).length){
return true;
} else {
return false;
}
};
toFind = normalizer("Sponsored");
divs = Array.from(document.querySelectorAll("span")).map((element) => element.parentElement).map((element) => element);
innerText = divs.map((element) => element.innerText.replaceAll(" ", "")).map(text => normalizer(text)).map(normalized => comparator(normalized, toFind));
divs.forEach((div, key) => {
if(innerText[key]){
div.style.display = "none";
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<span>Sp</span>
<span>Sp</span>
<span>on</span>
<span>s</span>
<span>s</span>
<span>or</span>
<span>or</span>
<span>ed</span>
<span>or</span>
</div>
<div>
<span>Spa</span>
<span>on</span>
<span>s</span>
<span>or</span>
<span>ed</span>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.