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

Unified Diff: lib/content/snippets.js

Issue 30024560: Issue 7450 - Implement hide-if-contains-visible-text snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: This is now a snippet Created April 5, 2019, 9:04 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 | test/browser/_utils.js » ('j') | 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
@@ -900,8 +900,85 @@
}
return fetch_.apply(this, args);
};
}
exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
toRegExp, regexEscape);
+
+function hideIfContainsVisibleText(search, hideSelector, innerSelector = null)
+{
+ /**
+ * Determine if the text inside the element is visible.
+ * @param {Element} element the leaf element we are checking.
+ * @param {?CSSStyle} style the computed style of element.
+ * @returns {bool} whether the text is visible.
+ */
+ function isTextVisible(element, style)
+ {
+ if (!style)
+ style = window.getComputedStyle(element);
+
+ if (style.getPropertyValue("opacity") == "0")
+ return false;
+ if (style.getPropertyValue("font-size") == "0px")
+ return false;
+ if (style.getPropertyValue("color") ==
+ style.getPropertyValue("background-color"))
+ return false;
+
+ return true;
+ }
+
+ /**
+ * Returns the visible text content from an element and its children.
+ * @param {Element} element the element whose visible text we want.
+ * @returns {String} the text that is visible.
+ */
+ function getVisibleContent(element)
+ {
+ let style = window.getComputedStyle(element);
+ if (style.getPropertyValue("display") == "none")
+ return "";
+ let visibility = style.getPropertyValue("visibility");
+ if (visibility == "hidden" || visibility == "collapse")
+ return "";
+
+ let text = "";
+ for (let node of element.childNodes)
+ {
+ switch (node.nodeType)
+ {
+ case Node.ELEMENT_NODE:
+ text += getVisibleContent(node);
+ break;
+ case Node.TEXT_NODE:
+ if (isTextVisible(element, style))
+ text += node.nodeValue;
+ break;
+ }
+ }
+ return text;
+ }
+
+ let re = toRegExp(search);
+
+ new MutationObserver(() =>
+ {
+ for (let element of document.querySelectorAll(hideSelector))
+ {
+ let inners =
+ innerSelector ? element.querySelectorAll(innerSelector) : [element];
+ for (let inner of inners)
+ {
+ let content = getVisibleContent(inner);
+ if (re.test(content))
+ hideElement(element);
+ }
+ }
+ })
+ .observe(document, {childList: true, characterData: true, subtree: true});
+}
+
+exports["hide-if-contains-visible-text"] =
+ makeInjector(hideIfContainsVisibleText, hideElement);
« no previous file with comments | « no previous file | test/browser/_utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld