@@ -205,20 +205,41 @@ func renderIFrame(ctx *RenderContext, output io.Writer, iframeSandbox string) er
205205 // "allow-same-origin" should never be used, it leads to XSS attack, and it makes the JS in iframe can access parent window's config and CSRF token
206206 // when there is a strict CORS policy, the "onload" script can not read the loaded height at the moment.
207207 // TODO: when using dark theme, if the rendered content doesn't have proper style, the default text color is black, which is not easy to read
208- _ , err := io .WriteString (output , fmt .Sprintf (`
209- <iframe src="%s/%s/%s/render/%s/%s"
210- name="giteaExternalRender"
211- onload="try { this.height=giteaExternalRender.document.documentElement.scrollHeight; } catch(e) { this.style.height='80vh'; }"
208+ _ , err := io .WriteString (output ,
209+ `
210+ <script type='module' >
211+ window.addEventListener('message', (e) => {
212+ const el = document.getElementById('gitea-external-render');
213+ if (e.data && e.data.giteaIframeCmd === 'resize') {
214+ el.setAttribute('data-iframe-resized', 'true');
215+ el.style.height = e.data.height+'px';
216+ }
217+ });
218+ window.giteaExternalRenderOnload = (el) => {
219+ setTimeout(() => {
220+ if(el.getAttribute('data-iframe-resized')) return;
221+ try {
222+ el.height = el.document.documentElement.scrollHeight;
223+ } catch(e) {
224+ el.style.height = '80vh';
225+ }
226+ }, 100);
227+ };
228+ </script>
229+ ` + fmt .Sprintf (`
230+ <iframe id="gitea-external-render"
231+ src="%s/%s/%s/render/%s/%s"
212232width="100%%" height="0" scrolling="auto" frameborder="0" style="overflow: hidden"
233+ onload="giteaExternalRenderOnload(this)"
213234sandbox="%s"
214235></iframe>` ,
215- setting .AppSubURL ,
216- url .PathEscape (ctx .Metas ["user" ]),
217- url .PathEscape (ctx .Metas ["repo" ]),
218- ctx .Metas ["BranchNameSubURL" ],
219- url .PathEscape (ctx .RelativePath ),
220- html .EscapeString (iframeSandbox ),
221- ))
236+ setting .AppSubURL ,
237+ url .PathEscape (ctx .Metas ["user" ]),
238+ url .PathEscape (ctx .Metas ["repo" ]),
239+ ctx .Metas ["BranchNameSubURL" ],
240+ url .PathEscape (ctx .RelativePath ),
241+ html .EscapeString (iframeSandbox ),
242+ ))
222243 return err
223244}
224245
0 commit comments