iNET Interactive - Online Advertising Agency
          
Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Get current URL from Browser


Reply
 
Thread Tools Display Modes
  #1  
Old 12-21-2003, 08:52 AM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Cool 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
Reply With Quote
  #2  
Old 12-21-2003, 09:26 AM
Icek Icek is offline
Restricted
 
Join Date: Jun 2003
Location: Warsaw, Poland
Posts: 183
Default

i.e in VB script:
parent.script.document.location should hold the url address
Reply With Quote
  #3  
Old 12-21-2003, 09:35 AM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Default

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 !
Reply With Quote
  #4  
Old 12-21-2003, 10:01 AM
Csharp's Avatar
Csharp Csharp is offline
Senior Contributor
* Expert *
 
Join Date: Jul 2003
Location: Ashby, Leicestershire.
Posts: 963
Default

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 ~~
Reply With Quote
  #5  
Old 12-21-2003, 11:53 AM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Default

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..
Reply With Quote
  #6  
Old 12-21-2003, 03:36 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul
Retired Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 17,006
Default

I think you need different code for every available browser.
I read something about it on www.mvps.org/vbnet

Yeah, here it is:
http://www.mvps.org/vbnet/index.html...browserurl.htm
Reply With Quote
  #7  
Old 12-22-2003, 12:48 AM
PlenoJure's Avatar
PlenoJure PlenoJure is offline
'With Full Right'
Retired Moderator
* Guru *
 
Join Date: Jun 2003
Posts: 1,880
Default

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.
__________________
Adam
-- Please use [vb][/vb] tags for code | Posting Guidelines | Please check the MSDN and Search before posting
-- Other Sites: Did you search Google first? | My Blog
Reply With Quote
  #8  
Old 12-22-2003, 04:37 AM
dragon4spy dragon4spy is offline
Banned
 
Join Date: Jul 2003
Location: Angkor, Cambodia
Posts: 143
Default

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.
Reply With Quote
  #9  
Old 12-22-2003, 04:57 AM
PlenoJure's Avatar
PlenoJure PlenoJure is offline
'With Full Right'
Retired Moderator
* Guru *
 
Join Date: Jun 2003
Posts: 1,880
Default

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?
__________________
Adam
-- Please use [vb][/vb] tags for code | Posting Guidelines | Please check the MSDN and Search before posting
-- Other Sites: Did you search Google first? | My Blog
Reply With Quote
  #10  
Old 12-22-2003, 05:10 AM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Default

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 !
Reply With Quote
  #11  
Old 12-22-2003, 05:12 AM
dragon4spy dragon4spy is offline
Banned
 
Join Date: Jul 2003
Location: Angkor, Cambodia
Posts: 143
Default

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.
Reply With Quote
  #12  
Old 12-22-2003, 05:19 AM
dragon4spy dragon4spy is offline
Banned
 
Join Date: Jul 2003
Location: Angkor, Cambodia
Posts: 143
Default

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).
Reply With Quote
  #13  
Old 12-22-2003, 05:46 AM
PlenoJure's Avatar
PlenoJure PlenoJure is offline
'With Full Right'
Retired Moderator
* Guru *
 
Join Date: Jun 2003
Posts: 1,880
Default

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.
__________________
Adam
-- Please use [vb][/vb] tags for code | Posting Guidelines | Please check the MSDN and Search before posting
-- Other Sites: Did you search Google first? | My Blog
Reply With Quote
  #14  
Old 12-22-2003, 11:41 PM
dragon4spy dragon4spy is offline
Banned
 
Join Date: Jul 2003
Location: Angkor, Cambodia
Posts: 143
Default

That is a good news guy.
Reply With Quote
  #15  
Old 12-23-2003, 11:56 AM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Default


wait a moment .. i'm on it .. i have it .. now i'll code it and then i'll post the sourcecode

Last edited by franky : 12-23-2003 at 12:21 PM.
Reply With Quote
  #16  
Old 12-23-2003, 03:12 PM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Talking 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
Reply With Quote
  #17  
Old 12-24-2003, 08:23 AM
franky franky is offline
Newcomer
 
Join Date: Dec 2003
Posts: 7
Default 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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Web browser ctl doesn't update automatically Val Interface and Graphics 0 07-02-2003 04:05 PM
How TO - Manipulate Current Open Browser Properties CNW Media Web Programming 9 05-04-2003 09:48 PM
Current form, current sub Sandro Cella General 2 03-19-2003 02:24 PM
Newbie with Web Browser X-Fe@R General 3 09-10-2002 06:33 PM

Advertisement: