๐Ÿ“ฆ getify / A-Tale-Of-Three-Lists

๐Ÿ“„ fix-script-urls.js ยท 25 lines
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// Note: this code just hijacks any `node_modules` script URLs and
// remaps them to external https:// equivalents, if this page is
// loaded in an http/https instead of file context.

(function IIFE(){

	var scripts = document.getElementsByTagName("script"),
		curScript = scripts[scripts.length - 1], src,
		scriptURLs = curScript.getAttribute("data-srcs").split(/\s+/),
		remoteURLNeeded = /^https?:\/\//.test(document.location.href.toString());

	for (var i=0; i<scriptURLs.length; i++) {
		src = scriptURLs[i];
		if (src) {
			try {
				if (remoteURLNeeded && /\/node_modules\//.test(src)) {
					src = src.replace(/^.*\/node_modules\/([^\/]+)\//,"https://unpkg.com/$1@latest/");
				}
				document.write("<sc" + "ript src=\"" + src + "\"></scr" + "ipt>");
			} catch (err) {}
		}
	}

})();