summaryrefslogtreecommitdiff
path: root/src/lib/include.js
diff options
context:
space:
mode:
authorhackademix2018-07-01 01:01:23 +0200
committerhackademix2018-07-01 01:01:23 +0200
commiteceae7187a6f0e9510bc1165f6977256b87f490f (patch)
treed943f1ec73c09efa70954dcedb55eac82a726148 /src/lib/include.js
downloadnoscript-eceae7187a6f0e9510bc1165f6977256b87f490f.tar.gz
noscript-eceae7187a6f0e9510bc1165f6977256b87f490f.tar.xz
noscript-eceae7187a6f0e9510bc1165f6977256b87f490f.zip
Initial commit starting at version 10.1.8.3rc4.
Diffstat (limited to 'src/lib/include.js')
-rw-r--r--src/lib/include.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/lib/include.js b/src/lib/include.js
new file mode 100644
index 0000000..896c6d5
--- /dev/null
+++ b/src/lib/include.js
@@ -0,0 +1,35 @@
+var include = (() =>
+{
+ let _inclusions = new Map();
+
+ function scriptLoader(src) {
+ let script = document.createElement("script");
+ script.src = src;
+ return script;
+ }
+
+ function styleLoader(src) {
+ let style = document.createElement("link");
+ style.rel = "stylesheet";
+ style.type = "text/css";
+ style.href = src;
+ return style;
+ }
+
+ return async function include(src) {
+ if (_inclusions.has(src)) return await _inclusions.get(src);
+ if (Array.isArray(src)) {
+ return await Promise.all(src.map(s => include(s)));
+ }
+ debug("Including", src);
+
+ let loading = new Promise((resolve, reject) => {
+ let inc = src.endsWith(".css") ? styleLoader(src) : scriptLoader(src);
+ inc.onload = () => resolve(inc);
+ inc.onerror = () => reject(new Error(`Failed to load ${src}`));
+ document.head.appendChild(inc);
+ });
+ _inclusions.set(src, loading);
+ return await (loading);
+ }
+})();