Index: lib/content/snippets.js |
=================================================================== |
--- a/lib/content/snippets.js |
+++ b/lib/content/snippets.js |
@@ -92,17 +92,17 @@ |
*/ |
function makeInjector(injectable, ...dependencies) |
{ |
return (...args) => injectCode(stringifyFunctionCall(injectable, ...args), |
dependencies); |
} |
/** |
- * Hides an HTML element by settings its <code>style</code> attribute to |
+ * Hides an HTML element by setting its <code>style</code> attribute to |
* <code>display: none !important</code>. |
* |
* @param {HTMLElement} element The HTML element to hide. |
*/ |
function hideElement(element) |
{ |
element.style.setProperty("display", "none", "important"); |
@@ -212,27 +212,19 @@ |
if (!host || !root) |
return; |
// If the shadow contains the given text, check if the host or one of its |
// ancestors matches the selector; if a matching element is found, hide |
// it. |
if (root.textContent.includes(search)) |
{ |
- let element = host; |
- |
- do |
- { |
- if (element.matches(selector)) |
- { |
- hideElement(element); |
- break; |
- } |
- } |
- while (element = element.parentElement); |
+ let closest = host.closest(selector); |
+ if (closest) |
+ hideElement(closest); |
} |
} |
Object.defineProperty(Element.prototype, "attachShadow", { |
value(...args) |
{ |
// Create the shadow root first. It doesn't matter if it's a closed |
// shadow root, we keep the reference in a weak map. |
@@ -256,31 +248,41 @@ |
} |
}); |
} |
exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains, |
hideElement); |
/** |
- * Hides any HTML element if the text content of the element contains a given |
- * string. |
+ * Hides any HTML element or one of its ancestors matching a CSS selector 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} search The string to look for in HTML elements. |
* @param {string} selector The CSS selector that an HTML element must match |
* for it to be hidden. |
+ * @param {string?} [searchSelector] The CSS selector that an HTML element |
+ * containing the given string must match. Defaults to the value of the |
+ * <code>selector</code> argument. |
*/ |
-function hideIfContains(search, selector = "*") |
+function hideIfContains(search, selector = "*", searchSelector = null) |
{ |
+ if (searchSelector == null) |
+ searchSelector = selector; |
+ |
new MutationObserver(() => |
{ |
- for (let element of document.querySelectorAll(selector)) |
+ for (let element of document.querySelectorAll(searchSelector)) |
{ |
if (element.textContent.includes(search)) |
- hideElement(element); |
+ { |
+ let closest = element.closest(selector); |
+ if (closest) |
+ hideElement(closest); |
+ } |
} |
}) |
.observe(document, {childList: true, characterData: true, subtree: true}); |
} |
exports["hide-if-contains"] = hideIfContains; |
/** |