Author Topic: Get all the links elements into a browser document  (Read 4892 times)

0 Members and 2 Guests are viewing this topic.

Offline Eros Olmi

  • Full Member
  • ***
  • Posts: 243
  • User-Rate: +18/-7
    • thinBasic
Get all the links elements into a browser document
« on: April 11, 2009, 10:47:22 PM »
Does someone have a piece of code that can show me how to collect all the links in a web browser document?

IHTMLDocument2 has links Property that should return a reference to IHTMLElementCollection but than ...?

Thanks a lot
Eros

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
Re: Get all the links elements into a browser document
« Reply #1 on: April 12, 2009, 01:02:22 AM »
Then parse the IHTMLElementCollection calling the length property to retrieve the number of items in the collection and the item method to retrieve individual items. Something like:

Code: [Select]
DIM pIHTMLElementCollection AS IHTMLElementCollection
DIM hr AS LONG
DIM pDispatch AS IDispatch
DIM pIHTMLLinkElement AS IHTMLLinkElement
DIM i AS LONG
DIM nCount AS LONG
DIM vIndex AS VARIANT
DIM vItem AS VARIANT
DIM strRef AS STRING

pIHTMLElementCollection = pIHTMLDocument2.links
nCount = pIHTMLElementCollection.length
FOR i = 0 TO nCount - 1
   vIdx = i AS LONG
   pDispatch = pIHTMLElementCollection.Item(vIdx)
   pIHTMLLinkElement = pDispatch
   pDispatch = NOTHING
   IF ISNOTHING(pIHTMLLinkElement) THEN EXIT FOR
   ' Retrieve the properties
   strRef = pIHTMLLinkElement.href
   STDOUT "href = " & ACODE$(strRef)
   ...
   ...
   ' Release the object
   pIHTMLLinkElement = NOTHING
NEXT

or enumerate it using the standard IEnumVARIANT interface:

Code: [Select]
DIM pIHTMLElementCollection AS IHTMLElementCollection
DIM hr AS LONG
DIM penum AS IEnumVARIANT
DIM pIHTMLLinkElement AS IHTMLLinkElement
DIM vItem AS VARIANT
DIM strRef AS STRING

pIHTMLElementCollection = pIHTMLDocument2.links
penum = pIHTMLElementCollection.newEnum_
DO
   ' Retrieve a reference to the next object in the collection
   hr = pEnum.Next(1, vItem, BYVAL %NULL)
   IF hr <> %S_OK THEN EXIT DO
   ' Assign the VT_DISPATCH variant to the object variable
   pIHTMLLinkElement = vItem
   vItem = EMPTY
   IF ISNOTHING(pIHTMLLinkElement) THEN EXIT DO
   ' Retrieve the properties
   strRef = pIHTMLLinkElement.href
   STDOUT "href = " & ACODE$(strRef)
   ...
   ...
   ' Release the object
   pIHTMLLinkElement = NOTHING
LOOP

Offline Eros Olmi

  • Full Member
  • ***
  • Posts: 243
  • User-Rate: +18/-7
    • thinBasic
Re: Get all the links elements into a browser document
« Reply #2 on: April 12, 2009, 07:10:38 AM »
Thank you José.

There must be something not working in first method (second not yet tested).

I correctly get the number of items in "count"

Code: [Select]
nCount = pIHTMLElementCollection.length
but than
Code: [Select]
If IsNothing(pIHTMLLinkElement) Then Exit Fornever pass because "ISNOTHING(pIHTMLLinkElement)" is always true preventing the collection of hRef
 
« Last Edit: April 12, 2009, 08:16:52 AM by Eros Olmi »
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline Eros Olmi

  • Full Member
  • ***
  • Posts: 243
  • User-Rate: +18/-7
    • thinBasic
Re: Get all the links elements into a browser document
« Reply #3 on: April 12, 2009, 08:11:59 AM »
Also with second method, IEnumVARIANT interface, the following is immediately TRUE and exit the DO loop

Code: [Select]
If IsNothing(pIHTMLLinkElement) Then Exit Do
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Offline Dominic Mitchell

  • Jr. Member
  • **
  • Posts: 64
  • User-Rate: +11/-5
    • Prometheus Software
Dominic Mitchell
Phoenix Visual Designer
http://www.phnxthunder.com

Offline Eros Olmi

  • Full Member
  • ***
  • Posts: 243
  • User-Rate: +18/-7
    • thinBasic
Re: Get all the links elements into a browser document
« Reply #5 on: April 12, 2009, 02:13:33 PM »
Got it working.
Many thanks
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB