-
Notifications
You must be signed in to change notification settings - Fork 25
/
OpenTag.js
366 lines (333 loc) · 10.1 KB
/
OpenTag.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var q = {};
q.html = {};
q.html.fileLoader = {};
/**
* Load a file at url, optionally calling functions before and after it
* is loaded
* @param url The url to load
* @param preLoadAction A function called before the url is loaded. If it
returns false or throws an exception it will
prevent the url from loading. Takes the url as
an argument.
* @param postLoadHandler A function called after the url is loaded.
* Takes the url as an argument.
*/
q.html.fileLoader.load = function (url, preLoadAction, postLoadHandler,
parentNode, async) {
var scriptEl, preLoadResult, loadError, oldOnError, doPostLoad;
doPostLoad = function () {
postLoadHandler(url, loadError);
if (oldOnError) {
window.onerror = oldOnError;
}
};
try {
if (preLoadAction) {
preLoadResult = preLoadAction(url);
}
} catch (e) {
preLoadResult = false;
} finally {
if (preLoadResult !== false) {
scriptEl = q.html.fileLoader.createScriptEl(url, async);
if (postLoadHandler) {
scriptEl.onload = doPostLoad;
scriptEl.onreadystatechange = function () {
if ((this.readyState === "complete") ||
(this.readyState === "loading")) {
setTimeout(doPostLoad, 1);
}
};
}
if (!parentNode) {
parentNode = window.document.getElementsByTagName("head")[0];
}
if (window.onerror) {
oldOnError = window.onerror;
}
window.onerror = function (reason, url, lineNumber) {
loadError = {
reason: reason,
url: url,
lineNumber: lineNumber
};
return true;
};
parentNode.appendChild(scriptEl);
}
}
};
q.html.fileLoader.createScriptEl = function (path, async, forceReload) {
var scriptEl = document.createElement("script");
scriptEl.type = "text/javascript";
scriptEl.src = q.html.fileLoader.tidyUrl(path) +
(forceReload ? ("?" + new Date().getTime()) : "");
if (async !== false) {
scriptEl.async = "true";
scriptEl.defer = "true";
} else {
scriptEl.async = "false";
if (scriptEl !== false) {
scriptEl.async = false;
}
scriptEl.defer = "false";
}
return scriptEl;
};
q.html.fileLoader.tidyUrl = function (path) {
if (path.substring(0, 5) === 'http:') {
return path;
}
if (path.substring(0, 6) === 'https:') {
return path;
}
return "//" + path;
};
q.html.GlobalEval = {};
q.html.GlobalEval.globalEval = function (src) {
if (window.execScript) {
window.execScript(src);
} else {
var fn = function () {
window["eval"].call(window, src);
};
fn();
}
};
q.html.HtmlInjector = {};
q.html.HtmlInjector.inject = function (el, injectStart, str, cb, parentNode) {
if (str.toLowerCase().indexOf("<script") >= 0) {
var i, ii, d, scriptsRaw, scripts, script, contents;
d = document.createElement("div");
d.innerHTML = str;
scriptsRaw = d.getElementsByTagName("script");
//Make copy of the raw array that is given by the browser, as the
//raw array changes as the dom changes.
scripts = [];
for (i = 0, ii = scriptsRaw.length; i < ii; i += 1) {
scripts.push(scriptsRaw[i]);
}
contents = [];
for (i = 0, ii = scripts.length; i < ii; i += 1) {
script = scripts[i];
if (script.src) {
contents.push({src: script.src});
} else {
contents.push({script: script.innerHTML});
}
//Note: this line changes the length of scriptsRaw.
script.parentNode.removeChild(script);
}
if (d.innerHTML) {
el.innerHTML = injectStart ? (d.innerHTML + el.innerHTML) :
(el.innerHTML + d.innerHTML);
}
q.html.HtmlInjector.loadScripts(contents, 0, cb, el);
//use document fragments if adding to multiple elements!
} else {
el.innerHTML = injectStart ? (str + el.innerHTML) : (el.innerHTML + str);
cb();
}
};
q.html.HtmlInjector.loadScripts = function (contents, i, cb, parentNode) {
var ii, c;
for (ii = contents.length; i < ii; i += 1) {
c = contents[i];
if (c.src) {
q.html.fileLoader.load(
c.src,
null,
q.html.HtmlInjector.loadScripts.apply(this,
[contents, i + 1, cb, parentNode]),
parentNode
);
break;
} else {
q.html.GlobalEval.globalEval(c.script);
}
}
if (cb && (i === ii)) {
cb();
}
};
//Url filters is a number of url filters which are matched in order of priority. 1 is the highest priority.
//If a url filter matches, then if it is an include filterType, add the scripts in script loaders to the scripts that will be loaded.
//If it in an exclude filterType, then remove any scripts from the list of scripts that are to be lodded, through a lower priority.
var urlFilters = [{
filterType: "1", //Matches QTag.FILTER_TYPE_INCLUDE/EXCLUDE
patternType: "1", //Matches QTag.ALL/SUBSTRING/REGEX/EXACT_MATCH
pattern: "*", //Pattern for pattern type.
priority: 1,
scriptLoaderKeys: ["1"] //Matches keys in scriptLoader
}],
scriptLoaders = {
"1": {
id: "1",
name: "Name of the script to load",
pre: "", //Javascript to run before the url is loaded
url: "", //A url to load javascript from
post: "", //Javascript to run after the url has loaded
html: "", //If no url is specified, you can load some html through here.
locationId: 1, //1 = HEAD, 2 = BODY, 3 = DIV
positionId: 1, //1 = BEGINNING, 2 = HEAD
locationDetail: "", //If specified 3 for locationId, then the id of the DIV to put the code in
async: "" //If a url, whether the script element should be loaded asynchronously
}
};
function QTag(urlFilters, scriptLoaders) {
var i, ii, qTagLoader;
QTag.qTagLoaders = QTag.getLoaders(urlFilters, scriptLoaders, document.URL);
QTag.loadersFinished = 0;
for (i = 0, ii = QTag.qTagLoaders.length; i < ii; i += 1) {
qTagLoader = QTag.qTagLoaders[i];
if (qTagLoader.url) {
q.html.fileLoader.load(
qTagLoader.url,
QTag.getTimerStarter(qTagLoader),
QTag.getTimerEnder(qTagLoader),
qTagLoader.parentNode,
qTagLoader.async
);
} else if (qTagLoader.html) {
QTag.waitCounts[qTagLoader.id] = 0;
QTag.injectHtml(qTagLoader);
}
}
}
QTag.ALL = "1";
QTag.SUBSTRING = "2";
QTag.REGEX = "3";
QTag.EXACT_MATCH = "4";
QTag.FILTER_TYPE_INCLUDE = "1";
QTag.FILTER_TYPE_EXCLUDE = "2";
/**
* @param urlFilter An array containing objects which have a pattern type and
* a filter type
*/
QTag.getLoaders = function (urlFilters, scriptLoaders, url) {
var i, ii, urlFilter, loaderKeysSet = {}, matchedFilters = [],
loaders = [];
if ((!urlFilters) || (!url)) {
return loaders;
}
for (i = 0, ii = urlFilters.length; i < ii; i += 1) {
urlFilter = urlFilters[i];
if (QTag.doesUrlFilterMatch(urlFilter, url)) {
matchedFilters.push(urlFilter);
}
}
matchedFilters.sort(function (a, b) {
return b.priority - a.priority;
});
for (i = 0, ii = matchedFilters.length; i < ii; i += 1) {
QTag.updateLoaders(matchedFilters[i], loaderKeysSet);
}
for (i in loaderKeysSet) {
if (loaderKeysSet.hasOwnProperty(i)) {
loaders.push(scriptLoaders[i]);
}
}
return loaders;
};
/**
* Checks to see if a url filter matches a url
*/
QTag.doesUrlFilterMatch = function (urlFilter, url) {
var matches = false;
switch (urlFilter.patternType) {
case QTag.EXACT_MATCH:
if (url.toLowerCase() === urlFilter.pattern.toLowerCase()) {
matches = true;
}
break;
case QTag.SUBSTRING:
if (url.toLowerCase().indexOf(urlFilter.pattern.toLowerCase()) >= 0) {
matches = true;
}
break;
case QTag.REGEX:
if (new RegExp(urlFilter.pattern).test(url)) {
matches = true;
}
break;
case QTag.ALL:
matches = true;
break;
}
return matches;
};
/**
* Update the loader key set with the given filter
*/
QTag.updateLoaders = function (urlFilter, loaderKeysSet) {
var i, ii, scriptLoaderKeys = urlFilter.scriptLoaderKeys;
if (urlFilter.filterType === QTag.FILTER_TYPE_INCLUDE) {
for (i = 0, ii = scriptLoaderKeys.length; i < ii; i += 1) {
if (scriptLoaderKeys.hasOwnProperty(i)) {
loaderKeysSet[scriptLoaderKeys[i]] = true;
}
}
} else if (urlFilter.filterType === QTag.FILTER_TYPE_EXCLUDE) {
for (i = 0, ii = scriptLoaderKeys.length; i < ii; i += 1) {
if (scriptLoaderKeys.hasOwnProperty(i)) {
delete loaderKeysSet[scriptLoaderKeys[i]];
}
}
}
};
QTag.waitCounts = {};
QTag.injectHtml = function (qTagLoader) {
var el;
if (QTag.waitCounts[qTagLoader.id] < 10) {
if (qTagLoader.locationId === 1) {
el = document.getElementsByTagName("head")[0];
}
if (qTagLoader.locationId === 2) {
el = document.body;
}
if (qTagLoader.locationId === 3) {
el = document.getElementById(qTagLoader.locationDetail);
}
if (el) {
QTag.getTimerStarter(qTagLoader)();
q.html.HtmlInjector.inject(el, qTagLoader.positionId === 1,
qTagLoader.html, QTag.getTimerEnder(qTagLoader));
} else {
QTag.waitCounts[qTagLoader.id] += 1;
setTimeout(function () {
QTag.injectHtml(qTagLoader);
}, 500);
}
}
};
QTag.getTimerStarter = function (qTagLoader) {
return QTag.createStatementEvaluator(qTagLoader.pre);
};
QTag.getTimerEnder = function (qTagLoader) {
return function (url, error) {
return QTag.createStatementEvaluator(qTagLoader.post)();
};
};
QTag.createStatementEvaluator = function (statement) {
if ((!!statement) && (statement.length > 0)) {
var fn, toRun = 'fn = function() {\n' +
'q.html.GlobalEval.globalEval(statement);\n' +
'QTag.incrementLoadCounter([]);\n' +
'};';
eval(toRun);
return fn;
} else {
return function () {
QTag.incrementLoadCounter([]);
};
}
};
QTag.incrementLoadCounter = function () {
QTag.loadersFinished += 1;
if (QTag.loadersFinished === QTag.qTagLoaders.length * 2) {
if (window.qTag_allLoaded) {
window.qTag_allLoaded();
}
}
};
var qTag = new QTag(urlFilters || [], scriptLoaders || {});