-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwaitless.js
150 lines (143 loc) · 5.21 KB
/
waitless.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function(global) {
const waitless = {
scripts: [],
functions: [],
isLibraryAvailable: isLibraryAvailable
};
// Lazy load iframes
function loadIframes() {
var iframeContainers = document.querySelectorAll('iframe[waitless]');
if (iframeContainers.length > 0) {
var observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0 // 100% = 1.0
};
var loadIframe = function(iframeEl) {
var iframeSrc = iframeEl.getAttribute('waitless');
try {
iframeEl.setAttribute('src', iframeSrc);
iframeEl.removeAttribute('waitless');
} catch (error) {
return;
}
};
var iframeObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
loadIframe(entry.target);
observer.unobserve(entry.target);
}
});
}, observerOptions);
iframeContainers.forEach(function(container) {
iframeObserver.observe(container);
});
waitless.triggerLoad = function() {
var iframeContainers = document.querySelectorAll('iframe[waitless]');
iframeContainers.forEach(function(container) {
loadIframe(container);
});
};
}
}
// Lazy load scripts
const interactionList = ['keydown', 'mousemove', 'wheel', 'touchmove', 'touchstart', 'touchend'];
let fallbackDelay = 10000;
let interactionDetected = false;
function loadScripts(scriptConfigs, globalCallback) {
removeEventListeners();
let loadedScripts = 0;
const totalScripts = scriptConfigs.length;
function scriptLoaded(callback) {
loadedScripts++;
if (loadedScripts === totalScripts && typeof globalCallback === 'function') {
globalCallback();
}
if (typeof callback === 'function') {
callback();
}
}
function scriptError(src, callback) {
const error = new Error(`Failed to load script: ${src}`);
loadedScripts++;
if (typeof callback === 'function') {
callback(error);
}
if (loadedScripts === totalScripts && typeof globalCallback === 'function') {
globalCallback(error);
}
}
scriptConfigs.forEach(function(config) {
const { src, location = 'body', callback } = config;
const script = document.createElement('script');
script.src = src;
let loaded = false;
script.onload = function() {
if (!loaded) {
loaded = true;
scriptLoaded(callback);
}
};
script.onerror = function() {
if (!loaded) {
loaded = true;
scriptError(src, callback);
}
};
if (location === 'head') {
document.head.appendChild(script);
} else if (location === 'body') {
document.body.appendChild(script);
}
});
const waitlessScripts = document.querySelectorAll('script[waitless]');
const urlRegex = /^(?:https?:\/\/)?(?:[a-zA-Z0-9-]+\.)?[a-zA-Z0-9-]+\.[a-zA-Z0-9]+(?:\/(?:[^\/.]+\/)*[^\/.]+\.[a-zA-Z0-9]+)?\/?$/;
waitlessScripts.forEach(script => {
const waitlessUrl = script.getAttribute('waitless');
if (urlRegex.test(waitlessUrl)) {
script.src = waitlessUrl;
script.removeAttribute('waitless');
} else {
console.error(`Invalid URL: ${waitlessUrl}`);
}
});
}
const scriptLoadTimeout = setTimeout(() => {
if (!interactionDetected) {
loadScripts(waitless.scripts, allScriptsReady);
}
}, fallbackDelay);
function loadScriptsOnFirstInteraction() {
if (!interactionDetected) {
loadScripts(waitless.scripts, allScriptsReady);
}
interactionDetected = true;
clearTimeout(scriptLoadTimeout);
}
function allScriptsReady() {
waitless.functions.forEach(func => {
if (typeof func === 'function') {
func();
}
});
}
function isLibraryAvailable(libraryName) {
return typeof window[libraryName] !== 'undefined';
}
function removeEventListeners() {
interactionList.forEach(event => {
document.removeEventListener(event, loadScriptsOnFirstInteraction);
});
}
// Load iframes
document.addEventListener('DOMContentLoaded', function() {
loadIframes();
});
// Load scripts
interactionList.forEach(event => {
document.addEventListener(event, loadScriptsOnFirstInteraction);
});
global.waitless = waitless;
console.log('waitless 1.0.6');
})(this);