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

Delta Between Two Patch Sets: lib/content/snippets.js

Issue 30024560: Issue 7450 - Implement hide-if-contains-visible-text snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Added some more CSS properties to check. Minor review comments addressed. Created April 27, 2019, 5:31 p.m.
Right Patch Set: Updated patch for real Created May 9, 2019, 12:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/browser/snippets.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 */ 419 */
420 function hideIfMatches(match, selector, searchSelector) 420 function hideIfMatches(match, selector, searchSelector)
421 { 421 {
422 if (searchSelector == null) 422 if (searchSelector == null)
423 searchSelector = selector; 423 searchSelector = selector;
424 424
425 let callback = () => 425 let callback = () =>
426 { 426 {
427 for (let element of document.querySelectorAll(searchSelector)) 427 for (let element of document.querySelectorAll(searchSelector))
428 { 428 {
429 if (match(element)) 429 let closest = element.closest(selector);
430 { 430 if (closest && match(element, closest))
431 let closest = element.closest(selector); 431 hideElement(closest);
432 if (closest)
433 hideElement(closest);
434 }
435 } 432 }
436 }; 433 };
437 new MutationObserver(callback) 434 new MutationObserver(callback)
438 .observe(document, {childList: true, characterData: true, subtree: true}); 435 .observe(document, {childList: true, characterData: true, subtree: true});
439 callback(); 436 callback();
440 } 437 }
441 438
442 /** 439 /**
443 * Hides any HTML element or one of its ancestors matching a CSS selector if 440 * Hides any HTML element or one of its ancestors matching a CSS selector if
444 * the text content of the element contains a given string. 441 * the text content of the element contains a given string.
(...skipping 29 matching lines...) Expand all
474 * for it to be hidden. 471 * for it to be hidden.
475 * @param {?string} [searchSelector] The CSS selector that an HTML element 472 * @param {?string} [searchSelector] The CSS selector that an HTML element
476 * containing the given string must match. Defaults to the value of the 473 * containing the given string must match. Defaults to the value of the
477 * <code>selector</code> argument. 474 * <code>selector</code> argument.
478 */ 475 */
479 function hideIfContainsVisibleText(search, selector, searchSelector = null) 476 function hideIfContainsVisibleText(search, selector, searchSelector = null)
480 { 477 {
481 /** 478 /**
482 * Determines if the text inside the element is visible. 479 * Determines if the text inside the element is visible.
483 * @param {Element} element The element we are checking. 480 * @param {Element} element The element we are checking.
484 * @param {?CSSStyleDeclaration} [style] The computed style of element. If 481 * @param {?CSSStyleDeclaration} style The computed style of element. If
485 * falsey it will be queried. 482 * falsey it will be queried.
486 * @returns {bool} Whether the text is visible. 483 * @returns {bool} Whether the text is visible.
487 */ 484 */
488 function isTextVisible(element, style) 485 function isTextVisible(element, style)
489 { 486 {
490 if (!style) 487 if (!style)
491 style = window.getComputedStyle(element); 488 style = window.getComputedStyle(element);
492 489
493 if (style.getPropertyValue("opacity") == "0") 490 if (style.getPropertyValue("opacity") == "0")
494 return false; 491 return false;
495 if (style.getPropertyValue("font-size") == "0px") 492 if (style.getPropertyValue("font-size") == "0px")
496 return false; 493 return false;
497 494
498 let color = style.getPropertyValue("color"); 495 let color = style.getPropertyValue("color");
499 // if color is transparent... 496 // if color is transparent...
500 if (color == "rgba(0, 0, 0, 0)") 497 if (color == "rgba(0, 0, 0, 0)")
501 return false; 498 return false;
502 if (style.getPropertyValue("background-color") == color) 499 if (style.getPropertyValue("background-color") == color)
503 return false; 500 return false;
504 501
505 return true; 502 return true;
506 } 503 }
507 504
508 /** 505 /**
506 * Check if an element is visible
507 * @param {Element} element The element to check visibility of.
508 * @param {?CSSStyleDeclaration} style The computed style of element. If
509 * falsey it will be queried.
510 * @param {?Element} closest The closest parent to reach.
511 * @return {bool} Whether the element is visible.
512 */
513 function isVisible(element, style, closest)
514 {
515 if (!style)
516 style = window.getComputedStyle(element);
517
518 if (style.getPropertyValue("display") == "none")
519 return false;
520 let visibility = style.getPropertyValue("visibility");
521 if (visibility == "hidden" || visibility == "collapse")
522 return false;
523
524 if (!closest || element == closest)
525 return true;
526
527 let parent = element.parentElement;
528 if (!parent)
529 return true;
530
531 return isVisible(parent, null, closest);
532 }
533
534 /**
509 * Returns the visible text content from an element and its descendants. 535 * Returns the visible text content from an element and its descendants.
510 * @param {Element} element The element whose visible text we want. 536 * @param {Element} element The element whose visible text we want.
537 * @param {Element} closest The closest parent to reach while checking
538 * for visibility.
511 * @returns {string} The text that is visible. 539 * @returns {string} The text that is visible.
512 */ 540 */
513 function getVisibleContent(element) 541 function getVisibleContent(element, closest)
514 { 542 {
515 let style = window.getComputedStyle(element); 543 let style = window.getComputedStyle(element);
516 if (style.getPropertyValue("display") == "none") 544 if (!isVisible(element, style, closest))
517 return "";
518 let visibility = style.getPropertyValue("visibility");
519 if (visibility == "hidden" || visibility == "collapse")
520 return ""; 545 return "";
521 546
522 let text = ""; 547 let text = "";
523 for (let node of element.childNodes) 548 for (let node of element.childNodes)
524 { 549 {
525 switch (node.nodeType) 550 switch (node.nodeType)
526 { 551 {
527 case Node.ELEMENT_NODE: 552 case Node.ELEMENT_NODE:
528 text += getVisibleContent(node); 553 text += getVisibleContent(node, element);
529 break; 554 break;
530 case Node.TEXT_NODE: 555 case Node.TEXT_NODE:
531 if (isTextVisible(element, style)) 556 if (isTextVisible(element, style))
532 text += node.nodeValue; 557 text += node.nodeValue;
533 break; 558 break;
534 } 559 }
535 } 560 }
536 return text; 561 return text;
537 } 562 }
538 563
539 let re = toRegExp(search); 564 let re = toRegExp(search);
540 565
541 hideIfMatches(element => re.test(getVisibleContent(element)), 566 hideIfMatches(
542 selector, searchSelector); 567 (element, closest) => re.test(getVisibleContent(element, closest)),
568 selector, searchSelector);
543 } 569 }
544 570
545 exports["hide-if-contains-visible-text"] = hideIfContainsVisibleText; 571 exports["hide-if-contains-visible-text"] = hideIfContainsVisibleText;
546 572
547 /** 573 /**
548 * Hides any HTML element or one of its ancestors matching a CSS selector if 574 * Hides any HTML element or one of its ancestors matching a CSS selector if
549 * the text content of the element contains a given string and, optionally, if 575 * the text content of the element contains a given string and, optionally, if
550 * the element's computed style contains a given string. 576 * the element's computed style contains a given string.
551 * 577 *
552 * @param {string} search The string to look for in HTML elements. If the 578 * @param {string} search The string to look for in HTML elements. If the
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 1020
995 args[0] = url.href; 1021 args[0] = url.href;
996 } 1022 }
997 1023
998 return fetch_.apply(this, args); 1024 return fetch_.apply(this, args);
999 }; 1025 };
1000 } 1026 }
1001 1027
1002 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter, 1028 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
1003 toRegExp, regexEscape); 1029 toRegExp, regexEscape);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld