有一个iframe具有以下结构,通过外部网站external.com
加载并在我的域example.com
托管:
<body> <!-- this is the body of my html from where I'm trying to make the reference -->
<iframe>
#document
<html>
<body>
<script type="text/html" id="icon-close"><div class="icon icon-close">
<div class="icon icon close"</div>
</script>
</body>
</html>
</iframe>
</body>
我特意尝试在这种情况下选择icon-close
元素。
我可以使用$("iframe")
选择iframe但是当我尝试访问检查器代码中的内容以使用$("iframe").contents()
选择元素时,我会收到以下内容:
DOMException: Failed to read the 'contentDocument' property from
'HTMLIFrameElement': Blocked a frame with origin
"https://external.com" from accessing a cross-origin frame.
我尝试在引用之前设置domain.document = 'example.com'
:
DOMException: Failed to set the 'domain' property on 'Document':
'example.com' is not a suffix of 'external.com'.
我也尝试将其设置为external.com
:
DOMException: Failed to read the 'contentDocument' property from
'HTMLIFrameElement': Blocked a frame with origin "http://example.com" from
accessing a cross-origin frame.
我见过相关帖子here和here,但不明白如何解决这个问题。
如何绕过相同的原始政策来访问iframe中的元素?