-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamicpolyfill.js
131 lines (122 loc) · 4.23 KB
/
dynamicpolyfill.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
const polyfillio = "https://polyfill.io/v3/polyfill.min.js?features=";
function dynamicPolyfill(tocheck, scriptToPolyfill, functionToRunonLoad) {
if(typeof Promise !== "undefined" || window.hasOwnProperty("Promise")) {
var polyfillReq = [];
if(Array.isArray(tocheck)) {
tocheck.forEach(
function(tocheck) {
polyfillReq.push(checking(tocheck));
}
);
} else {
polyfillReq.push(checking(tocheck));
}
polyfillReq = polyfillReq.filter(
function(p) {
return p !== undefined;
}
).join(",");
loadPolyfill(polyfillReq, scriptToPolyfill, functionToRunonLoad);
} else {
promiFill(tocheck, scriptToPolyfill, functionToRunonLoad);
}
}
function promiFill(tocheck, scriptToPolyfill, functionToRunonLoad) {
var promPolyfill = document.createElement("script");
promPolyfill.src = polyfillio + "Promise";
document.body.appendChild(promPolyfill);
promPolyfill.onerror = function(response) {
console.error("Polyfilling Promise failed", response);
};
promPolyfill.onload = function() {
dynamicPolyfill(tocheck, scriptToPolyfill, functionToRunonLoad);
};
}
function checking(check) {
var splitChars = ".";
if(window.hasOwnProperty(check)!== true || (typeof this.check !== 'function') !== true || (check in this) !== true) {
if (check.indexOf(splitChars) >= 1) {
var split = check.split(".");
var firstWord = (split[0] === "window" ? split[0] : window[split[0]]);
var lastWord = new Object(split[split.length - 1]);
if ((firstWord.prototype.hasOwnProperty(lastWord) !== true) && (firstWord.hasOwnProperty(lastWord) !== true) && (lastWord in firstWord !== true)) {
return check;
}
} else {
return check;
}
}
}
function loadPolyfill(url, scriptToPolyfill, functionToRunonLoad) {
if(url !== "") {
var polyfill = document.createElement("script");
polyfill.src = polyfillio + encodeURIComponent(url);
polyfill.onerror = function(response) {
console.error("Loading the polyfill(s) failed!", response);
};
polyfill.onload = function() {
loadMyScript(scriptToPolyfill, functionToRunonLoad);
};
document.body.appendChild(polyfill);
} else {
loadMyScript(scriptToPolyfill, functionToRunonLoad);
}
}
function loadMyScript(url, functionToRunonLoad) {
if(Array.isArray(url)) {
var promises = [];
url.forEach(
function(url) {
promises.push(nonblankURL(url));
}
);
Promise.all(promises).then(
function() {
initialiseMyScript(functionToRunonLoad);
}
).catch(function(error) {return error;});
} else if(!Array.isArray(url) && url !== null && url !== "") {
nonblankURL(url).then(
function() {
initialiseMyScript(functionToRunonLoad);
}
).catch(function(error) {return error;});
} else {
initialiseMyScript(functionToRunonLoad);
}
}
function nonblankURL(uri) {
return new Promise(
function(resolve, reject) {
var thescript = document.createElement("script");
thescript.src = encodeURI(uri);
thescript.onerror = function(response) {
return reject("Loading the script failed!", response);
};
thescript.onload = function() {
return resolve(uri);
};
document.body.appendChild(thescript);
}
);
}
function initialiseMyScript(functionToRunonLoad) {
if(Array.isArray(functionToRunonLoad)) {
functionToRunonLoad.forEach(
function(functionToRunonLoad) {
initScript(functionToRunonLoad);
}
);
} else {
initScript(functionToRunonLoad);
}
function initScript(fn) {
try {
var run = new Function(fn);
run();
} catch(err) {
console.error("There was an error: ", err, err.name, err.stack);
}
}
}
module.exports = dynamicPolyfill;