Jump to content

Grab HTML from iframe


ton10

Recommended Posts

A while back I created some code that made use of file_get_contents in order to grab HTML from a given URL and parse certain information from it. (In order to generate forum signatures.) Now, I need my code to be able to grab HTML from a page currently displayed in an iframe within the page on my site.

 

To describe my needs a little better, the user must log in to an off-site website in order to retrieve stats. Instead of me creating a database to store their log in information, I would like for them to manually log in to the off-site website through the iframe in my page. Then once the page with their stats is showing from within the same iframe, they can click a button that will allow the PHP (possibly javascript is also required) to grab the HTML from the page and create their forum signature.

 

Make sense? I don't normally ask for help because I like figuring out these things on my own, but I am slightly pressed for time due to other things going on.

 

I'm thinking the solution would have something to do with javascript grabbing the iframe's html content via the user's current cache of the page and then posting it along to the php for processing.

Link to comment
Share on other sites

You are not allowed to access the contents of an iframe that is not at the same domain as the parent page. It would be a HUGE security problem if you could

 

So you're saying that what I need to do is not possible? (At least through this method?)

Link to comment
Share on other sites

This is possible, give the iframe a name then use javascript to get the html.

here is an example

<html>
<head>
<script type="text/javascript">
function getText()
{
var x=document.getElementById("frame1").contentDocument;
alert(x.getElementsByTagName("h3")[0].childNodes[0].nodeValue);
}
</script>
</head>
<body>

<iframe src="frame_a.htm" id="frame1" ></iframe>
<br /><br />
<input type="button" onclick="getText()" value="Get text" />

</body>
</html>

Link to comment
Share on other sites

odpoa, that definitely works, but like I originally stated the data must be extracted from an off-site website. (Not the same domain.)

 

Like rhodesa said, I cannot do any sort of DOM cross-site scripting because it's not allowed due to security issues.

 

I think I have figured out how to get around all this however it may not be the best or straight forward method. Using jmarshall.com's CGIProxy (with SSL support) I should be able to allow the users to log in and access the off-site website through an iframe that points to my domain's proxy which serves the other website. Confusing huh?

 

I tested this theory by loading Google's frontpage in an iframe via CGIProxy and grabbing elements from it via javascript. It worked of course, since all relevant pages and data of course point to my domain. I can't test SSL support for log in screens, etc yet because of my home server limitations but I am pretty optimistic that once I get it implemented everything should run smoothly (short of actually programming the php portion).

 

What do you guys think? Will it work?

Link to comment
Share on other sites

Sorry for the double post, but I have made some progress in the first issue I had (which was cross-site scripting with a parent document and iframe loaded with a location on a different domain). My idea of implementing a cgi proxy pointed to the offsite page worked in allowing for cross-site scripting since it would be my site that now served the page.

 

However, now I'm back to my original question, with possibly more specifics. I need to grab the body HTML code from the page loaded inside the iframe. I know how to do this with javascript, but how can I pass it (iframe.contentWindow.document.body.innerHTML) from the javascript to PHP for processing? Passing it via POST would work good for me if that's possible.

Link to comment
Share on other sites

  • 4 years later...

Sorry for the double post, but I have made some progress in the first issue I had (which was cross-site scripting with a parent document and iframe loaded with a location on a different domain). My idea of implementing a cgi proxy pointed to the offsite page worked in allowing for cross-site scripting since it would be my site that now served the page.

 

However, now I'm back to my original question, with possibly more specifics. I need to grab the body HTML code from the page loaded inside the iframe. I know how to do this with javascript, but how can I pass it (iframe.contentWindow.document.body.innerHTML) from the javascript to PHP for processing? Passing it via POST would work good for me if that's possible.

I am looking into building a site using this exact same thought process. How did you get things working? could you give examples? Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.