forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.noscript.js
87 lines (69 loc) · 2.44 KB
/
ls.noscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(function(window, factory) {
var globalInstall = function(){
factory(window.lazySizes);
window.removeEventListener('lazyunveilread', globalInstall, true);
};
factory = factory.bind(null, window, window.document);
if(typeof module == 'object' && module.exports){
factory(require('lazysizes'));
} else if(window.lazySizes) {
globalInstall();
} else {
window.addEventListener('lazyunveilread', globalInstall, true);
}
}(window, function(window, document, lazySizes) {
/*jshint eqnull:true */
'use strict';
var dummyParent = {nodeName: ''};
var supportPicture = !!window.HTMLPictureElement && ('sizes' in document.createElement('img'));
var config = (window.lazySizes && lazySizes.cfg) || window.lazySizesConfig;
var handleLoadingElements = function(e){
var i, isResponsive, hasTriggered, onload, loading;
var loadElements = e.target.querySelectorAll('img, iframe');
for(i = 0; i < loadElements.length; i++){
isResponsive = loadElements[i].getAttribute('srcset') || (loadElements[i].parentNode || dummyParent).nodeName.toLowerCase() == 'picture';
if(!supportPicture && isResponsive){
lazySizes.uP(loadElements[i]);
}
if(!loadElements[i].complete && (isResponsive || loadElements[i].src)){
e.detail.firesLoad = true;
if(!onload || !loading){
loading = 0;
/*jshint loopfunc:true */
onload = function(evt){
loading--;
if((!evt || loading < 1) && !hasTriggered){
hasTriggered = true;
e.detail.firesLoad = false;
lazySizes.fire(e.target, '_lazyloaded', {}, false, true);
}
if(evt && evt.target){
evt.target.removeEventListener('load', onload);
evt.target.removeEventListener('error', onload);
}
};
setTimeout(onload, 3500);
}
loading++;
loadElements[i].addEventListener('load', onload);
loadElements[i].addEventListener('error', onload);
}
}
};
if(!config){
config = {};
window.lazySizesConfig = config;
}
config.getNoscriptContent = function(noScript){
return noScript.textContent || noScript.innerText;
};
window.addEventListener('lazybeforeunveil', function(e){
if(e.detail.instance != lazySizes || e.defaultPrevented || e.target.getAttribute('data-noscript') == null){return;}
var noScript = e.target.querySelector('noscript, script[type*="html"]') || {};
var content = config.getNoscriptContent(noScript);
if(content){
e.target.innerHTML = content;
handleLoadingElements(e);
}
});
}));