-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (93 loc) · 3.26 KB
/
index.js
File metadata and controls
115 lines (93 loc) · 3.26 KB
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
/*global require, module, window*/
var Promise = require('kaerus-component-uP'),
Ajax = require('kaerus-component-ajax');
var global = window;
var cached = {};
function Script(file,options) {
var loaded = cached[file],
source, stamp = '',
script, head, child, promise;
options = options ? options : {};
if(!isNaN(options)) options = {timeout:options};
if(options.cache === undefined) options.cache = true;
if(options.cache && loaded) return loaded;
if(options.timeout === undefined) options.timeout = 5000;
// stamp to circumvent the browser cache
if(options.stamp === undefined||options.stamp === true){
var timestamp = Date.now();
if(file.indexOf('?') < 0 ) options.stamp = '?' + timestamp;
else options.stamp = '&' + timestamp;
}
else if(options.stamp === false) options.stamp = '';
if(options.async === undefined) options.async = true;
if(options.defer === undefined) options.defer = true;
if(!options.type) options.type = 'application/javascript';
source = file + options.stamp;
function onloaded(event) {
loaded.timeout(null);
loaded.fulfill(source);
// detach script from head
if(options.detach && head && script)
head.removeChild(script);
}
function onerror(event) {
event = event || window.event;
loaded.reject(event);
}
loaded = new Promise();
if(options.ajax){
loaded = Ajax.get(source, {
timeout:options.timeout,
accept:options.type
},
null,
loaded).then(function(code){
try {
return new Function('exports',code + ';return exports')(Object.create(null));
} catch(error) {
if(console.error) console.error(source,error);
else console.log(source, error);
}
});
}
else {
loaded.timeout(options.timeout);
head = document.getElementsByTagName("head")[0];
script = document.createElement("script");
script.src = source;
script.async = options.async;
script.defer = options.defer;
if(script.readyState) {
script.onreadystatechange = function(event) {
if(this.readyState === "loaded" ||
this.readyState === "complete") {
this.onreadystatechange = null;
onloaded(event);
}
};
} else {
script.onload = onloaded;
script.onerror = onerror;
}
head.appendChild(script);
}
if(options.cache) cached[file] = loaded;
return loaded;
}
var SCRIPT = /<script\b(.*)[^>]*>([\s\S]*?)<\/script>/gm;
Script.parse = function(html){
var script, src, type;
while ((script = SCRIPT.exec(html))) {
if((src = script[1].match(/src=\"(.+)\"/))) {
Script(src).done();
}
else {
type = script[1].match(/type=\"(.+)\"/);
if(script[2] && (!type || type[1] === 'text/javascript')) {
/* todo: consider injecting into head to get better traces */
global.eval(script[2]);
}
}
}
};
module.exports = Script;