简体   繁体   English

如何访问跨域 iframe 的原始 DOM?

[英]How do I access the raw DOM of a cross-domain iframe?

I am trying to access the iframe DOM elements through a Chrome browser extension with something like document.querySelector("iframe").contentWindow.document The iframe's source is cross-domain.我正在尝试通过具有类似document.querySelector("iframe").contentWindow.document的 Chrome 浏览器扩展程序访问 iframe DOM 元素 iframe 的源是跨域的。 I'm getting a permission error, which Google tells me is because browsers specifically prohibit this.我收到一个权限错误,谷歌告诉我这是因为浏览器明确禁止这样做。

However, I do have access to the raw DOM on my side from the inspector tool.但是,我确实可以通过检查器工具访问我这边的原始 DOM。 If I run the same command in the console after inspecting element, I'm able to access the exact inner-HTML element I need with no permission error using the same exact command.如果我在检查元素后在控制台中运行相同的命令,我可以使用相同的命令访问我需要的确切的内部 HTML 元素,而不会出现权限错误。 I think this is because I'm looking at the DOM rather than trying to grab it from the source, which is what I need, just inside the chrome extension instead and not the chrome console.我认为这是因为我正在查看 DOM 而不是试图从源代码中获取它,这是我需要的,只是在 chrome 扩展而不是 chrome 控制台中。 Is it possible to simply read the DOM of an inner-HTML element in an off-origin iframe?是否可以简单地读取异源 iframe 中的内部 HTML 元素的 DOM? Even if it's not through the chrome extension?即使不是通过 chrome 扩展?

At this point I'm thinking I'd have to edit the chrome browser itself.在这一点上,我想我必须编辑 chrome 浏览器本身。 Please help.请帮忙。

You can not do this on cross-domain iframes due to security restrictions in the browser.由于浏览器的安全限制,您不能在跨域 iframe 上执行此操作。 You only option is to get the other site to enable CORS for your site您唯一的选择是让其他站点为您的站点启用 CORS

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

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