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: This is now a snippet Created April 5, 2019, 9:04 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/_utils.js » ('j') | no next file with comments »
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 887 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 898
899 args[0] = url.href; 899 args[0] = url.href;
900 } 900 }
901 901
902 return fetch_.apply(this, args); 902 return fetch_.apply(this, args);
903 }; 903 };
904 } 904 }
905 905
906 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter, 906 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
907 toRegExp, regexEscape); 907 toRegExp, regexEscape);
908
909 function hideIfContainsVisibleText(search, hideSelector, innerSelector = null)
910 {
911 /**
912 * Determine if the text inside the element is visible.
913 * @param {Element} element the leaf element we are checking.
914 * @param {?CSSStyle} style the computed style of element.
915 * @returns {bool} whether the text is visible.
916 */
917 function isTextVisible(element, style)
918 {
919 if (!style)
920 style = window.getComputedStyle(element);
921
922 if (style.getPropertyValue("opacity") == "0")
923 return false;
924 if (style.getPropertyValue("font-size") == "0px")
925 return false;
926 if (style.getPropertyValue("color") ==
927 style.getPropertyValue("background-color"))
928 return false;
929
930 return true;
931 }
932
933 /**
934 * Returns the visible text content from an element and its children.
935 * @param {Element} element the element whose visible text we want.
936 * @returns {String} the text that is visible.
937 */
938 function getVisibleContent(element)
939 {
940 let style = window.getComputedStyle(element);
941 if (style.getPropertyValue("display") == "none")
942 return "";
943 let visibility = style.getPropertyValue("visibility");
944 if (visibility == "hidden" || visibility == "collapse")
945 return "";
946
947 let text = "";
948 for (let node of element.childNodes)
949 {
950 switch (node.nodeType)
951 {
952 case Node.ELEMENT_NODE:
953 text += getVisibleContent(node);
954 break;
955 case Node.TEXT_NODE:
956 if (isTextVisible(element, style))
957 text += node.nodeValue;
958 break;
959 }
960 }
961 return text;
962 }
963
964 let re = toRegExp(search);
965
966 new MutationObserver(() =>
967 {
968 for (let element of document.querySelectorAll(hideSelector))
969 {
970 let inners =
971 innerSelector ? element.querySelectorAll(innerSelector) : [element];
972 for (let inner of inners)
973 {
974 let content = getVisibleContent(inner);
975 if (re.test(content))
976 hideElement(element);
977 }
978 }
979 })
980 .observe(document, {childList: true, characterData: true, subtree: true});
981 }
982
983 exports["hide-if-contains-visible-text"] =
984 makeInjector(hideIfContainsVisibleText, hideElement);
OLDNEW
« 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