Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/content/snippets.js

Issue 29836573: Issue 6809 - Implement hide-if-contains snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created July 23, 2018, 11:11 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/content/snippets.js
===================================================================
--- a/lib/content/snippets.js
+++ b/lib/content/snippets.js
@@ -222,8 +222,31 @@
return root;
}
});
}
exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains,
hideElement);
+
+/**
+ * Hides any HTML element if the text content of the element contains a given
+ * string.
+ *
+ * @param {string} search The string to look for in every HTML element.
+ * @param {string} selector The CSS selector that an HTML element must match
+ * for it to be hidden.
+ */
+function hideIfContains(search, selector = "*")
+{
+ new MutationObserver(() =>
+ {
+ for (let element of document.querySelectorAll(selector))
+ {
+ if (element.textContent.includes(search))
+ hideElement(element);
+ }
+ })
+ .observe(document, {childList: true, characterData: true, subtree: true});
+}
+
+exports["hide-if-contains"] = hideIfContains;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld