-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlfills.js
66 lines (61 loc) · 2.19 KB
/
htmlfills.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
import {SelectorObserver} from 'https://cdn.jsdelivr.net/gh/u1ui/[email protected]/SelectorObserver.min.js'
const scripts = {};
const polyfills = {
dialog: {
supports: 'HTMLDialogElement' in window,
js: 'https://cdn.jsdelivr.net/gh/nuxodin/[email protected]/dialog.min.js',
},
"[focusgroup]": { // waiting for but to fix: https://github.com/MicrosoftEdge/MSEdgeExplainers/pull/581
supports: 'focusgroup' in document.head,
js: 'https://cdn.jsdelivr.net/gh/MicrosoftEdge/MSEdgeExplainers/Focusgroup/focusgroup_polyfill.js',
},
"[inert]": {
supports: Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'inert')?.enumerable === true, // hacky test, mod.js adds inert property support
js: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/inert.min.js',
//js: 'https://unpkg.com/[email protected]/dist/inert.min.js',
},
"search": {
supports: window.HTMLSearchElement,
js: (el)=> el.setAttribute('role', 'search'),
},
}
Object.keys(polyfills).forEach(selector => {
const data = polyfills[selector];
if (data.supports) return;
const obs = new SelectorObserver({
on: (el) => {
if (data.js instanceof Function) {
data.js(el);
}
else {
onScript(data.js, () => {
console.log('💊 lazyfill: "'+selector+'" polyfilled, you need the polyfill: '+data.js);
//data.onfound && data.onfound(el)
});
obs.disconnect();
}
},
})
obs.observe(selector);
});
function onScript(path, cb){
if (!scripts[path]) {
scripts[path] = {
callbacks:[cb]
};
loadScript(path, function(){
scripts[path].callbacks.forEach(cb);
scripts[path].loaded = true;;
});
}
if (scripts[path].loaded) cb();
else scripts[path].callbacks.push(cb);
}
function loadScript(path, cb, eb) {
const elem = document.createElement('script');
elem.async = false;
elem.src = path;
elem.onload = cb;
elem.onerror = eb;
document.documentElement.firstChild.appendChild(elem);
}