forked from MiguelCastillo/load-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (122 loc) · 3.88 KB
/
index.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, factory) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS support
module.exports = factory();
} else if (typeof define === "function" && define.amd) {
// Do AMD support
define(["LoadJS"], factory);
} else {
// Do browser support
global.LoadJS = factory();
}
})(this, function () {
var cache = {};
var head = document.getElementsByTagName("head")[0] || document.documentElement;
function exec(options) {
if (typeof options === "undefined") {
return Promise.resolve();
}
if (typeof options === "string") {
options = {
url: options
};
}
var cacheId = options.id || options.url;
var cacheEntry = cache[cacheId];
if (cacheEntry) {
// console.log("load-js: cache hit", cacheId);
return cacheEntry;
}
else if (options.allowExternal !== false) {
var el = getScriptById(options.id) || getScriptByUrl(options.url);
if (el) {
var promise = Promise.resolve(el);
if (cacheId) {
cache[cacheId] = promise;
}
return promise;
}
}
if (!options.url && !options.text) {
throw new Error("load-js: must provide a url or text to load");
}
var pending = (options.url ? loadScript : runScript)(head, createScript(options));
if (cacheId && options.cache !== false) {
cache[cacheId] = pending;
}
return pending;
}
function runScript(head, script) {
head.appendChild(script);
return Promise.resolve(script);
}
function loadScript(head, script) {
return new Promise(function (resolve, reject) {
// Handle Script loading
var done = false;
// Attach handlers for all browsers.
//
// References:
// http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer
// http://stevesouders.com/efws/script-onload.php
// https://www.html5rocks.com/en/tutorials/speed/script-loading/
//
script.onload = script.onreadystatechange = function () {
if (!done && (!script.readyState || script.readyState === "loaded" || script.readyState === "complete")) {
done = true;
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
resolve(script);
}
};
script.onerror = reject;
head.appendChild(script);
});
}
function createScript(options) {
options.type = options.type || detectScriptType(options.url);
if (options.type == "text/css") {
var script = document.createElement("link");
script.rel = "stylesheet";
script.href = options.url;
} else {
var script = document.createElement("script");
script.charset = options.charset || "utf-8";
script.async = !!options.async;
if (options.url) {
script.src = options.url;
}
if (options.text) {
script.text = options.text;
}
}
script.type = options.type;
script.id = options.id || options.url;
script.loadJS = "watermark";
return script;
}
function detectScriptType(url) {
return url.includes('.css') ? "text/css" : "text/javascript";
}
function getScriptById(id) {
var script = id && document.getElementById(id);
if (script && script.loadJS !== "watermark") {
console.warn("load-js: duplicate script with id:", id);
return script;
}
}
function getScriptByUrl(url) {
var script = url &&
(document.querySelector("script[src='" + url + "']") ||
document.querySelector("link[href='" + url + "']"));
if (script && script.loadJS !== "watermark") {
console.warn("load-js: duplicate script with url:", url);
return script;
}
}
return function load(items) {
return items instanceof Array ?
Promise.all(items.map(exec)) :
exec(items);
}
});