Better recognition of tracking pixels

pull/153/head
M66B 6 years ago
parent 20b1303f8c
commit 9eecb9824e

@ -66,6 +66,7 @@ import static androidx.core.text.HtmlCompat.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE;
public class HtmlHelper { public class HtmlHelper {
static final int PREVIEW_SIZE = 250; static final int PREVIEW_SIZE = 250;
private static final int TRACKING_PIXEL_SURFACE = 25;
private static final List<String> heads = Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6", "p", "table", "ol", "ul", "br", "hr"); private static final List<String> heads = Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6", "p", "table", "ol", "ul", "br", "hr");
private static final List<String> tails = Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li"); private static final List<String> tails = Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li");
@ -78,13 +79,9 @@ public class HtmlHelper {
Document document = Jsoup.parse(html); Document document = Jsoup.parse(html);
// Remove tracking pixels // Remove tracking pixels
for (Element img : document.select("img")) { for (Element img : document.select("img"))
String src = img.attr("src"); if (isTrackingPixel(img))
String height = img.attr("height").trim();
String width = img.attr("width").trim();
if ("1".equals(height) && "1".equals(width) && !TextUtils.isEmpty(src))
img.removeAttr("src"); img.removeAttr("src");
}
// Remove Javascript // Remove Javascript
for (Element e : document.select("*")) for (Element e : document.select("*"))
@ -186,13 +183,17 @@ public class HtmlHelper {
String src = img.attr("src"); String src = img.attr("src");
String alt = img.attr("alt"); String alt = img.attr("alt");
String title = img.attr("title"); String title = img.attr("title");
String height = img.attr("height").trim();
String width = img.attr("width").trim(); boolean tracking = isTrackingPixel(img);
Element div = document.createElement("div"); Element div = document.createElement("div");
Uri uri = Uri.parse(src); Uri uri = Uri.parse(src);
if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) { if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
// Remove link tracking pixel
if (tracking)
img.removeAttr("src");
boolean linked = false; boolean linked = false;
for (Element parent : img.parents()) for (Element parent : img.parents())
if ("a".equals(parent.tagName())) { if ("a".equals(parent.tagName())) {
@ -221,8 +222,8 @@ public class HtmlHelper {
div.appendElement("em").text(title); div.appendElement("em").text(title);
} }
// Tracking image // Tracking pixel
if ("1".equals(height) && "1".equals(width) && !TextUtils.isEmpty(src)) { if (tracking) {
div.appendElement("br"); div.appendElement("br");
div.appendElement("strong").text(context.getString(R.string.title_hint_tracking_image)); div.appendElement("strong").text(context.getString(R.string.title_hint_tracking_image));
} }
@ -513,6 +514,23 @@ public class HtmlHelper {
return sb.toString(); return sb.toString();
} }
static boolean isTrackingPixel(Element img) {
String src = img.attr("src");
String width = img.attr("width").trim();
String height = img.attr("height").trim();
if (TextUtils.isEmpty(src))
return false;
if (TextUtils.isEmpty(width) || TextUtils.isEmpty(height))
return false;
try {
return (Integer.parseInt(width) * Integer.parseInt(height) <= TRACKING_PIXEL_SURFACE);
} catch (NumberFormatException ignored) {
return false;
}
}
static void trimEnd(StringBuilder sb) { static void trimEnd(StringBuilder sb) {
int length = sb.length(); int length = sb.length();
while (length > 0 && sb.charAt(length - 1) == ' ') while (length > 0 && sb.charAt(length - 1) == ' ')

Loading…
Cancel
Save