Firefox, mozilla based browser URL

Just starting out? Need help? Post your questions and find answers here.
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Firefox, mozilla based browser URL

Post by ultralazor »

doctorized wrote:
ultralazor wrote:Getting window caption yields you no url.. No browsers put the url there but instead the title of the DOM if there is one.
No my friend. By taking the window caption I want the page title. For example the caption of this page is "PureBasic Forum Post a reply". That's what I want to take from GetWindowText_().
Oh, I thought it was trying to get URL, I'm surprised someone didn't tell how to get window titles in the first couple posts..

something from one of my projects:

Code: Select all

GetWindowText_(GetForegroundWindow_(),@title$,400)
so many ideas so little time..
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Firefox, mozilla based browser URL

Post by Faildeath »

Hello, sorry to bump up this topic, but, I'm having a tought time trying to solve my problem as I sadly understand almost nothing in these procedures.

I'd like to know why is the procedure sending multiple time the same url$? For eatch page I get between 1 and 3 time the same url$
but most of the time I get it twice.

And I've just seen that when clicking the 'Post a reply' page, I even got :"Tip: Styles can be applied quickly to selected text." as a result for the 'address'.

Also made a little modification as with Firefox 11.0 the url address bar give the correct address and then choped the "http://" part sending 2 different url$ for eatch page you visit.
I've also tried with IE just to see the result, same, returns between 1 and 3 time eatch page but doesn't have the "http://" issue.

Code: Select all

#CHILDID_SELF					= 0
#WINEVENT_OUTOFCONTEXT		= 0
#WINEVENT_SKIPOWNPROCESS	= $2
#EVENT_OBJECT_FOCUS			= $8005
#EVENT_OBJECT_VALUECHANGE	= $800E
#ROLE_SYSTEM_DOCUMENT		= $F
#ROLE_SYSTEM_PANE				= $10
#ROLE_SYSTEM_TEXT				= $2A

Procedure.l ASCIItoBSTR(asciiString$)
; Thanks to freak for this one
	Protected Result = 0
	CompilerIf #PB_Compiler_Unicode
		Result = SysAllocString_(@asciiString$)
	CompilerElse
	Protected *buff = AllocateMemory(Len(asciiString$)*2 + 2)
		If *buff
			PokeS(*buff, asciiString$, -1, #PB_Unicode)
			Result = SysAllocString_(*buff)
			FreeMemory(*buff)
		EndIf
	CompilerEndIf
ProcedureReturn Result
EndProcedure

Procedure WinEventFunc(HookHandle.l, hEvent.l, hwnd.l, idObject.l, idChild.l, idEventThread.l, dwmsEventTime.l)
	Protected *objectIa.IAccessible, a.i=0, b.i=0, c.i=0, d.i=0, e.i=0, f.i=0, g.i=0, h.i=0, i.i=0, j.i=0
	Static previousUrl$
	Select hEvent
	Case #EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE
		className$ = Space(256)
		GetClassName_(hwnd, @className$, 256)
		If className$ = "MozillaWindowClass" Or className$ = "Internet Explorer_Server"
			If CallFunction(0, "AccessibleObjectFromEvent", hwnd, idObject, idChild, @*objectIa, @v.VARIANT) = #S_OK
				v.VARIANT\vt = #VT_I4
				v\lVal = #CHILDID_SELF
				If *objectIa\get_accRole(v, @v) = #S_OK
					If v\lVal = #ROLE_SYSTEM_PANE Or v\lVal = #ROLE_SYSTEM_DOCUMENT Or v\lVal = #ROLE_SYSTEM_TEXT
						v\vt = #VT_I4
						v\lVal = #CHILDID_SELF
						url$ = Space(#MAX_PATH)
						bstr = ASCIItoBSTR(Space(#MAX_PATH))
						If *objectIa\get_accValue(v, @bstr) = #S_OK
							len = WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, 0, 0, 0, 0)
							url$ = Space(len)
							WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, @url$, len, 0, 0)
							
							If previousUrl$ <> url$ And url$ <> ""
								If Left(url$, 7) = "http://"
									;
								ElseIf Left(url$, 4) = "www."
									url$="http://"+url$
								EndIf
								
								AddGadgetItem(2, -1, url$) : SetClipboardText(url$)
								
							EndIf
							
						EndIf
					EndIf
					*objectIa\Release()
				EndIf
			EndIf
		EndIf
	EndSelect
EndProcedure

OpenWindow(0, 550, 450, 500, 500, "Try", #PB_Window_MinimizeGadget)
ButtonGadget(1, 10, 10, 120, 40, "Snif", #PB_Button_Toggle)
ListViewGadget(2, 10, 60, 480, 430)


quit = 0
CoInitialize_(0)
hdll = OpenLibrary(0, "Oleacc.dll")
Repeat
	Event = WaitWindowEvent()
	Select EventWindow()
	Case 0
		Select Event
		Case #PB_Event_Gadget
			Select EventGadget()
			Case 1
				If GetGadgetState(1) = 1
					eHook = SetWinEventHook_(#EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE, #Null, @WinEventFunc(), 0, 0, #WINEVENT_OUTOFCONTEXT|#WINEVENT_SKIPOWNPROCESS)
				Else
					UnhookWinEvent_(eHook)
				EndIf
			EndSelect
		Case #PB_Event_CloseWindow : Quit = 1
		EndSelect
	EndSelect
Until quit = 1 
CoUninitialize_()
If eHook : UnhookWinEvent_(eHook) : EndIf
If hdll : CloseLibrary(0) : EndIf
If someone could help my, would be very apreciated ;)
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Firefox, mozilla based browser URL

Post by doctorized »

A small change to your code. Change:

Code: Select all

If Left(url$, 7) = "http://"
;
ElseIf Left(url$, 4) = "www."
  url$="http://"+url$
EndIf
to

Code: Select all

If Left(url$, 7) <> "http://"
   url$="http://"+url$
EndIf
because the second time the URL has not the "http://".
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Firefox, mozilla based browser URL

Post by Faildeath »

Just under my nose and couldn't see it...

Thanks for the help doctorized :)


Edit :
I've tested a bit with it and still seems to get 2 time each page on the list.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Firefox, mozilla based browser URL

Post by doctorized »

Faildeath wrote:I've tested a bit with it and still seems to get 2 time each page on the list.
Have you made any modifications that are not in the above code?
I am asking it because after

Code: Select all

AddGadgetItem(2, -1, url$) : SetClipboardText(url$)
the line

Code: Select all

previousUrl$ = url$
seems to be missing. You check if the current URL is not the same with the previous one but the previous one seems to get value nowhere.
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Firefox, mozilla based browser URL

Post by Faildeath »

Very good observation, didn't even noticed it. I must have remooved it along with other parts of my code.
I've made no other modif, copied it and modified the part, added the forgotten line and still comming with 2 time the same url.
But there is improvement, I now have a 30-40% chances of have just 1 time each url.

There is the code I tested in case I made another mistake...

Code: Select all

#CHILDID_SELF               = 0
#WINEVENT_OUTOFCONTEXT      = 0
#WINEVENT_SKIPOWNPROCESS   = $2
#EVENT_OBJECT_FOCUS         = $8005
#EVENT_OBJECT_VALUECHANGE   = $800E
#ROLE_SYSTEM_DOCUMENT      = $F
#ROLE_SYSTEM_PANE            = $10
#ROLE_SYSTEM_TEXT            = $2A

Procedure.l ASCIItoBSTR(asciiString$)
; Thanks to freak for this one
   Protected Result = 0
   CompilerIf #PB_Compiler_Unicode
      Result = SysAllocString_(@asciiString$)
   CompilerElse
   Protected *buff = AllocateMemory(Len(asciiString$)*2 + 2)
      If *buff
         PokeS(*buff, asciiString$, -1, #PB_Unicode)
         Result = SysAllocString_(*buff)
         FreeMemory(*buff)
      EndIf
   CompilerEndIf
ProcedureReturn Result
EndProcedure

Procedure WinEventFunc(HookHandle.l, hEvent.l, hwnd.l, idObject.l, idChild.l, idEventThread.l, dwmsEventTime.l)
   Protected *objectIa.IAccessible, a.i=0, b.i=0, c.i=0, d.i=0, e.i=0, f.i=0, g.i=0, h.i=0, i.i=0, j.i=0
   Static previousUrl$
   Select hEvent
   Case #EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE
      className$ = Space(256)
      GetClassName_(hwnd, @className$, 256)
      If className$ = "MozillaWindowClass" Or className$ = "Internet Explorer_Server"
         If CallFunction(0, "AccessibleObjectFromEvent", hwnd, idObject, idChild, @*objectIa, @v.VARIANT) = #S_OK
            v.VARIANT\vt = #VT_I4
            v\lVal = #CHILDID_SELF
            If *objectIa\get_accRole(v, @v) = #S_OK
               If v\lVal = #ROLE_SYSTEM_PANE Or v\lVal = #ROLE_SYSTEM_DOCUMENT Or v\lVal = #ROLE_SYSTEM_TEXT
                  v\vt = #VT_I4
                  v\lVal = #CHILDID_SELF
                  url$ = Space(#MAX_PATH)
                  bstr = ASCIItoBSTR(Space(#MAX_PATH))
                  If *objectIa\get_accValue(v, @bstr) = #S_OK
                     len = WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, 0, 0, 0, 0)
                     url$ = Space(len)
                     WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, @url$, len, 0, 0)
                     
                     If previousUrl$ <> url$ And url$ <> ""
                        If Left(url$, 7) <> "http://"
                           url$="http://"+url$
                        EndIf
                        
                        AddGadgetItem(2, -1, url$) : SetClipboardText(url$)
                        previousUrl$ = url$
                     EndIf
                     
                  EndIf
               EndIf
               *objectIa\Release()
            EndIf
         EndIf
      EndIf
   EndSelect
EndProcedure

OpenWindow(0, 550, 450, 500, 500, "Try", #PB_Window_MinimizeGadget)
ButtonGadget(1, 10, 10, 120, 40, "Snif", #PB_Button_Toggle)
ListViewGadget(2, 10, 60, 480, 430)

quit = 0
CoInitialize_(0)
hdll = OpenLibrary(0, "Oleacc.dll")
Repeat
   Event = WaitWindowEvent()
   Select EventWindow()
   Case 0
      Select Event
      Case #PB_Event_Gadget
         Select EventGadget()
         Case 1
            If GetGadgetState(1) = 1
               eHook = SetWinEventHook_(#EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE, #Null, @WinEventFunc(), 0, 0, #WINEVENT_OUTOFCONTEXT|#WINEVENT_SKIPOWNPROCESS)
            Else
               UnhookWinEvent_(eHook)
            EndIf
         EndSelect
      Case #PB_Event_CloseWindow : Quit = 1
      EndSelect
   EndSelect
Until quit = 1
CoUninitialize_()
If eHook : UnhookWinEvent_(eHook) : EndIf
If hdll : CloseLibrary(0) : EndIf
Thanks for the help, very apreciated.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Firefox, mozilla based browser URL

Post by doctorized »

I think now the code is running just fine.
We were checking if the previous url was the same with the new one and the new one hadn't "http://" in front.

Code: Select all

#CHILDID_SELF               = 0
#WINEVENT_OUTOFCONTEXT      = 0
#WINEVENT_SKIPOWNPROCESS   = $2
#EVENT_OBJECT_FOCUS         = $8005
#EVENT_OBJECT_VALUECHANGE   = $800E
#ROLE_SYSTEM_DOCUMENT      = $F
#ROLE_SYSTEM_PANE            = $10
#ROLE_SYSTEM_TEXT            = $2A

Procedure.l ASCIItoBSTR(asciiString$)
; Thanks to freak for this one
   Protected Result = 0
   CompilerIf #PB_Compiler_Unicode
      Result = SysAllocString_(@asciiString$)
   CompilerElse
   Protected *buff = AllocateMemory(Len(asciiString$)*2 + 2)
      If *buff
         PokeS(*buff, asciiString$, -1, #PB_Unicode)
         Result = SysAllocString_(*buff)
         FreeMemory(*buff)
      EndIf
   CompilerEndIf
ProcedureReturn Result
EndProcedure

Procedure WinEventFunc(HookHandle.l, hEvent.l, hwnd.l, idObject.l, idChild.l, idEventThread.l, dwmsEventTime.l)
   Protected *objectIa.IAccessible, a.i=0, b.i=0, c.i=0, d.i=0, e.i=0, f.i=0, g.i=0, h.i=0, i.i=0, j.i=0
   Static previousUrl$
   Select hEvent
   Case #EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE
      className$ = Space(256)
      GetClassName_(hwnd, @className$, 256)
      If className$ = "MozillaWindowClass" Or className$ = "Internet Explorer_Server"
         If CallFunction(0, "AccessibleObjectFromEvent", hwnd, idObject, idChild, @*objectIa, @v.VARIANT) = #S_OK
            v.VARIANT\vt = #VT_I4
            v\lVal = #CHILDID_SELF
            If *objectIa\get_accRole(v, @v) = #S_OK
               If v\lVal = #ROLE_SYSTEM_PANE Or v\lVal = #ROLE_SYSTEM_DOCUMENT Or v\lVal = #ROLE_SYSTEM_TEXT
                  v\vt = #VT_I4
                  v\lVal = #CHILDID_SELF
                  url$ = Space(#MAX_PATH)
                  bstr = ASCIItoBSTR(Space(#MAX_PATH))
                  If *objectIa\get_accValue(v, @bstr) = #S_OK
                     len = WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, 0, 0, 0, 0)
                     url$ = Space(len)
                     WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, @url$, len, 0, 0)
                     
                     If Left(url$, 4) <> "http"; to catch http and https.
                        url$="http://"+url$
                     EndIf
                     If previousUrl$ <> url$ And url$ <> ""
                        AddGadgetItem(2, -1, url$) : SetClipboardText(url$)
                        previousUrl$ = url$
                     EndIf
                     
                  EndIf
               EndIf
               *objectIa\Release()
            EndIf
         EndIf
      EndIf
   EndSelect
EndProcedure

OpenWindow(0, 550, 450, 500, 500, "Try", #PB_Window_MinimizeGadget)
ButtonGadget(1, 10, 10, 120, 40, "Snif", #PB_Button_Toggle)
ListViewGadget(2, 10, 60, 480, 430)

quit = 0
CoInitialize_(0)
hdll = OpenLibrary(0, "Oleacc.dll")
Repeat
   Event = WaitWindowEvent()
   Select EventWindow()
   Case 0
      Select Event
      Case #PB_Event_Gadget
         Select EventGadget()
         Case 1
            If GetGadgetState(1) = 1
               eHook = SetWinEventHook_(#EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE, #Null, @WinEventFunc(), 0, 0, #WINEVENT_OUTOFCONTEXT|#WINEVENT_SKIPOWNPROCESS)
            Else
               UnhookWinEvent_(eHook)
            EndIf
         EndSelect
      Case #PB_Event_CloseWindow : Quit = 1
      EndSelect
   EndSelect
Until quit = 1
CoUninitialize_()
If eHook : UnhookWinEvent_(eHook) : EndIf
If hdll : CloseLibrary(0) : EndIf
Faildeath
User
User
Posts: 18
Joined: Mon Mar 19, 2012 7:49 pm

Re: Firefox, mozilla based browser URL

Post by Faildeath »

So simple yet hard to see sometimes...

Thanks doc, works perfectly now :)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Firefox, mozilla based browser URL

Post by IdeasVacuum »

This code is related to what I'm looking for, but it's really doing too much, I only need to collect one web address........

In my scenario, the User already has one or more Browsers running, and one of them is displaying the web page of interest. So, my App could ask the User to simply pick the Browser Url field.

It is easy enough to get the handle for the browser window itself via WindowFromPoint_().
The problem though, is how to get the handle for the Url field? If it is possible to get that handle, then the Url it contains could be collected using SendMessage_():

Code: Select all

;This is not an app!
;It's just (inadequate) test code to retrieve the current URL from a browser...

Enumeration
#Win
#StrText
#BtnPick
EndEnumeration

Global sgText.s = Space(1024)

Procedure ShowWin()
;------------------

  If OpenWindow(#Win, 0, 0, 500, 80, "Get Text", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)

               StringGadget(#StrText, 10, 10, 480, 25, "")
               ButtonGadget(#BtnPick, 10, 50, 480, 25, "Pick Browser Window Title Bar")
  EndIf

EndProcedure

Procedure.i PickWin()
;--------------------
Protected  sWinTitle.s = Space(1024)
Protected     iEvent.i = 0
Protected  iHwndPick.i = 0
Protected      iStop.i = #False
Protected  WinPickPt.POINT


           Repeat
                     iEvent = WaitWindowEvent(1)

                     If (GetAsyncKeyState_(#VK_LBUTTON) & 32768)

                            WinPickPt\x = DesktopMouseX()
                            WinPickPt\y = DesktopMouseY()
                              iHwndPick = WindowFromPoint_(PeekQ(WinPickPt))

                           GetWindowText_(iHwndPick, sWinTitle, 1024)

                           If Not( (sWinTitle = "") Or (sWinTitle = "Get Text") Or (sWinTitle = "Program Manager") )

                                     SetGadgetText(#StrText,sWinTitle) ;Confirms the Window picked
                                     ProcedureReturn(iHwndPick)
                                     iStop = #True
                           EndIf

                     EndIf

                     If (GetAsyncKeyState_(#VK_ESCAPE) & 32768)

                           ProcedureReturn(0)
                           iStop = #True

                     EndIf

           Until iStop = #True

EndProcedure

Procedure WaitForUser()
;---------------------
Protected iEvent.i = 0
Protected  iExit.i = #False
Protected iGdgID.i = 0
Protected   hWnd.i = 0
Protected   cWnd.i = 0

     Repeat
                         iEvent = WaitWindowEvent(1)
                  Select iEvent
                  
                          Case #PB_Event_Gadget

                                    iGdgID = EventGadget()
                             Select iGdgID
                  
                                        Case #BtnPick

                                                    hWnd = PickWin()
                                             If Not(hWnd = 0)
                  
                                                     ;Using GetWindow here is not specific enough
                                                     ;Need to give SendMessage the handle of the URL field
                                                     cWnd = GetWindow_(hWnd,#GW_CHILD)
                  
                                                     SendMessage_(cWnd,#WM_GETTEXT,0,@sgText)
                  
                                                     sgText = Trim(sgText)
                                                     If(Len(sgText) > 4) : SetGadgetText(#StrText,sgText) : EndIf
                                             EndIf
                             EndSelect
                  EndSelect

     Until iEvent = #PB_Event_CloseWindow

EndProcedure

;###Main Entry Point
ShowWin()
WaitForUser()

End
In the code above, I'm asking for the Browser title bar to be picked - if the Url field is picked in FireFox, the window handle is returned. The window handle for IE or Chrome however is not returned unless the title bar is picked rather than the Url field.

Edit: The Class Name for Goggle Chrome is now "Chrome_WidgetWin_1"
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Firefox, mozilla based browser URL

Post by IdeasVacuum »

.... Re the latest version of the original code, edited by doctorized, it works well with the three Browsers I have tested, so long as the code is compiled in ASCII mode.

If the compiler is set to Unicode, the URL string is output in Chinese:

http://睷⹷異敲慢楳⹣牦支杮楬桳瘯敩瑷灯捩瀮灨昿ㄽ☳㵴㔲㄰☱瑳牡㵴〶

To correct this, replace:

Code: Select all

WideCharToMultiByte_(#CP_ACP, 0, bstr, -1, @sUrl, len, 0, 0)
with:

Code: Select all

sUrl = PeekS(bstr, len, #PB_Unicode)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Firefox, mozilla based browser URL

Post by doctorized »

Along with the URL is there any way to get meta data too?
By saying meta data I mean these:

Code: Select all

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en-gb" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="resource-type" content="document" />
<meta name="distribution" content="global" />
<meta name="keywords" content="" />
<meta name="description" content="" />
I want to search if some specific "meta name" is present and if so, to get its "content".
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Firefox, mozilla based browser URL

Post by doctorized »

No one can help? Is there a way to get these data or not? :cry:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Firefox, mozilla based browser URL

Post by IdeasVacuum »

meta data...don't you have to download the page and parse it?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Firefox, mozilla based browser URL

Post by doctorized »

IdeasVacuum wrote:meta data...don't you have to download the page and parse it?
The page is already opened in a browser. With the code above I can take the URL. I want the meta data too.
Post Reply