You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 25, 2022. It is now read-only.
functionimportModule(url){returnnewPromise((resolve,reject)=>{constscript=document.createElement("script");consttempGlobal="__tempModuleLoadingVariable"+Math.random().toString(32).substring(2);script.type="module";script.textContent=`import * as m from "${url}"; window.${tempGlobal} = m;`;script.onload=()=>{resolve(window[tempGlobal]);deletewindow[tempGlobal];script.remove();};script.onerror=()=>{reject(newError("Failed to load module script with URL "+url));deletewindow[tempGlobal];script.remove();};document.documentElement.appendChild(script);});}
This importModule function does not work, because a inline script without src attribute will never trigger load event.
The text was updated successfully, but these errors were encountered:
functionimportModule(url){// escape characters that are used to delimit the module URL.// this way the following module works: 'data:text/javascript,console.log("hello")'url=url.replace(/\\/g,'\\\\').replace(/"/g,'\\"');returnnewPromise((resolve,reject)=>{constscript=document.createElement("script");consttempGlobal="__tempModuleLoadingVariable"+Math.random().toString(32).substring(2);functioncleanup(){deletewindow[tempGlobal];script.remove();}window[tempGlobal]=function(module){cleanup();resolve(module);};script.type="module";script.textContent=`import * as m from "${url}"; window.${tempGlobal}(m);`;script.onerror=()=>{reject(newError("Failed to load module script with URL "+url));cleanup();};document.documentElement.appendChild(script);});}
Note that the returned promise does not reject when the module identifier is not valid (eg importModule('')), couldn't get that to work (it stays stuck in a pending state)
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
https://github.com/tc39/proposal-dynamic-import#using-host-specific-mechanisms
This
importModule
function does not work, because a inline script withoutsrc
attribute will never triggerload
event.The text was updated successfully, but these errors were encountered: