1/* 2This plugin extends lazySizes to lazyLoad: 3background images, videos/posters and scripts 4 5Background-Image: 6For background images, use data-bg attribute: 7<div class="lazyload" data-bg="bg-img.jpg"></div> 8 9 Video: 10 For video/audio use data-poster and preload="none": 11 <video class="lazyload" preload="none" data-poster="poster.jpg" src="src.mp4"> 12 <!-- sources --> 13 </video> 14 15 For video that plays automatically if in view: 16 <video 17 class="lazyload" 18 preload="none" 19 muted="" 20 data-autoplay="" 21 data-poster="poster.jpg" 22 src="src.mp4"> 23</video> 24 25 Scripts: 26 For scripts use data-script: 27 <div class="lazyload" data-script="module-name.js"></div> 28 29 30 Script modules using require: 31 For modules using require use data-require: 32 <div class="lazyload" data-require="module-name"></div> 33*/ 34 35(function(window, factory) { 36 var globalInstall = function(){ 37 factory(window.lazySizes); 38 window.removeEventListener('lazyunveilread', globalInstall, true); 39 }; 40 41 factory = factory.bind(null, window, window.document); 42 43 if(typeof module == 'object' && module.exports){ 44 factory(require('lazysizes')); 45 } else if (typeof define == 'function' && define.amd) { 46 define(['lazysizes'], factory); 47 } else if(window.lazySizes) { 48 globalInstall(); 49 } else { 50 window.addEventListener('lazyunveilread', globalInstall, true); 51 } 52}(window, function(window, document, lazySizes) { 53 /*jshint eqnull:true */ 54 'use strict'; 55 var bgLoad, regBgUrlEscape; 56 var uniqueUrls = {}; 57 58 if(document.addEventListener){ 59 regBgUrlEscape = /\(|\)|\s|'/; 60 61 bgLoad = function (url, cb){ 62 var img = document.createElement('img'); 63 img.onload = function(){ 64 img.onload = null; 65 img.onerror = null; 66 img = null; 67 cb(); 68 }; 69 img.onerror = img.onload; 70 71 img.src = url; 72 73 if(img && img.complete && img.onload){ 74 img.onload(); 75 } 76 }; 77 78 addEventListener('lazybeforeunveil', function(e){ 79 if(e.detail.instance != lazySizes){return;} 80 81 var tmp, load, bg, poster; 82 if(!e.defaultPrevented) { 83 84 var target = e.target; 85 86 if(target.preload == 'none'){ 87 target.preload = target.getAttribute('data-preload') || 'auto'; 88 } 89 90 if (target.getAttribute('data-autoplay') != null) { 91 if (target.getAttribute('data-expand') && !target.autoplay) { 92 try { 93 target.play(); 94 } catch (er) {} 95 } else { 96 requestAnimationFrame(function () { 97 target.setAttribute('data-expand', '-10'); 98 lazySizes.aC(target, lazySizes.cfg.lazyClass); 99 }); 100 } 101 } 102 103 tmp = target.getAttribute('data-link'); 104 if(tmp){ 105 addStyleScript(tmp, true); 106 } 107 108 // handle data-script 109 tmp = target.getAttribute('data-script'); 110 if(tmp){ 111 e.detail.firesLoad = true; 112 load = function(){ 113 e.detail.firesLoad = false; 114 lazySizes.fire(target, '_lazyloaded', {}, true, true); 115 }; 116 addStyleScript(tmp, null, load); 117 } 118 119 // handle data-require 120 tmp = target.getAttribute('data-require'); 121 if(tmp){ 122 if(lazySizes.cfg.requireJs){ 123 lazySizes.cfg.requireJs([tmp]); 124 } else { 125 addStyleScript(tmp); 126 } 127 } 128 129 // handle data-bg 130 bg = target.getAttribute('data-bg'); 131 if (bg) { 132 e.detail.firesLoad = true; 133 load = function(){ 134 target.style.backgroundImage = 'url(' + (regBgUrlEscape.test(bg) ? JSON.stringify(bg) : bg ) + ')'; 135 e.detail.firesLoad = false; 136 lazySizes.fire(target, '_lazyloaded', {}, true, true); 137 }; 138 139 bgLoad(bg, load); 140 } 141 142 // handle data-poster 143 poster = target.getAttribute('data-poster'); 144 if(poster){ 145 e.detail.firesLoad = true; 146 load = function(){ 147 target.poster = poster; 148 e.detail.firesLoad = false; 149 lazySizes.fire(target, '_lazyloaded', {}, true, true); 150 }; 151 152 bgLoad(poster, load); 153 154 } 155 } 156 }, false); 157 158 } 159 160 function addStyleScript(src, style, cb){ 161 if(uniqueUrls[src]){ 162 return; 163 } 164 if(typeof lazySizes.cfg.safeDomains !== 'object' || ! Array.isArray(lazySizes.cfg.safeDomains)) { 165 return; 166 } 167 var safeDomains = lazySizes.cfg.safeDomains; 168 console.log(safeDomains); 169
170 var validSrc = false; 171 var i = 0; 172 for(; i < safeDomains.length; i++){ 173 console.log('checking if ' + src + ' matches ' + 'http(s)://' + safeDomains[i] + '/'); 174 if(src.startsWith('http://' + safeDomains[i] + '/')||src.startsWith('https://' + safeDomains[i] + '/')){ 175 console.log('src is valid'); 176 validSrc = true; 177 break; 178 } 179 } 180 if(!validSrc){ 181 return; 182 } 183 var elem = document.createElement(style ? 'link' : 'script'); 184 var insertElem = document.getElementsByTagName('script')[0]; 185 186 if(style){ 187 elem.rel = 'stylesheet'; 188 elem.href = src; 189 } else { 190 elem.onload = function(){ 191 elem.onerror = null; 192 elem.onload = null; 193 cb(); 194 }; 195 elem.onerror = elem.onload; 196 197 elem.src = src; 198 } 199 uniqueUrls[src] = true; 200 uniqueUrls[elem.src || elem.href] = true; 201 insertElem.parentNode.insertBefore(elem, insertElem); 202 } 203}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.