PageSourceSearch

https://beatscratch.io/app/MIDI.js/js/util/dom_request_script.js

js beatscratch.io collected 2026-10-02 01:57:27 UTC 5,577 bytes, 225 lines download raw bytes

1/*
2	-----------------------------------------------------------
3	dom.loadScript.js : 0.1.4 : 2014/02/12 : http://mudcu.be
4	-----------------------------------------------------------
5	Copyright 2011-2014 Mudcube. All rights reserved.
6	-----------------------------------------------------------
7	/// No verification
8	dom.loadScript.add("../js/jszip/jszip.js");
9	/// Strict loading order and verification.
10	dom.loadScript.add({
11		strictOrder: true,
12		urls: [
13			{
14				url: "../js/jszip/jszip.js",
15				verify: "JSZip",
16				onsuccess: function() {
17					console.log(1)
18				}
19			},
20			{ 
21				url: "../inc/downloadify/js/swfobject.js",
22				verify: "swfobject",
23				onsuccess: function() {
24					console.log(2)
25				}
26			}
27		],
28		onsuccess: function() {
29			console.log(3)
30		}
31	});
32	/// Just verification.
33	dom.loadScript.add({
34		url: "../js/jszip/jszip.js",
35		verify: "JSZip",
36		onsuccess: function() {
37			console.log(1)
38		}
39	});
40*/
41
42if (typeof(dom) === "undefined") var dom = {};
43
44(function() { "use strict";
45
46dom.loadScript = function() {
47	this.loaded = {};
48	this.loading = {};
49	return this;
50};
51
52dom.loadScript.prototype.add = function(config) {
53	var that = this;
54	if (typeof(config) === "string") {
55		config = { url: config };
56	}
57	var urls = config.urls;
58	if (typeof(urls) === "undefined") {
59		urls = [{ 
60			url: config.url, 
61			verify: config.verify
62		}];
63	}
64	/// adding the elements to the head
65	var doc = document.getElementsByTagName("head")[0];
66	/// 
67	var testElement = function(element, test) {
68		if (that.loaded[element.url]) return;
69		if (test && globalExists(test) === false) return;
70		that.loaded[element.url] = true;
71		//
72		if (that.loading[element.url]) that.loading[element.url]();
73		delete that.loading[element.url];
74		//
75		if (element.onsuccess) element.onsuccess();
76		if (typeof(getNext) !== "undefined") getNext();
77	};
78	///
79	var hasError = false;
80	var batchTest = [];
81	var addElement = function(element) {
82		if (typeof(element) === "string") {
83			element = {
84				url: element,
85				verify: config.verify
86			};
87		}
88		if (/([\w\d.\[\]\'\"])$/.test(element.verify)) { // check whether its a variable reference
89			var verify = element.test = element.verify;
90			if (typeof(verify) === "object") {
91				for (var n = 0; n < verify.length; n ++) {
92					batchTest.push(verify[n]);
93				}			
94			} else {
95				batchTest.push(verify);
96			}
97		}
98		if (that.loaded[element.url]) return;
99		var script = document.createElement("script");
100		script.onreadystatechange = function() {
101			if (this.readyState !== "loaded" && this.readyState !== "complete") return;
102			testElement(element);
103		};
104		script.onload = function() {
105			testElement(element);
106		};
107		script.onerror = function() {
108			hasError = true;
109			delete that.loading[element.url];
110			if (typeof(element.test) === "object") {
111				for (var key in element.test) {
112					removeTest(element.test[key]);
113				}			
114			} else {
115				removeTest(element.test);
116			}
117		};
118		script.setAttribute("type", "text/javascript");
119		script.setAttribute("src", element.url);
120		doc.appendChild(script);
121		that.loading[element.url] = function() {};
122	};
123	/// checking to see whether everything loaded properly
124	var removeTest = function(test) {
125		var ret = [];
126		for (var n = 0; n < batchTest.length; n ++) {
127			if (batchTest[n] === test) continue;
128			ret.push(batchTest[n]);
129		}
130		batchTest = ret;
131	};
132	var onLoad = function(element) {
133		if (element) {
134			testElement(element, element.test);
135		} else {
136			for (var n = 0; n < urls.length; n ++) {
137				testElement(urls[n], urls[n].test);
138			}
139		}
140		var istrue = true;
141		for (var n = 0; n < batchTest.length; n ++) {
142			if (globalExists(batchTest[n]) === false) {
143				istrue = false;
144			}
145		}
146		if (!config.strictOrder && istrue) { // finished loading all the requested scripts
147			if (hasError) {
148				if (config.error) {
149					config.error();
150				}
151			} else if (config.onsuccess) {
152				config.onsuccess();
153			}
154		} else { // keep calling back the function
155			setTimeout(function() { //- should get slower over time?
156				onLoad(element);
157			}, 10);
158		}
159	};
160	/// loading methods;  strict ordering or loose ordering
161	if (config.strictOrder) {
162		var ID = -1;
163		var getNext = function() {
164			ID ++;
165			if (!urls[ID]) { // all elements are loaded
166				if (hasError) {
167					if (config.error) {
168						config.error();
169					}
170				} else if (config.onsuccess) {
171					config.onsuccess();
172				}
173			} else { // loading new script
174				var element = urls[ID];
175				var url = element.url;
176				if (that.loading[url]) { // already loading from another call (attach to event)
177					that.loading[url] = function() {
178						if (element.onsuccess) element.onsuccess();
179						getNext();
180					}
181				} else if (!that.loaded[url]) { // create script element
182					addElement(element);
183					onLoad(element);
184				} else { // it's already been successfully loaded
185					getNext();
186				}
187			}
188		};
189		getNext();
190	} else { // loose ordering
191		for (var ID = 0; ID < urls.length; ID ++) {
192			addElement(urls[ID]);
193			onLoad(urls[ID]);
194		}
195	}
196};
197
198dom.loadScript = new dom.loadScript();
199
200var globalExists = function(path, root) {
201	try {
202		path = path.split('"').join('').split("'").join('').split(']').join('').split('[').join('.');
203		var parts = path.split(".");
204		var length = parts.length;
205		var object = root || window;
206		for (var n = 0; n < length; n ++) {
207			var key = parts[n];
208			if (object[key] == null) {
209				return false;
210			} else { //
211				object = object[key];
212			}
213		}
214		return true;
215	} catch(e) {
216		return false;
217	}
218};
219
220})();
221
222/// For NodeJS
223if (typeof (module) !== "undefined" && module.exports) {
224	module.exports = dom.loadScript;
225}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.