* Load a script or stylesheet by adding it to the dom. * @param {string} fileName Path of the file we want to load. * @returns {Promise} resolved when the file is loaded
(fileName)
| 190 | * @returns {Promise} resolved when the file is loaded |
| 191 | */ |
| 192 | function loadFile (fileName) { |
| 193 | const extension = fileName.slice((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1); |
| 194 | let script, stylesheet; |
| 195 | |
| 196 | switch (extension.toLowerCase()) { |
| 197 | case "js": |
| 198 | return new Promise((resolve) => { |
| 199 | Log.log(`Load script: ${fileName}`); |
| 200 | script = document.createElement("script"); |
| 201 | script.type = "text/javascript"; |
| 202 | script.src = fileName; |
| 203 | script.onload = function () { |
| 204 | resolve(); |
| 205 | }; |
| 206 | script.onerror = function () { |
| 207 | Log.error("Error on loading script:", fileName); |
| 208 | script.remove(); |
| 209 | resolve(); |
| 210 | }; |
| 211 | document.getElementsByTagName("body")[0].appendChild(script); |
| 212 | }); |
| 213 | case "mjs": |
| 214 | return new Promise((resolve) => { |
| 215 | Log.log(`Load module script: ${fileName}`); |
| 216 | script = document.createElement("script"); |
| 217 | script.type = "module"; |
| 218 | script.src = fileName; |
| 219 | script.onload = function () { |
| 220 | resolve(); |
| 221 | }; |
| 222 | script.onerror = function () { |
| 223 | Log.error("Error on loading module script:", fileName); |
| 224 | script.remove(); |
| 225 | resolve(); |
| 226 | }; |
| 227 | document.getElementsByTagName("body")[0].appendChild(script); |
| 228 | }); |
| 229 | case "css": |
| 230 | return new Promise((resolve) => { |
| 231 | Log.log(`Load stylesheet: ${fileName}`); |
| 232 | |
| 233 | stylesheet = document.createElement("link"); |
| 234 | stylesheet.rel = "stylesheet"; |
| 235 | stylesheet.type = "text/css"; |
| 236 | stylesheet.href = fileName; |
| 237 | stylesheet.onload = function () { |
| 238 | resolve(); |
| 239 | }; |
| 240 | stylesheet.onerror = function () { |
| 241 | Log.error("Error on loading stylesheet:", fileName); |
| 242 | stylesheet.remove(); |
| 243 | resolve(); |
| 244 | }; |
| 245 | document.getElementsByTagName("head")[0].appendChild(stylesheet); |
| 246 | }); |
| 247 | } |
| 248 | } |
| 249 |
no outgoing calls
no test coverage detected