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

Unified Diff: include.preload.js

Issue 29670575: Issue 5899 - Use CSS attribute selectors for collapsing media elements (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Jan. 16, 2018, 10:28 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
===================================================================
--- a/include.preload.js
+++ b/include.preload.js
@@ -30,16 +30,18 @@
["audio", "MEDIA"],
["video", "MEDIA"],
["frame", "SUBDOCUMENT"],
["iframe", "SUBDOCUMENT"],
["object", "OBJECT"],
["embed", "OBJECT"]
]);
+let collapsedElements = new WeakMap();
Manish Jethani 2018/01/16 10:47:27 A weak map of collapsed elements to their original
+
function getURLsFromObjectElement(element)
{
let url = element.getAttribute("data");
if (url)
return [url];
for (let child of element.children)
{
@@ -94,17 +96,17 @@
}
if (element.poster)
urls.push(element.poster);
return urls;
}
-function getURLsFromElement(element)
+function getURLsFromElement(element, all)
{
let urls;
switch (element.localName)
{
case "object":
urls = getURLsFromObjectElement(element);
break;
@@ -114,25 +116,49 @@
urls = getURLsFromMediaElement(element);
break;
default:
urls = getURLsFromAttributes(element);
break;
}
+ if (all)
+ return urls;
+
for (let i = 0; i < urls.length; i++)
{
if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
urls.splice(i--, 1);
}
return urls;
}
+function collapseElement(element)
Manish Jethani 2018/01/16 10:47:27 This is basically what hideElement does, except it
+{
+ let value = element.style.getPropertyValue("display");
+ let priority = element.style.getPropertyPriority("display");
+ if (value != "none" || priority != "important")
+ {
+ element.style.setProperty("display", "none", "important");
+ collapsedElements.set(element, {value, priority});
+ }
+}
+
+function restoreCollapsedElement(element)
+{
+ let display = collapsedElements.get(element);
+ if (display)
Manish Jethani 2018/01/16 10:47:27 Restore the original display settings.
+ {
+ element.style.setProperty("display", display.value, display.priority);
+ collapsedElements.delete(element);
+ }
+}
+
function hideElement(element)
{
function doHide()
{
let propertyName = "display";
let propertyValue = "none";
if (element.localName == "frame")
{
@@ -156,31 +182,38 @@
}
function checkCollapse(element)
{
let mediatype = typeMap.get(element.localName);
if (!mediatype)
return;
- let urls = getURLsFromElement(element);
+ let urls = getURLsFromElement(element, mediatype == "MEDIA");
Manish Jethani 2018/01/16 10:47:27 We have to pass true here as the second argument,
if (urls.length == 0)
return;
browser.runtime.sendMessage(
{
type: "filters.collapse",
urls,
mediatype,
baseURL: document.location.href
},
collapse =>
{
- if (collapse)
+ if (mediatype == "MEDIA")
+ {
+ if (collapse)
Manish Jethani 2018/01/16 10:47:27 I'm distinguishing "collapsing" from "hiding" here
+ collapseElement(element);
+ else
+ restoreCollapsedElement(element);
+ }
+ else if (collapse)
{
hideElement(element);
}
}
);
}
function checkSitekey()
@@ -517,14 +550,21 @@
}, true);
document.addEventListener("load", event =>
{
let element = event.target;
if (/^i?frame$/.test(element.localName))
checkCollapse(element);
}, true);
+
+ document.addEventListener("loadstart", event =>
Manish Jethani 2018/01/16 10:47:27 The "loadstart" event is dispatched by VIDEO and A
+ {
+ let element = event.target;
+ if (typeMap.get(element.localName) == "MEDIA")
Manish Jethani 2018/01/16 10:47:27 We have to do this check because IMG and PICTURE e
+ checkCollapse(element);
+ }, true);
}
window.checkCollapse = checkCollapse;
window.elemhide = elemhide;
window.typeMap = typeMap;
window.getURLsFromElement = getURLsFromElement;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld