我有一个包含 iFrame 的页面,该页面加载不同的网站,并且希望能够与 iFrame 的内容进行交互(单击、聚焦、选择、输入值等)。
到目前为止,我可以检查 iFrame 是否已加载,但是一旦尝试执行简单的操作(例如隐藏其中的元素),就不会发生任何事情。
list .json
{
"manifest_version": 2,
"name": "my Extension",
"version": "0.1",
"content_scripts" : [
{
"matches": [
"http://*.myPage.com/*",
"https://*.iframePage.com/*"
],
"js": ["lib/jquery-2.1.4.min.js", "content.js"]
}
]
}
内容.js
console.log('Page Loaded');
var myFrame = $('iframe');
myFrame.ready(function() {
console.log('Frame Loaded');
});
chrome 中还有一个警告,内容大致如下:
Uncaught SecurityError: Blocked a frame with origin "https://www.iframePage.com" from accessing a frame with origin "http://www.myPage.com". The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match.
不确定这是否与扩展问题有关。
最佳答案
这是一个跨源错误。
Cross-origin issues are about the communication between iframes. You can always embed any iframe but, if origin differ, iframes cannot interact with each other.
您无法访问 iframe 的内容。
The basis of Same Origin Policy is that the window can work in contexts of each other only if they are from same protocol://domain:port,
关于javascript - 无法通过 chrome 扩展与 iFrame 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32585125/