javascript - 无法通过 chrome 扩展与 iFrame 交互

标签 javascript jquery google-chrome iframe google-chrome-extension

我有一个包含 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,

引用此SO Answer-ways to circumvent Cross Domain .

关于javascript - 无法通过 chrome 扩展与 iFrame 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32585125/

相关文章:

javascript - 删除数组中的所有空间 javascript

javascript - 检查元素是否在 jquery 集合中

javascript - 由于 MIME 类型错误,Chrome 拒绝执行 AJAX 脚本

javascript - 表标签在 Chrome/Firefox 中禁用吗?

html - Uri 到 Skype : are not working in Chrome

javascript - StreetView 中标记上的 InfoWindows

javascript - 获取类的值

javascript - 如何干净地访问 jquery 对象的直接后代?

javascript - focusout jquery 中的无限循环,仅在 Chrome 中

javascript - 带有复选框的 jquery Datatables 行分组