Index: include.preload.js |
=================================================================== |
--- a/include.preload.js |
+++ b/include.preload.js |
@@ -30,107 +30,79 @@ |
["audio", "MEDIA"], |
["video", "MEDIA"], |
["frame", "SUBDOCUMENT"], |
["iframe", "SUBDOCUMENT"], |
["object", "OBJECT"], |
["embed", "OBJECT"] |
]); |
-function getURLsFromObjectElement(element) |
+function* getURLsFromObjectElement(element) |
{ |
- let url = element.getAttribute("data"); |
- if (url) |
- return [url]; |
+ const paramNames = [ |
+ "movie", // Adobe Flash |
+ "source", // Silverlight |
+ "src", // Real Media + Quicktime |
+ "FileName" // Windows Media |
+ ]; |
+ |
+ let data = element.getAttribute("data"); |
+ if (data) |
+ { |
+ yield data; |
+ return; |
+ } |
+ |
+ let param = [].find.call( |
+ element.children, |
+ child => child.localName == "param" && |
+ paramNames.includes(child.getAttribute("name")) && |
+ child.getAttribute("value") |
+ ); |
+ |
+ if (param) |
+ yield param.getAttribute("value"); |
+} |
+ |
+function* getURLsFromAttributes({src = "", srcset = ""}) |
+{ |
+ yield src; |
+ |
+ yield* srcset.split(",").map(url => url.trim().replace(/\s+\S+$/, "")); |
+} |
+ |
+function* getURLsFromMediaElement(element) |
+{ |
+ yield* getURLsFromAttributes(element); |
for (let child of element.children) |
{ |
- if (child.localName != "param") |
- continue; |
- |
- let name = child.getAttribute("name"); |
- if (name != "movie" && // Adobe Flash |
- name != "source" && // Silverlight |
- name != "src" && // Real Media + Quicktime |
- name != "FileName") // Windows Media |
- continue; |
- |
- let value = child.getAttribute("value"); |
- if (!value) |
- continue; |
- |
- return [value]; |
+ if (["source", "track"].includes(child.localName)) |
+ yield* getURLsFromAttributes(child); |
} |
- return []; |
-} |
- |
-function getURLsFromAttributes(element) |
-{ |
- let urls = []; |
- |
- if (element.src) |
- urls.push(element.src); |
- |
- if (element.srcset) |
- { |
- for (let candidate of element.srcset.split(",")) |
- { |
- let url = candidate.trim().replace(/\s+\S+$/, ""); |
- if (url) |
- urls.push(url); |
- } |
- } |
- |
- return urls; |
-} |
- |
-function getURLsFromMediaElement(element) |
-{ |
- let urls = getURLsFromAttributes(element); |
- |
- for (let child of element.children) |
- { |
- if (child.localName == "source" || child.localName == "track") |
- urls.push(...getURLsFromAttributes(child)); |
- } |
- |
- if (element.poster) |
- urls.push(element.poster); |
- |
- return urls; |
+ yield element.poster; |
} |
function getURLsFromElement(element) |
{ |
- let urls; |
- switch (element.localName) |
- { |
- case "object": |
- urls = getURLsFromObjectElement(element); |
- break; |
+ let urls = null; |
- case "video": |
- case "audio": |
- case "picture": |
- urls = getURLsFromMediaElement(element); |
- break; |
+ let {localName} = element; |
- default: |
- urls = getURLsFromAttributes(element); |
- break; |
- } |
+ if (localName == "object") |
+ urls = getURLsFromObjectElement(element); |
+ else if (["video", "audio", "picture"].includes(localName)) |
+ urls = getURLsFromMediaElement(element); |
+ else |
+ urls = getURLsFromAttributes(element); |
- for (let i = 0; i < urls.length; i++) |
- { |
- if (/^(?!https?:)[\w-]+:/i.test(urls[i])) |
- urls.splice(i--, 1); |
- } |
- |
- return urls; |
+ return Array.from(urls).filter( |
+ url => url && !/^(?!https?:)[\w-]+:/i.test(url) |
+ ); |
} |
function hideElement(element) |
{ |
function doHide() |
{ |
let propertyName = "display"; |
let propertyValue = "none"; |