Firefox URL not detected on W10 [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Firefox URL not detected on W10 [Resolved]

Post by Kwai chang caine »

Hello at all

I use this magical code of Sparkie (modified by NetMaestro and perhaps another members) for read directly the URL of Firefox (Portable or not) without Sendkeys :shock: 8)
https://www.purebasic.fr/english/viewto ... 45#p174745
I have also try the last code above of Doctorized
https://www.purebasic.fr/english/viewto ... 44#p378044

That works on my portable W7 machine with v5.73, but not on my main machine in W10 :|
Apparently it's this condition (at line 38) who are not respected and i don't know why :cry:

Code: Select all

If v\lVal = #ROLE_SYSTEM_PANE Or v\lVal = #ROLE_SYSTEM_DOCUMENT Or v\lVal = #ROLE_SYSTEM_TEXT
Someone have an idea why that not works ?

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)
                     url$ = PeekS(bstr, len)
                     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
Have a good day
Last edited by Kwai chang caine on Fri Dec 01, 2023 6:06 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Firefox URL not detected on W10

Post by BarryG »

My advice is to skip this type of code. Firefox (and other web browsers) are always changing and the code above isn't future-proof for them. I learned this years ago and now just copy the URL from the address bar with SendKeys. Been working perfectly that way over multiple Firefox versions for 10 years and I don't have to worry about my code breaking when Firefox changes.
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Firefox URL not detected on W10

Post by Axolotl »

Sorry, but this is not my area of expertise. I tried myself to figure out the IAccessable interface and always failed.

The value of v\lVal is $E (14) at my system. But this investigation is not helping anyways.

Code: Select all

#ROLE_SYSTEM_APPLICATION     = $0E  
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Firefox URL not detected on W10

Post by RASHAD »

Hi KCC
Run the snippet first then FireFox
Tested with PB 6.03 x86 Windows 11 x64

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

Global eHook

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(#MAX_PATH)
      GetClassName_(hwnd, @className$, #MAX_PATH)
      If className$ = "MozillaWindowClass" Or className$ = "Internet Explorer_Server" Or className$ = "IEFrame" Or className$ = "OperaWindowClass"
        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 = @url$
              If *objectIa\get_accValue(v, @bstr) = #S_OK
                url$ = PeekS(bstr, -1, #PB_Unicode) 
                If previousUrl$ <> url$ And url$ <> ""
                  AddGadgetItem(0, -1, url$)
                  SetClipboardText(url$)
                  previousUrl$ = url$
                EndIf                   
              EndIf
            EndIf
            *objectIa\Release()
          Else
            MessageRequester("Error","get_accRole Error ",#MB_OK|#MB_ICONERROR)
          EndIf
        EndIf
      EndIf
  EndSelect
EndProcedure

OpenWindow(0, 550, 450, 500, 500, "Get Browser URL", #PB_Window_MinimizeGadget)
ListViewGadget(0, 10, 10, 480, 480)

CoInitialize_(0)
hdll = OpenLibrary(0, "Oleacc.dll")
If hdll
  eHook = SetWinEventHook_(#EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE, #Null, @WinEventFunc(), 0, 0, #WINEVENT_OUTOFCONTEXT|#WINEVENT_SKIPOWNPROCESS)
  If eHook = 0
    MessageRequester("Error","Unsuccessful Hooking",#MB_OK|#MB_ICONERROR)
  EndIf
Else
  MessageRequester("Error","The DLL not loaded ",#MB_OK|#MB_ICONERROR)
EndIf
Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect
Until quit = 1
CoUninitialize_()
UnhookWinEvent_(eHook)
CloseLibrary(0)
Egypt my love
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Firefox URL not detected on W10

Post by Axolotl »

Hi Rashad,
unfortunately your code does not show anything different than KCC's.
My debug result (see code below) is still 14 .....

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

Global eHook

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(#MAX_PATH)
      GetClassName_(hwnd, @className$, #MAX_PATH)
      If className$ = "MozillaWindowClass" Or className$ = "Internet Explorer_Server" Or className$ = "IEFrame" Or className$ = "OperaWindowClass"
        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
Debug "== " + v\llVal 	; Debug output == 14 --> which means #ROLE_SYSTEM_APPLICATION acc. to MSDN. 
            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 = @url$
              If *objectIa\get_accValue(v, @bstr) = #S_OK
                url$ = PeekS(bstr, -1, #PB_Unicode) 
                If previousUrl$ <> url$ And url$ <> ""
                  AddGadgetItem(0, -1, url$)
                  SetClipboardText(url$)
                  previousUrl$ = url$
                EndIf                   
              EndIf
            EndIf
            *objectIa\Release()
          Else
            MessageRequester("Error","get_accRole Error ",#MB_OK|#MB_ICONERROR)
          EndIf
        EndIf
      EndIf
  EndSelect
EndProcedure

OpenWindow(0, 550, 450, 500, 500, "Get Browser URL", #PB_Window_MinimizeGadget)
ListViewGadget(0, 10, 10, 480, 480)

CoInitialize_(0)
hdll = OpenLibrary(0, "Oleacc.dll")
If hdll
  eHook = SetWinEventHook_(#EVENT_OBJECT_FOCUS, #EVENT_OBJECT_VALUECHANGE, #Null, @WinEventFunc(), 0, 0, #WINEVENT_OUTOFCONTEXT|#WINEVENT_SKIPOWNPROCESS)
  If eHook = 0
    MessageRequester("Error","Unsuccessful Hooking",#MB_OK|#MB_ICONERROR)
  EndIf
Else
  MessageRequester("Error","The DLL not loaded ",#MB_OK|#MB_ICONERROR)
EndIf
Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect
Until quit = 1
CoUninitialize_()
UnhookWinEvent_(eHook)
CloseLibrary(0)
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Firefox URL not detected on W10

Post by Kwai chang caine »

First thanks a lot at all For your interest 8)

@BarryG
You have right, alredy i was so surprised to find this code and see Windows can read in FF :shock:
Since Mozilla does everything not to use Microsoft elements and to be independent. :?
So, when i saw that the code didn't work, I did as you said, but it's still a bit crude with sendkeys and sometimes dangerous when it doesn't goes where it should :oops:
Furthermore, in my project i use a long press of left mouse button together, that works ...but it's not really good :oops:
And yet, for the moment this is the method I use :wink: , for lack of anything better.
But thanks to RASHAD now, it's not sure that it will be the only one :wink:

@RASHAD
It's a miracle !!!! that works now !!!
But the most surprising, it's the first code i gave works too :shock:
At my advice, it was my W10 who a problem, because i was forced to reinstal it this afternoon, and now all works
I don't know why that not worked before, like Axolotl :wink: but now that works and I fell on my ass, when I saw the power of this code :shock:
Because it even manages to read the text fields inside FF, I've never seen that 8)
Perhaps it can read other things on hover that would be madness for me, to spot certain skimmed texts :idea:

But for the moment....KCC !!! you must go drink some herbal tea and calm down :?
You have your URL reading, we know you, soon you will ask to drive completely FF :mrgreen:

One thousand of thanks !!!
And have a very good end of day for all 8)
ImageThe happiness is a road...
Not a destination
Post Reply