简体   繁体   English

Chrome扩展程序 - 访问iframe元素

[英]Chrome Extension - Access iframe Elements

I'm appending an iframe to a page using content script with src set to chrome.extension.getURL(myPage) . 我正在使用content scriptiframe附加到页面,并将src设置为chrome.extension.getURL(myPage) Later on some event, I want to retrieve some element from the frame. 稍后在某些事件中,我想从框架中检索一些元素。 I tried the following code in content script : 我在content script尝试了以下代码:

var textFrame = document.getElementById('iframeId');
var text = (textFrame.contentDocument || textFrame.contentWindow.document).getElementById('someDivId');  

but it throws the following error: 但它会引发以下错误:

Unsafe JavaScript attempt to access frame with URL chrome-extension://ipkjfhkdgodpcgpjepdjhcbfcbbbcpee/TopBar.html from frame with URL http://theInjectedPage.com/xxx/xxx/xxx . 不安全的JavaScript尝试使用URL chrome-extension访问框架://ipkjfhkdgodpcgpjepdjhcbfcbbbcpee/TopBar.html来自URL为http://theInjectedPage.com/xxx/xxx/xxx的框架。 Domains, protocols and ports must match. 域,协议和端口必须匹配。

In manifest file all_frames is set to true . manifest文件中, all_frames设置为true

Please help me to resolve this. 请帮我解决这个问题。


Update: Here is a part of my manifest file: 更新:这是我的清单文件的一部分:

 "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"]

There is nothing wrong with your permissions, but i'm just wondering why you got this error from chrome that the page TopBar.html is trying to access the frame ?? 您的权限没有任何问题,但我只是想知道为什么你从chrome得到这个错误,TopBar.html页面试图访问框架? are you creating the iframe via content_script? 你是通过content_script创建iframe的吗? and what is the url you are giving to the iframe ? 你给iframe的网址是什么?

you may check this code sample it injects in each page you are opening an iframe. 你可以检查它在你打开iframe的每个页面中注入的代码示例。

manifest.json 的manifest.json

{
  "name":"example",
  "description": "",
  "version": "1.0",
  "manifest_version": 2,
  "permissions": [],
  "content_scripts": [
    {
      "all_frames": true,
      "matches": ["http://*/*","https://*/*"],
      "css": [],
      "js": ["content_script.js"]
    }]
}

and the content_script.js 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);

also I've uploaded this sample extension and you can download it from this link 我也上传了这个示例扩展,你可以从这个链接下载它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM