简体   繁体   English

从不同域的父级读取和操作ifame的文档对象模型

[英]Read and manipulate ifame's document object model from the parent which is in different domain

I would like to implement a page which can display (embedded) an other page from a completely different domain, which is not under my control. 我想实现一个页面,该页面可以显示(嵌入)来自完全不同的域的另一个页面,该域不在我的控制之下。 (say www.google.com). (例如www.google.com)。 The end user fill a url input field in the parent page then the parent page shows the embedded content. 最终用户在父页面中填写url输入字段,然后父页面显示嵌入的内容。

This could be easy so far however, the parent page must have the ability to read and manipulate the embedded page's document. 到目前为止,这可能很容易,但是父页面必须具有读取和操作嵌入式页面文档的能力。 Nothing fancy just highlighting elements. 只是强调元素没什么花哨的。 I know this arises serious security concerns. 我知道这引起了严重的安全问题。 Some of the read data from the child page must be sent to my server (same domain as the parent page) 从子页面读取的某些数据必须发送到我的服务器(与父页面相同的域)

(Please note despite of the topic, this application would be completely legal commercial service, nothing to do with any gray or black trick or practice) (请注意,尽管有这个话题,但此应用程序将是完全合法的商业服务,与任何灰色或黑色技巧或做法无关)

  • Is it possible at all? 有可能吗?
  • Is it help if the end user installs an extension from me (written for some helper functionality to accomplish the task), and trusts me as the publisher? 如果最终用户安装了我的扩展程序(为完成任务而使用一些辅助功能编写)并信任我作为发布者,是否有帮助?
  • If none of the above helps, is there any way to accomplish the specified task than this iframe idea? 如果以上方法均无济于事,那么除了这个iframe创意外,还有什么方法可以完成指定的任务?

EDIT 编辑

(The app should be implemented for Chrome, FF and IE, I've just could not add more tags than 5..) (该应用程序应针对Chrome,FF和IE实施,我无法添加超过5个标签。)

One option would be moving DOM editing to the back end of the application. 一种选择是将DOM编辑移至应用程序的后端。

To elaborate, I did a project similar to this a year ago and we also wanted to be able to highlight portions of text on the page. 详细地说,我一年前做了一个与此类似的项目,我们还希望能够突出显示页面上的文本部分。 We loaded the HTML to put into the iframe in the back end, used a library that mimics jQuery (in our case, pyquery), and made our modifications to the DOM there. 我们将HTML加载到后端的iframe中,使用了一个模仿jQuery的库(在本例中为pyquery),并在那里对DOM进行了修改。 In our case, that was just adding spans around the text to highlight. 在我们的案例中,这只是在文本周围添加跨度以突出显示。

The problem with that approach is that you can't predict how any arbitrary website's DOM looks, so the application would be fairly fragile. 这种方法的问题在于您无法预测任意网站的DOM的外观,因此该应用程序将相当脆弱。 Even something as simple as dropping span tags around a given set of text could break the page. 甚至像在给定的一组文本周围添加span标签一样简单的操作也可能破坏页面。

It can be accomplished with a Chrome extension. 可以使用Chrome扩展程序来完成。 The basic idea would be to inject a content-script in both the parent page and embedded page, which will allow you to interact with the content of the page(s). 基本思想是在父页面和嵌入式页面中注入内容脚本 ,这将使您能够与页面内容进行交互。 You would then use a background-page to pass information between the two pages, via the chrome.runtime API. 然后,您将使用背景页面通过chrome.runtime API在两个页面之间传递信息。

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

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