2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-12 15:19:16 +00:00
|
|
|
// loadScript(script: Array<Promise|string>|string): Promise
|
2017-08-05 16:49:25 +00:00
|
|
|
// eslint-disable-next-line no-var
|
2017-10-29 17:22:10 +00:00
|
|
|
var loadScript = (() => {
|
2017-08-05 16:49:25 +00:00
|
|
|
const cache = new Map();
|
|
|
|
|
2017-09-12 15:19:16 +00:00
|
|
|
function inject(file) {
|
|
|
|
if (!cache.has(file)) {
|
|
|
|
cache.set(file, doInject(file));
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
2017-09-12 15:19:16 +00:00
|
|
|
return cache.get(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doInject(file) {
|
2017-08-05 16:49:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-12 15:19:16 +00:00
|
|
|
let el;
|
|
|
|
if (file.endsWith('.js')) {
|
|
|
|
el = document.createElement('script');
|
|
|
|
el.src = file;
|
|
|
|
} else {
|
|
|
|
el = document.createElement('link');
|
|
|
|
el.rel = 'stylesheet';
|
|
|
|
el.href = file;
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
2017-09-12 15:19:16 +00:00
|
|
|
el.onload = () => {
|
|
|
|
el.onload = null;
|
|
|
|
el.onerror = null;
|
2018-10-10 09:40:07 +00:00
|
|
|
resolve(el);
|
2017-08-05 16:49:25 +00:00
|
|
|
};
|
2017-09-12 15:19:16 +00:00
|
|
|
el.onerror = () => {
|
|
|
|
el.onload = null;
|
|
|
|
el.onerror = null;
|
|
|
|
reject(new Error(`Failed to load script: ${file}`));
|
2017-08-05 16:49:25 +00:00
|
|
|
};
|
2017-09-12 15:19:16 +00:00
|
|
|
document.head.appendChild(el);
|
2017-08-05 16:49:25 +00:00
|
|
|
});
|
2017-09-12 15:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return files => {
|
2017-11-01 01:28:54 +00:00
|
|
|
if (!Array.isArray(files)) {
|
2017-09-12 15:19:16 +00:00
|
|
|
files = [files];
|
|
|
|
}
|
|
|
|
return Promise.all(files.map(f => (typeof f === 'string' ? inject(f) : f)));
|
2017-08-05 16:49:25 +00:00
|
|
|
};
|
|
|
|
})();
|
2017-12-08 02:45:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
(() => {
|
|
|
|
let subscribers, observer;
|
|
|
|
// natively declared <script> elements in html can't have onload= attribute
|
|
|
|
// due to the default extension CSP that forbids inline code (and we don't want to relax it),
|
|
|
|
// so we're using MutationObserver to add onload event listener to the script element to be loaded
|
2017-12-08 18:55:08 +00:00
|
|
|
window.onDOMscriptReady = (srcSuffix, timeout = 1000) => {
|
2017-12-08 02:45:27 +00:00
|
|
|
if (!subscribers) {
|
|
|
|
subscribers = new Map();
|
|
|
|
observer = new MutationObserver(observe);
|
|
|
|
observer.observe(document.head, {childList: true});
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-08 18:55:08 +00:00
|
|
|
const listeners = subscribers.get(srcSuffix);
|
2017-12-08 02:45:27 +00:00
|
|
|
if (listeners) {
|
|
|
|
listeners.push(resolve);
|
|
|
|
} else {
|
2017-12-08 18:55:08 +00:00
|
|
|
subscribers.set(srcSuffix, [resolve]);
|
2017-12-08 02:45:27 +00:00
|
|
|
}
|
2017-12-08 18:55:08 +00:00
|
|
|
// a resolved Promise won't reject anymore
|
2018-10-10 08:14:51 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
emptyAfterCleanup(srcSuffix);
|
|
|
|
reject(new Error('Timeout'));
|
|
|
|
}, timeout);
|
2017-12-08 02:45:27 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
function observe(mutations) {
|
|
|
|
for (const {addedNodes} of mutations) {
|
|
|
|
for (const n of addedNodes) {
|
|
|
|
if (n.src && getSubscribersForSrc(n.src)) {
|
|
|
|
n.addEventListener('load', notifySubscribers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSubscribersForSrc(src) {
|
2017-12-08 18:55:08 +00:00
|
|
|
for (const [suffix, listeners] of subscribers.entries()) {
|
|
|
|
if (src.endsWith(suffix)) {
|
|
|
|
return {suffix, listeners};
|
2017-12-08 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifySubscribers(event) {
|
|
|
|
this.removeEventListener('load', notifySubscribers);
|
2017-12-08 18:55:08 +00:00
|
|
|
for (let data; (data = getSubscribersForSrc(this.src));) {
|
|
|
|
data.listeners.forEach(fn => fn(event));
|
|
|
|
if (emptyAfterCleanup(data.suffix)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyAfterCleanup(suffix) {
|
|
|
|
if (!subscribers) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
subscribers.delete(suffix);
|
2017-12-08 02:45:27 +00:00
|
|
|
if (!subscribers.size) {
|
|
|
|
observer.disconnect();
|
|
|
|
observer = null;
|
|
|
|
subscribers = null;
|
2017-12-08 18:55:08 +00:00
|
|
|
return true;
|
2017-12-08 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|