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

Side by Side 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: Added some more CSS properties to check. Minor review comments addressed. Created April 27, 2019, 5:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/browser/snippets.js » ('j') | test/browser/snippets.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 } 401 }
402 }); 402 });
403 } 403 }
404 404
405 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains, 405 exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains,
406 toRegExp, regexEscape, 406 toRegExp, regexEscape,
407 hideElement); 407 hideElement);
408 408
409 /** 409 /**
410 * Hides any HTML element or one of its ancestors matching a CSS selector if 410 * Hides any HTML element or one of its ancestors matching a CSS selector if
411 * it matches the provided condition.
412 *
413 * @param {function} match The function that provides the matching condition.
414 * @param {string} selector The CSS selector that an HTML element must match
415 * for it to be hidden.
416 * @param {?string} [searchSelector] The CSS selector that an HTML element
417 * containing the given string must match. Defaults to the value of the
418 * <code>selector</code> argument.
419 */
420 function hideIfMatches(match, selector, searchSelector)
421 {
422 if (searchSelector == null)
423 searchSelector = selector;
424
425 let callback = () =>
426 {
427 for (let element of document.querySelectorAll(searchSelector))
428 {
429 if (match(element))
430 {
431 let closest = element.closest(selector);
432 if (closest)
433 hideElement(closest);
434 }
435 }
436 };
437 new MutationObserver(callback)
438 .observe(document, {childList: true, characterData: true, subtree: true});
439 callback();
440 }
441
442 /**
443 * Hides any HTML element or one of its ancestors matching a CSS selector if
411 * the text content of the element contains a given string. 444 * the text content of the element contains a given string.
412 * 445 *
413 * @param {string} search The string to look for in HTML elements. If the 446 * @param {string} search The string to look for in HTML elements. If the
414 * string begins and ends with a slash (<code>/</code>), the text in between 447 * string begins and ends with a slash (<code>/</code>), the text in between
415 * is treated as a regular expression. 448 * is treated as a regular expression.
416 * @param {string} selector The CSS selector that an HTML element must match 449 * @param {string} selector The CSS selector that an HTML element must match
417 * for it to be hidden. 450 * for it to be hidden.
418 * @param {string?} [searchSelector] The CSS selector that an HTML element 451 * @param {?string} [searchSelector] The CSS selector that an HTML element
419 * containing the given string must match. Defaults to the value of the 452 * containing the given string must match. Defaults to the value of the
420 * <code>selector</code> argument. 453 * <code>selector</code> argument.
421 */ 454 */
422 function hideIfContains(search, selector = "*", searchSelector = null) 455 function hideIfContains(search, selector = "*", searchSelector = null)
423 { 456 {
424 if (searchSelector == null) 457 let re = toRegExp(search);
425 searchSelector = selector; 458
459 hideIfMatches(element => re.test(element.textContent),
460 selector, searchSelector);
461 }
462
463 exports["hide-if-contains"] = hideIfContains;
464
465 /**
466 * Hides any HTML element matching a CSS selector if the visible text content
467 * of the element contains a given string.
468 *
469 * @param {string} search The string to match to the visible text. Is considered
470 * visible text that isn't hidden by CSS properties or other means.
471 * If the string begins and ends with a slash (<code>/</code>), the
472 * text in between is treated as a regular expression.
473 * @param {string} selector The CSS selector that an HTML element must match
474 * for it to be hidden.
475 * @param {?string} [searchSelector] The CSS selector that an HTML element
476 * containing the given string must match. Defaults to the value of the
477 * <code>selector</code> argument.
478 */
479 function hideIfContainsVisibleText(search, selector, searchSelector = null)
480 {
481 /**
482 * Determines if the text inside the element is visible.
483 * @param {Element} element The element we are checking.
484 * @param {?CSSStyleDeclaration} [style] The computed style of element. If
485 * falsey it will be queried.
486 * @returns {bool} Whether the text is visible.
487 */
488 function isTextVisible(element, style)
489 {
490 if (!style)
491 style = window.getComputedStyle(element);
492
493 if (style.getPropertyValue("opacity") == "0")
494 return false;
495 if (style.getPropertyValue("font-size") == "0px")
496 return false;
497
498 let color = style.getPropertyValue("color");
499 // if color is transparent...
500 if (color == "rgba(0, 0, 0, 0)")
501 return false;
502 if (style.getPropertyValue("background-color") == color)
503 return false;
504
505 return true;
506 }
507
508 /**
509 * Returns the visible text content from an element and its descendants.
510 * @param {Element} element The element whose visible text we want.
511 * @returns {string} The text that is visible.
512 */
513 function getVisibleContent(element)
514 {
515 let style = window.getComputedStyle(element);
516 if (style.getPropertyValue("display") == "none")
517 return "";
518 let visibility = style.getPropertyValue("visibility");
519 if (visibility == "hidden" || visibility == "collapse")
520 return "";
521
522 let text = "";
523 for (let node of element.childNodes)
524 {
525 switch (node.nodeType)
526 {
527 case Node.ELEMENT_NODE:
528 text += getVisibleContent(node);
529 break;
530 case Node.TEXT_NODE:
531 if (isTextVisible(element, style))
532 text += node.nodeValue;
533 break;
534 }
535 }
536 return text;
537 }
426 538
427 let re = toRegExp(search); 539 let re = toRegExp(search);
428 540
429 new MutationObserver(() => 541 hideIfMatches(element => re.test(getVisibleContent(element)),
430 { 542 selector, searchSelector);
431 for (let element of document.querySelectorAll(searchSelector))
432 {
433 if (re.test(element.textContent))
434 {
435 let closest = element.closest(selector);
436 if (closest)
437 hideElement(closest);
438 }
439 }
440 })
441 .observe(document, {childList: true, characterData: true, subtree: true});
442 } 543 }
443 544
444 exports["hide-if-contains"] = hideIfContains; 545 exports["hide-if-contains-visible-text"] = hideIfContainsVisibleText;
445 546
446 /** 547 /**
447 * Hides any HTML element or one of its ancestors matching a CSS selector if 548 * Hides any HTML element or one of its ancestors matching a CSS selector if
448 * the text content of the element contains a given string and, optionally, if 549 * the text content of the element contains a given string and, optionally, if
449 * the element's computed style contains a given string. 550 * the element's computed style contains a given string.
450 * 551 *
451 * @param {string} search The string to look for in HTML elements. If the 552 * @param {string} search The string to look for in HTML elements. If the
452 * string begins and ends with a slash (<code>/</code>), the text in between 553 * string begins and ends with a slash (<code>/</code>), the text in between
453 * is treated as a regular expression. 554 * is treated as a regular expression.
454 * @param {string} selector The CSS selector that an HTML element must match 555 * @param {string} selector The CSS selector that an HTML element must match
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 994
894 args[0] = url.href; 995 args[0] = url.href;
895 } 996 }
896 997
897 return fetch_.apply(this, args); 998 return fetch_.apply(this, args);
898 }; 999 };
899 } 1000 }
900 1001
901 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter, 1002 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
902 toRegExp, regexEscape); 1003 toRegExp, regexEscape);
OLDNEW
« no previous file with comments | « no previous file | test/browser/snippets.js » ('j') | test/browser/snippets.js » ('J')

Powered by Google App Engine
This is Rietveld