wait for just created script elements to load

fixes #1233
This commit is contained in:
tophf 2021-04-24 16:37:30 +03:00
parent b0a03d53fc
commit be4fd17113

View File

@ -69,6 +69,7 @@
//#region for our extension pages //#region for our extension pages
const reqPromise = {};
window.require = async function require(urls, cb) { window.require = async function require(urls, cb) {
const promises = []; const promises = [];
const all = []; const all = [];
@ -83,13 +84,14 @@
if (!el) { if (!el) {
el = document.createElement(tag); el = document.createElement(tag);
toLoad.push(el); toLoad.push(el);
promises.push(new Promise((resolve, reject) => { reqPromise[url] = new Promise((resolve, reject) => {
el.onload = resolve; el.onload = resolve;
el.onerror = reject; el.onerror = reject;
el[attr] = url; el[attr] = url;
if (isCss) el.rel = 'stylesheet'; if (isCss) el.rel = 'stylesheet';
}).catch(console.warn)); }).catch(console.warn);
} }
promises.push(reqPromise[url]);
all.push(el); all.push(el);
} }
if (toLoad.length) document.head.append(...toLoad); if (toLoad.length) document.head.append(...toLoad);