Index: include.preload.js |
=================================================================== |
--- a/include.preload.js |
+++ b/include.preload.js |
@@ -30,107 +30,83 @@ |
["audio", "MEDIA"], |
["video", "MEDIA"], |
["frame", "SUBDOCUMENT"], |
["iframe", "SUBDOCUMENT"], |
["object", "OBJECT"], |
["embed", "OBJECT"] |
]); |
-function getURLsFromObjectElement(element) |
+function* getURLsFromObjectParamChildren({children: [...children]}) |
{ |
- let url = element.getAttribute("data"); |
- if (url) |
- return [url]; |
- |
- for (let child of element.children) |
- { |
- if (child.localName != "param") |
- continue; |
+ const names = [ |
+ "movie", // Adobe Flash |
+ "source", // Silverlight |
+ "src", // Real Media + Quicktime |
+ "FileName" // Windows Media |
+ ]; |
- let name = child.getAttribute("name"); |
- if (name != "movie" && // Adobe Flash |
- name != "source" && // Silverlight |
- name != "src" && // Real Media + Quicktime |
- name != "FileName") // Windows Media |
- continue; |
+ let param = children.find( |
+ child => child.localName == "param" && |
+ names.includes(child.getAttribute("name")) && |
+ child.getAttribute("value") |
+ ); |
- let value = child.getAttribute("value"); |
- if (!value) |
- continue; |
- |
- return [value]; |
- } |
- |
- return []; |
+ if (param) |
+ yield param.getAttribute("value"); |
} |
-function getURLsFromAttributes(element) |
+function* getURLsFromObjectElement(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; |
+ let data = element.getAttribute("data"); |
+ if (data) |
+ yield data; |
+ else |
+ yield* getURLsFromObjectParamChildren(element); |
} |
-function getURLsFromMediaElement(element) |
+function* getURLsFromMediaSourceChildren({children: [...children]}) |
{ |
- let urls = getURLsFromAttributes(element); |
+ let sources = children.filter( |
+ ({localName}) => ["source", "track"].includes(localName) |
+ ); |
+ |
+ for (let source of sources) |
+ yield* getURLsFromAttributes(source); |
+} |
- for (let child of element.children) |
- { |
- if (child.localName == "source" || child.localName == "track") |
- urls.push(...getURLsFromAttributes(child)); |
- } |
+function* getURLsFromMediaElement(element) |
+{ |
+ yield* getURLsFromAttributes(element); |
+ yield* getURLsFromMediaSourceChildren(element); |
- if (element.poster) |
- urls.push(element.poster); |
+ yield element.poster; |
+} |
- return urls; |
+function* getURLsFromAttributes({src, srcset = ""}) |
+{ |
+ yield src; |
+ |
+ yield* srcset.split(",").map(url => url.trim().replace(/\s+\S+$/, "")); |
} |
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 [...urls].filter(url => url && !/^(?!https?:)[\w-]+:/i.test(url)); |
} |
function hideElement(element) |
{ |
function doHide() |
{ |
let propertyName = "display"; |
let propertyValue = "none"; |