Chrome扩展程序 - 访问iframe元素

Nav*_*hat 7 javascript iframe google-chrome google-chrome-extension

我附加一个iframe使用的页面content scriptsrc设置为chrome.extension.getURL(myPage).稍后在某些事件中,我想从框架中检索一些元素.我尝试了以下代码content script:

var textFrame = document.getElementById('iframeId');
var text = (textFrame.contentDocument || textFrame.contentWindow.document).getElementById('someDivId');  
Run Code Online (Sandbox Code Playgroud)

但它会引发以下错误:

不安全的JavaScript尝试使用URL chrome-extension访问框架://ipkjfhkdgodpcgpjepdjhcbfcbbbcpee/TopBar.html来自URL为http://theInjectedPage.com/xxx/xxx/xxx的框架.域,协议和端口必须匹配.

manifest文件all_frames中设置为true.

请帮我解决这个问题.


更新:这是我的清单文件的一部分:

 "permissions": [
   "tabs",
   "chrome://favicon/",
   "http://*/*", 
   "https://*/*"
 ],
 "background": {
    "scripts": ["Javascript/background.js"]
  },
 "content_scripts": [
  {
    "matches": ["http://*/*"],
    "js": ["Javascript/References/jquery-1.7.min.js","Javascript/content_script.js"],
    "run_at": "document_start",
    "all_frames": true
  }
 ],
  "web_accessible_resources": ["TopBar.html","Javascript/TopBar.js","Javascript/References/jquery-1.7.min.js"]
Run Code Online (Sandbox Code Playgroud)

Pro*_*nia 2

您的权限没有任何问题,但我只是想知道为什么您从 chrome 收到此错误,页面 TopBar.html 正在尝试访问框架?您是通过 content_script 创建 iframe 吗?你给 iframe 的 url 是什么?

您可以检查它在您打开 iframe 的每个页面中注入的代码示例。

清单.json

{
  "name":"example",
  "description": "",
  "version": "1.0",
  "manifest_version": 2,
  "permissions": [],
  "content_scripts": [
    {
      "all_frames": true,
      "matches": ["http://*/*","https://*/*"],
      "css": [],
      "js": ["content_script.js"]
    }]
}
Run Code Online (Sandbox Code Playgroud)

content_script.js

var iframe= document.createElement("iframe");
iframe.setAttribute("width", "100%");
iframe.setAttribute("height", "200px;");
iframe.setAttribute("src", "http://gamalshaban.me/blog");
document.body.appendChild(iframe);
Run Code Online (Sandbox Code Playgroud)

我还上传了这个示例扩展,您可以从此链接下载它