fix: defer inline scripts in HTML preview so Chart.js and CDN scripts load before chart code runs (#1228)#1247
Conversation
|
@Wendong-Fan @bytecii @4pmtong @Pakchoioioi could you please review the PR? Best Regards |
| hasExternal = true; | ||
| break; | ||
| } | ||
| idx = lower.indexOf('<script', idx + 1); |
There was a problem hiding this comment.
Not all script tags are js scripts right? for example, type="module" and type="application/ld+json"
There was a problem hiding this comment.
Seems that some script attributes will be removed of the script such as crossorigin etc?
src/lib/htmlFontStyles.ts
Outdated
| const content = html.slice(contentStart, contentEnd); | ||
| if (!hasSrc && content.trim().length > 0) { | ||
| const escaped = content.replace(/<\/script>/gi, '<\\/script>'); | ||
| result += `<script>window.addEventListener('load',function(){${escaped}});</script>`; |
There was a problem hiding this comment.
Should we use DOMContentLoaded instead of load? Instead of waiting all things such as images?
…b.com/spider-yamet/eigent into fix/render-images-fail-render-in-HTML
|
@bytecii Let me summarize the update. 1. Not all script tags are JS |
bytecii
left a comment
There was a problem hiding this comment.
In general LGTM but it will be good if others can take a quick look at this @Wendong-Fan
|
@Wendong-Fan @bytecii Could you please check PR status? |
Summary
Fixes charts and script-driven visuals not rendering when opening HTML reports (e.g.
in_progress_priority_impact_report.html) in the file viewer. Inline scripts were running before external scripts (e.g. Chart.js from CDN), soChartwas undefined and canvas charts did not draw.Changes
src/lib/htmlFontStyles.ts: AddeddeferInlineScriptsUntilLoad(html). When the document contains at least one<script src="...">, it wraps inline<script>content inwindow.addEventListener('load', ...)so it runs after all resources (including CDN scripts) have loaded.src/components/Folder/index.tsx: UsesdeferInlineScriptsUntilLoadwhen building HTML for the iframe before settingsrcdoc, so chart-initialization code runs only after Chart.js is available.Testing
Closes #1228