 |

12-21-2003, 08:52 AM
|
|
|
Get current URL from Browser
|
|
Hi,
im now working for almost two weeks on this 'simple' problem:
i want to get the URI (or the HTML Source-Code) of the webpage that is currently displayed in any browser (it should work with IExplorer, of course, but also with Netscape).
Well, at first I wrote my own Proxy-Server. Basically, this isn't very difficult and it works fine with most websites. But in some cases there are problems with different server-configurations, different Http-Requests, PHP-Boards  and so on...so this wasn't a good idea 
After that, i tried with DDE - but as far as i know, DDE is no longer supported in Netscape 6 and higher 
So now i'm searching for another solution - eventually by using some nice API-functions ... ?
Does anyone have an idea ?? I'm glad on every hint  It may be VB6, VB.Net, C++, C#,... - no matter, main thing it helps !
Greetings,
Frank
|
|

12-21-2003, 09:26 AM
|
|
Restricted
|
|
Join Date: Jun 2003
Location: Warsaw, Poland
|
|
i.e in VB script:
parent.script.document.location should hold the url address
|
|

12-21-2003, 09:35 AM
|
|
|
|
|
Quote: Originally Posted by Icek i.e in VB script:
parent.script.document.location should hold the url address
Well, thanks, that is a VB Script, which is embedded in the HTML-Code, isn't it ? But I want to get the URL with my external application !
|
|

12-21-2003, 10:01 AM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Jul 2003
Location: Ashby, Leicestershire.
|
|
i'll try to get you started  ...
Code:
Private Sub Command1_Click()
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
Dim objSWindows As Object
Set objSWindows = objShell.Windows
Dim o As Object
For Each o In objSWindows
'/// this will also show local folders ( eg: my documents ) so you can filter them out if needed.
If Not Left(o.LocationUrl, 7) = "file://" Then
MsgBox o.LocationUrl
End If
Next
End Sub
|
__________________
~~ please don't PM me regarding code, I only reply to personnal messages ~~
|

12-21-2003, 11:53 AM
|
|
|
thanks a lot . That's a great idea...but it works only on microsoft internet explorer - the netscape window is ignored 
you see, it is difficult to make me happy.. 
|
|

12-21-2003, 03:36 PM
|
 |
Lost Soul
Retired Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
|
|
|

12-22-2003, 12:48 AM
|
 |
'With Full Right'
Retired Moderator * Guru *
|
|
|
|
But it gets even more complicated than that, as some code will be limited to certain versions of the browser. So if you cover IE, Netscape, Mozilla & Opera; you'll probably face approching a dozen different sets of code each tied to certain versions of the browser. So this will probably be a bit of a challenge, to say the least.
What I would probably do is use EnumWindows to find the browsers, then use EnumChildWindows to go thru all of the child windows calling GetWindowText or something similar to see if there is any test return from each, the check it to see if it contains a valid URL ( IsValidURL). That should work with most browsers with some tweaking, but it's also gooing to be a bit slow as you'll be doing a lot of extra work just to find the windows you are after.
|
|

12-22-2003, 04:37 AM
|
|
Banned
|
|
Join Date: Jul 2003
Location: Angkor, Cambodia
|
|
It won't work. The URL in the browsers is the same as caption control whose text can't be capture by GetWindowText.
|
Last edited by dragon4spy : 12-22-2003 at 04:44 AM.
|

12-22-2003, 04:57 AM
|
 |
'With Full Right'
Retired Moderator * Guru *
|
|
|
|
dragon4spy,
Did you note where I said "or something similar"? It could be SendMessage using WM_GETTEXT or one of serveral other methods. How exactly it's done depends on the browser.
So shall I assume you have tested this method on all of the major browsers? Since you state it won't work I must assume so, so what were you findings? Or, are you just assuming it wont work?
|
|

12-22-2003, 05:10 AM
|
|
|
thanks a lot! the tip from Flyguy works fine, the only browser-specific thing you have to change is the identifier: iexplorer for Internet Explorer(at least version 5 needed, as far as i know), NETSCAPE6 for Netscape 6 and higher and opera for Opera !
But there is still one problem: if more than one instance of the browser is opened, the DDE-call only gets the URL from the last opened one 
Is it possible to fix that? I want to get all viewed URL's ! How can i access a specified (i.ex. by its window handle) browser-window with DDE ?
@PlenoJure : your idea would solve this problem, too. So i also will try this !
|
|

12-22-2003, 05:12 AM
|
|
Banned
|
|
Join Date: Jul 2003
Location: Angkor, Cambodia
|
|
I have tried it already. Not just try for a day or two, but months. You can search this forum for my threads concerning about this problem few months ago.
|
|

12-22-2003, 05:19 AM
|
|
Banned
|
|
Join Date: Jul 2003
Location: Angkor, Cambodia
|
|
Quote:
But there is still one problem: if more than one instance of the browser is opened, the DDE-call only gets the URL from the last opened one
Is it possible to fix that?
|
Well, i recommend not to use DDE. It is out of date and very unstable.
I have the same problem, but now i've decided to support only IE, cos it's the most popular browser (according to statistic of a poll in this forum).
|
|

12-22-2003, 05:46 AM
|
 |
'With Full Right'
Retired Moderator * Guru *
|
|
|
|
I've just ran a few tests with the method I mentioned, and I'm having some luck. I'm not getting them all, but I should be able to get to to work with at least some browsers. I'll post back later with more results.
|
|

12-22-2003, 11:41 PM
|
|
Banned
|
|
Join Date: Jul 2003
Location: Angkor, Cambodia
|
|
That is a good news guy. 
|
|

12-23-2003, 11:56 AM
|
|
|
Last edited by franky : 12-23-2003 at 12:21 PM.
|

12-23-2003, 03:12 PM
|
|
|
I got it ! :)
Quote: Originally Posted by franky
  
wait a moment .. i'm on it .. i have it .. now i'll code it and then i'll post the sourcecode
HERE IT IS !
It works both with Netscape Navigator and Internet Explorer. It should also work with any other Browser (and any other application) that is of the "Edit" class. The only thing you have to fit is the if-condition in "EnumProc". As it is by now, the public function "GetURLList" returns the currently viewed URLs as a string, seperated by ",".
Simply write this code into a module:
Code:
Option Explicit
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long
Private Type ProcData
AppHwnd As Long
title As String
Placement As String
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDFIRST = 0
'string that will contain the List of URL's, seperated by ","
Private sURLList As String
Public Function GetURLList() As String
sURLList = ""
EnumWindows AddressOf EnumProc, 0
If Len(sURLList) > 0 Then
sURLList = Mid(sURLList, 2) 'cut the leading ","
End If
GetURLList = sURLList
End Function
' If this window is of the Edit class, return
' its contents. Otherwise search its children
' for an Edit object.
Private Function EditInfo(window_hwnd As Long) As String
Dim txt As String
Dim buf As String
Dim buflen As Long
Dim child_hwnd As Long
Dim children() As Long
Dim num_children As Integer
Dim i As Integer
' Get the class name.
buflen = 256
buf = Space$(buflen - 1)
buflen = GetClassName(window_hwnd, buf, buflen)
buf = Left$(buf, buflen)
' See if we found an Edit object.
If buf = "Edit" Then
EditInfo = WindowText(window_hwnd)
Exit Function
End If
' It's not an Edit object. Search the children.
' Make a list of the child windows.
num_children = 0
child_hwnd = GetWindow(window_hwnd, GW_CHILD)
Do While child_hwnd <> 0
num_children = num_children + 1
ReDim Preserve children(1 To num_children)
children(num_children) = child_hwnd
child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
Loop
' Get information on the child windows.
For i = 1 To num_children
txt = EditInfo(children(i))
If txt <> "" Then Exit For
Next i
EditInfo = txt
End Function
' ************************************************
' Return the text associated with the window.
' ************************************************
Private Function WindowText(window_hwnd As Long) As String
Dim txtlen As Long
Dim txt As String
WindowText = ""
If window_hwnd = 0 Then Exit Function
txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
If txtlen = 0 Then Exit Function
txtlen = txtlen + 1
txt = Space$(txtlen)
txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, ByVal txt)
WindowText = Left$(txt, txtlen)
End Function
Private Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
Dim buf As String * 1024
Dim title As String
Dim length As Long
' Get the window's title.
length = GetWindowText(app_hwnd, buf, Len(buf))
title = Left$(buf, length)
' See if the title ends with " - Netscape" or " - Microsoft Internet Explorer".
If Right$(title, 10) = " -Netscape" Or Right$(title, 30) = " - Microsoft Internet Explorer" Then
' This is it. Find the ComboBox information.
sURLList = sURLList & "," & EditInfo(app_hwnd)
End If
' Continue searching
EnumProc = 1
End Function
|
|

12-24-2003, 08:23 AM
|
|
|
remark
hum, it is not completely perfect yet. I recognized some cases in that this algorithm returned not a url, but the text of another Edit-class child of the browser (for example the text entered in the google-searchbar) . To make it absolutely sure, you should check the return value of WindowText, if it is a valid URL, using the IsValidURL function . To implement this, is your homework for the new year 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |
|