Solved

Get URL of image displayed in IFRAME?

Posted on 2004-03-25
15
1,886 Views
Last Modified: 2007-12-19
Hi,

I asked this question a while ago and was surprised that nobody had an answer. I´ll try my luck again, this time with a slightly rephrased description of the problem. Hopefully somebody can point me in the right direction.

Many webpages position ads in IFRAMEs. Usually they display an image in the IFRAME. Left-clicking on it opens the linked page. Right-clicking allows either to save the image (Save Image as) or to view its properties. In the property window the full address of the image is revealed, for example:

https://img.web.de/_Muster/bookmark/credit.gif

MY QUESTION: IS IT POSSIBLE TO PROGRAMMATICALLY RETRIEVE THE SAME INFORMATION – THE ADDRESS OF THE IMAGE?

The source code of the webpage only contains an src to the document linked in the IFRAME but not the address of the image, for example:

    <IFRAME src="/iframe.ng/site=freemail&category=login&special=top&adsize=468x60&content=webde" width="640" height="70" scrolling="no" marginwidth="0" marginheight="0" frameborder="0">
    </IFRAME>

Accessing the content of the document linked in the IFRAME is not possible because of cross frame security.

Here is an example site: http://freemail.web.de
The second element from the top is an IFRAME, containing five different ads.


I fooled around with this, without much success:

Private Sub setEvent(doc As HTMLDocument)
   Dim sElement As IHTMLElement
   Dim testforError As String

   On Error Resume Next
'   MsgBox "setEvent " & doc.location.href

   For Each sElement In doc.All
      testforError = sElement.tagName
      If Err.Number = 0 Then
         With sElement
            If .tagName = "IMG" Or .tagName = "A" Then ….
            If sElement.tagName = "FRAME" Then ….
               
            '====================================================        
            'This approach only provides the src to the document linked in the IFRAME
            'How to get the address of the image displayed in the IFRAME? Obviously
            'Internet Explorer knows how to get it, since it shows it in the property window      
            If sElement.tagName = "IFRAME" Then
               For cnt = 0 To doc.All.length
                  If doc.All(cnt).sourceIndex = sElement.sourceIndex Then MsgBox doc.All(cnt).src
               Next
            End If
            '====================================================

            If Err.Number <> 0 Then
               Debug.Print Err.Description
               Err.Clear
            End If
         End With
      End If
   Next
End Sub

Thanks for your help!
0
Comment
Question by:_Mark_
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10681910
You are trying to script cross domain.  That is prevented by builtin security.  It is not going to return the value you want, because it is not allowed to access it cross domain.

Cd&
0
 
LVL 14

Expert Comment

by:dfu23
ID: 10681971
Ok ... so in your subroutine the HTML document that is passed in is the page that contains the banner ad (or the IFRAME that point to the banner ad) or is it the actual banner ad page?

I think I just confused myself ...

Example, let's say we have 2 pages ...

MyPage.htm - which contains an IFRAME to a banner
BannerPage.htm - which contains the A tag and IMG tag which you want to find the source

If you can parse through MyPage.htm you should be able to get the SRC of the banner IFRAME and then open BannerPage.htm and parse through that page ... once you get in BannerPage.htm you should be golden, right? Or am I missing something?

Let me know ...
0
 

Author Comment

by:_Mark_
ID: 10682120
Thanks for your input, guys.

COBOLdinosaur:
Maybe this is a naive assumption, but does this mean that Internet Explorer can access the information inspite cross domain security (it´s in the property window of the image), but I can´t?

dfu23:

You are right. It would defy the purpose of my app though if I had to leave the current page and navigate to the BannerPage.htm (in order to be allowed to parse its content). Opening it in a second (hidden) browser window is not very tempting either. Another problem: Even if I did navigate to BannerPage.htm, how would I know which one of the parsed images (page might have more than one) is the one that was displayed in the IFRAME on MyPage.htm?

Thanks.
0
 
LVL 31

Expert Comment

by:seanpowell
ID: 10682257
Just out of curiosity - why do you need to access the URL of the image in an iframe, as opposed to the image itself?
0
 
LVL 14

Expert Comment

by:dfu23
ID: 10682274
I know in the background you can do a scape from a URL (something like this):

// This uses items from the System.Net namespace

String url = "http://a.banner.ad/path/to/page";
String result = "";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    result = reader.ReadToEnd();
    reader.Close();
}

Now you should have the source of the BannerPage.htm ... best way to get what you need ... I'm not really sure.
0
 

Author Comment

by:_Mark_
ID: 10682346
seanpowell:

I want to be able to programmatically save the image to disk. This particular IFRAME image doesn´t get stored in the IE cache, so I need to download it. Therefore I need the url.

dfu23:

I am coding this app in VB6 and, to be honest, I haven´t delved into the NET Framework yet. I guess the above can´t be translated into VB6, can it?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10682804
The iframe is implemented as a window object.  When you right click on the image, the focus is to the instance of window in the iframe.  The the browser is not going cross domain in that case.  However when you try a reference to the iframe window from the window in teh main page you are now going cross domain.

If you you are trying to automate the saving of the image you don't need a browser at all you can do an http get server to server, and save the overhead and security hassle.

Cd&
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 14

Expert Comment

by:dfu23
ID: 10683917
I remember doing a "screen scrape" in classic ASP (VBScript) but don't remember the object that I used to get it to work ... maybe the XMLHTTP?

Sorry, I'm not very familiar with VB.
0
 

Author Comment

by:_Mark_
ID: 10684988
Sorry for the delay. In my part of the world (Austria) it was time to get some sleep ...

Interesting information on IE, COBOLdinosaur, thanks.

I have a small Browser Helper Object that allows "one click" saving of any image on a page. It works well, even on pages with nested frames, but I feel it is incomplete as long as it doesn´t also work for images displayed in iframes.

The example page I mentioned at the beginning of this thread contains an iframe, which in itself contains five iframes. Each of these five has a different image assigned to it. If I click on the iframe in the main page, the focus is on this window object and from there (I assume) it is passed on to the underlying, nested iframe, which in itself is again a window object. So I am now at the level of this sub-iframe and IE can access its properties and provide the image url.

It seems to be very difficult to do the same in code. This is how I imagine it:

To find out which nested iframe was clicked I would have to store the position of the mouse over the top iframe. Then, I guess, I would have to navigate to the address to which this main iframe links to. There I should be able to locate the desired sub-iframe, using the mouse coordinates. I extract the src of the sub-iframe, navigate to it, and parse it for .tagName="IMG". Considering that there might be more than one nesting level, this seems to become quite impossible.

Hopefully there is a more realistic way to do this?

Thanks.
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 250 total points
ID: 10695238
Well it does not matter how you navigate, you are still not going to do it with clinet scripting.  The onlyway that scripting can reference anything on the page in the iframe is with scripting that is on the same page.  Other wise it is cross- domain, and you are not going there no matter what you want.

The helper app works with nested frames be cause it is a seperate executable probram; not scripting.  It works with the framest, not he main page.  Obviously it does not understand iframes.

Cd&
0
 

Author Comment

by:_Mark_
ID: 10695542
What I meant with "navigate" was to actually load the cross domain page in a separate window - in that case access would, as you said, become possible. This wouldn´t be an acceptable solution anyways though.

I accept your answer and thank you for your help.

One more thought:

Under "IE security settings" - "Miscellaneous", there are the options "Access data sources across domains" and "Navigate sub-frames across different domains". I thought setting both to "Allow" would make cross domain access possible. Did not work though. Do these settings only work with scripts embedded in the webpage and not with BHO´s?


0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10696174
Those user setting allow the use of cross-domain forms and re-direction into sub-frames without security alerts.  Neither involves scripting; just what the browser is allowed to do to access resources; it does not make anything additional available to the scripting engine.

BTW the purpose of the security restriction, is that without it, a malicious web site could present a form from say Amazon and then use scripting to steal credit card information that he user thought they were sending only to Amazon.

Thanks for the A.  :^)

Cd&
0
 

Expert Comment

by:turbo1212
ID: 11124138

  Dim Document              as IHTMLDocument2
  Dim pFramesCollection     as IHTMLFramesCollection2
  Dim pDisp                 as IDispatch
  Dim IWindow2              as IHTMLWindow2
  Dim i                     as integer
  Dim VarIndex              as OleVariant
  Dim FrameDocument         as IHTMLDocument2


  NOTE: Assumes Document is set to the document you want to access the frames from

  pFramesCollection = Document.Frames

  If Not pFramesCollection Is Nothing Then)


      For i = 0 to pFramesCollection.Length - 1

          VarIndex = i
          Set pDisp = pframescollection.item(VarIndex)
          pDisp.QueryInterface(IHTMLWindow2,IWindow2)
          if Not IWindow2 Is Nothing Then

              if Not IWindow2.Document Is Nothing Then

                  Set FrameDocument = IWindow2.Document

              End If
          End If

      Next i

    End If
0
 

Author Comment

by:_Mark_
ID: 11129162
Thanks turbo1212.

Looks interesting. I´ll test it soon.
0
 

Author Comment

by:_Mark_
ID: 11134151
I had to change the code a little to make it work with VB:

    Dim pFramesCol As IHTMLFramesCollection2
!   Dim pDisp As Object
    Dim IWindow2 As IHTMLWindow2
    Dim i As Integer
!   Dim varIndex As Variant
    Dim frameDoc As IHTMLDocument2
   
   Set pFramesCol = doc.frames
   
   If Not pFramesCol Is Nothing Then
   
      For i = 0 To pFramesCol.length - 1
     
         varIndex = i
         
         Set pDisp = pFramesCol.Item(varIndex)
         
!        ' pDisp.QueryInterface IHTMLWindow2, IWindow2

!         Set IWindow2 = pDisp
         
         If Not IWindow2 Is Nothing Then
         
            If Not IWindow2.Document Is Nothing Then            <== Error: Access denied!
         
               Set frameDoc = IWindow2.Document
           
            End If
         End If
      Next i      
   End If

Unfortunately, on pages with cross domain iframes the result was the usual: Access denied.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now