简体   繁体   English

Web 应用程序在 iframe 中时无法读取它自己的 DOM 元素。 在 iframe 之外工作正常

[英]Web app can't read it's own DOM elements when in an iframe. Works fine outside of iframe

I have a heavily customized web app (made with Gradio) that needs to go into an iframe on another site.我有一个高度定制的 web 应用程序(使用 Gradio 制作),需要将 go 转换为另一个站点上的 iframe。

Website is hosted on Digital Ocean.网站托管在 Digital Ocean 上。 Gradio app is hosted on a local PC. Gradio 应用程序托管在本地 PC 上。 Gradio app is js/css/html. Gradio 应用程序是 js/css/html。

When I load the Gradio app directly in the browser, everything works as expected.当我直接在浏览器中加载 Gradio 应用程序时,一切都按预期工作。

If I put the app in an iframe (otherwise skeleton HTML/not other scripts loaded) it seems as though the Gradio app can no longer read it's own DOM elements via it's own js.如果我将应用程序放在 iframe 中(否则骨架 HTML/未加载其他脚本),似乎 Gradio 应用程序无法再通过它自己的 js 读取它自己的 DOM 元素。

<iframe src="https://12894.gradio.app" name="frame2" id="frame2" frameborder="0" marginwidth="0" marginheight="0"></iframe>

"TypeError: Cannot read properties of null (reading 'getElementById')" and such. “TypeError:无法读取 null 的属性(读取 'getElementById')”等。

I'm NOT trying to read or change elements inside the iframe with anything outside of the iframe or vice/versa.我不会尝试使用 iframe 之外的任何内容来读取或更改 iframe 内部的元素,反之亦然。

My understanding was that when loading a page in an iframe, it should be able to read/modify it's own elements with it's own scripts.我的理解是,在 iframe 中加载页面时,它应该能够使用自己的脚本读取/修改自己的元素。 Am I mistaken?我弄错了吗?

Turns out, Gradio uses a shadow DOM that confuses things.事实证明,Gradio 使用了一个混淆事物的影子 DOM。 Just removed it from their code.刚刚从他们的代码中删除了它。 Runs fine now.现在运行良好。

function gradioApp(){
return document.getElementsByTagName('gradio-app')[0].shadowRoot;}

to

function gradioApp(){
return document;}

In case anyone else runs into this.以防其他人遇到这种情况。 Thanks for everyone's quick help!感谢大家的快速帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM