htmlpreview / htmlpreview.github.com

HTML Preview for GitHub Repositories
htmlpreview.github.com
1.53k stars 307 forks source link

addEventListener("DOMContentLoaded", ...) never runs? #87

Closed jason-s closed 3 years ago

jason-s commented 4 years ago

https://htmlpreview.github.io/?https://github.com/jason-s/embedded-blog-public/blob/master/demos/gravity1/gravity1.html

I don't get DOMContentLoaded event to fire for some reason.

niutech commented 3 years ago

This is because the scripts are being fetched and run after DOMContentLoaded event, so the solution is to use load event instead or check if DOMContentLoaded event has been fired:

if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', main); else main();
jason-s commented 3 years ago

Nope, then the solution is to use another service then.

This is a bug.

Rawgit.org works fine: https://ghcdn.rawgit.org/jason-s/embedded-blog-public/master/demos/gravity1/gravity1.html

niutech commented 3 years ago

Rawgit is a third-party proxy server and could theoretically shut down at any time, while HTMLPreview is a client-side project without third-party servers (with an exception of external JS/CSS scripts, which are being proxied due to CORS). Because of this, the scripts are deferred and run past the DOMContentLoaded event. I can experiment with manually dispatch DOMContentLoaded event, but I am afraid this could have side effects on existing scripts (double dispatch).

max-hk commented 3 years ago

How about running all functions returned from getEventListener(document).DOMContentLoaded?

niutech commented 3 years ago

@maxloh Do you mean getEventListeners()? It's only in Chrome DevTools. But I can manually dispatch DOMContentLoaded event like this: document.dispatchEvent(new Event('DOMContentLoaded', {bubbles: true, cancelable: true}));, although it may have side effects, which I wrote above.