【发布时间】:2017-05-04 16:54:40
【问题描述】:
有没有办法访问 iframe 中的 DOM 元素而不会出现跨域错误?
基本上,我希望能够从我的扩展程序中单击 iframe 内的按钮来启动视频。
我真的不知道如何继续,我尝试了iframe.contentDocument,但我得到了跨域错误。
background.js:
chrome.tabs.executeScript({
"file": "script.js",
"allFrames" : true
});
script.js:
var iframe = document.getElementById("iframeId");
var button = iframe.contentDocument.getElementsById("buttonId");
我得到的错误:Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "https://website.com" from accessing a cross-origin frame.
谢谢
【问题讨论】:
-
请edit 成为主题的问题:包括一个完整 minimal reproducible example 重复问题。包括一个manifest.json,一些背景/内容/弹出脚本/HTML。寻求调试帮助的问题(“为什么这段代码不工作?”)必须包括:►期望的行为,►特定问题或错误和►必要的最短代码重现它在问题本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:“如何创建 minimal reproducible example”、What topics can I ask about here? 和 How to Ask。
-
@Makyen 你想让我在我的问题中添加什么?什么不清楚?
-
需要minimal reproducible example 的原因是我们想提供帮助。如果我们不必重新创建复制问题所需的任何代码,那么提供帮助会要容易得多。这是您已经拥有的代码。因此,请帮助我们为您提供帮助并提供一个完整 minimal reproducible example,以重复此类问题的问题。如果没有minimal reproducible example,甚至开始帮助您所需的努力量大大更高,这显着减少了愿意/能够帮助您的人数。即使我们付出了额外的努力,我们也必须猜测您可能遇到的问题的很大一部分。
-
@Makyen 我添加了一些代码,但我认为这不是很有用
标签: javascript google-chrome iframe google-chrome-extension