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

Unified Diff: include.preload.js

Issue 4734012065054720: Issue 2634 - Consider alternative URLs for element collapsing (Closed)
Patch Set: Consider all URLs for element collapsing Created June 7, 2015, 3:14 p.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 | « include.postload.js ('k') | lib/filterComposer.js » ('j') | 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
@@ -29,21 +29,118 @@
"embed": "OBJECT"
};
+function getURLsFromObjectElement(element)
+{
+ var url = element.getAttribute("data");
+ if (url)
+ return [url];
+
+ for (var i = 0; i < element.children.length; i++)
+ {
+ var child = element.children[i];
+ if (child.localName != "param")
+ continue;
+
+ var name = child.getAttribute("name");
+ if (name != "movie" && // Adobe Flash
+ name != "source" && // Silverlight
+ name != "src" && // Real Media + Quicktime
+ name != "FileName") // Windows Media
+ continue;
+
+ var value = child.getAttribute("value");
+ if (!value)
+ continue;
+
+ return [value];
+ }
+
+ return [];
+}
+
+function getURLsFromAttributes(element)
+{
+ var urls = [];
+
+ if (element.src)
+ urls.push(element.src);
+
+ if (element.srcset)
+ {
+ var candidates = element.srcset.split(",");
+ for (var i = 0; i < candidates.length; i++)
+ {
+ var url = candidates[i].trim().replace(/\s+\S+$/, "");
+ if (url)
+ urls.push(url);
+ }
+ }
+
+ return urls;
+}
+
+function getURLsFromMediaElement(element)
+{
+ var urls = getURLsFromAttributes(element);
+
+ for (var i = 0; i < element.children.length; i++)
+ {
+ var child = element.children[i];
+ if (child.localName == "source" || child.localName == "track")
+ urls.push.apply(urls, getURLsFromAttributes(child));
+ }
+
+ if (element.poster)
+ urls.push(element.poster);
+
+ return urls;
+}
+
+function getURLsFromElement(element)
+{
+ var urls;
+ switch (element.localName)
+ {
+ case "object":
+ urls = getURLsFromObjectElement(element);
+ break;
+
+ case "video":
+ case "audio":
+ case "picture":
+ urls = getURLsFromMediaElement(element);
+ break;
+
+ default:
+ urls = getURLsFromAttributes(element);
+ break;
+ }
+
+ for (var i = 0; i < urls.length; i++)
+ {
+ if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
+ urls.splice(i--, 1);
+ }
+
+ return urls;
+}
+
function checkCollapse(element)
{
var tag = element.localName;
if (tag in typeMap)
{
// This element failed loading, did we block it?
- var url = element.src;
- if (!url || !/^https?:/i.test(url))
+ var urls = getURLsFromElement(element);
+ if (urls.length == 0)
return;
ext.backgroundPage.sendMessage(
{
type: "should-collapse",
- url: url,
- mediatype: typeMap[tag]
+ urls: urls,
+ mediatype: typeMap[tag],
+ baseURL: document.location.href
},
function(response)
« no previous file with comments | « include.postload.js ('k') | lib/filterComposer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld