Detect WebGadget URL change?

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Detect WebGadget URL change?

Post by BarryG »

I searched the forum for this but couldn't find anything. I have a local set of HTML files in a WebGadget, and I need to detect when the user has clicked a link that changes the URL to the new link. You'd think this would be easy, right?

This fails (despite my HTML files having valid <title></title> tags):

Code: Select all

If OpenWindow(0, 200, 200, 800, 600, "WebGadget", #PB_Window_SystemMenu)
  WebGadget(0, 10, 10, 780, 580, "file://index.html")
  Repeat
    Event = WaitWindowEvent()
    If EventType() = #PB_EventType_TitleChange
      Debug "changed" ; Never seen!
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
And manually checking if the URL changed doesn't work until the user moves the mouse over the WebGadget:

Code: Select all

If OpenWindow(0, 200, 200, 800, 600, "WebGadget", #PB_Window_SystemMenu)
  WebGadget(0, 10, 10, 780, 580, "file://index.html")
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      If weburl$<>oldweburl$
        oldweburl$=weburl$
        Debug "changed" ; User has to move the mouse after clicking a link to see this!
      EndIf
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
So is there an easy way to detect the page URL changing? Why is this so hard? Thanks.

(PS. Why isn't there a simple #PB_EventType_UrlChange function to detect this?).
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Detect WebGadget URL change?

Post by Bisonte »

Windows only:
PB Help wrote:

Code: Select all

Procedure NavigationCallback(Gadget, Url$)    ; Windows only!
    If Url$= "https://www.purebasic.com/news.php" 
      MessageRequester("", "No news today!") 
      ProcedureReturn #False 
    Else 
      ProcedureReturn #True 
    EndIf 
  EndProcedure 
  
  If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
    WebGadget(0, 10, 10, 580, 280, "https://www.purebasic.com") 
    SetGadgetAttribute(0, #PB_Web_NavigationCallback, @NavigationCallback())
    Repeat 
    Until WaitWindowEvent() = #PB_Event_CloseWindow 
  EndIf
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Detect WebGadget URL change?

Post by BarryG »

So is a callback really the only way to do this? Seems overly complicated. Why does the event type of #PB_EventType_TitleChange not do it? What title is it looking for, since it's obviously ignoring my pages' <title></title> tags?

Also, it doesn't work with enumerated gadget numbers (which my app uses). Why is it so very complicated just to detect a changed URL...

Code: Select all

Enumeration
  #web
EndEnumeration

Procedure NavigationCallback(#web, Url$) ; Syntax error here! :(
  If Url$= "https://www.purebasic.com/news.php" 
    MessageRequester("", "No news today!") 
  EndIf 
  ProcedureReturn #True
EndProcedure 

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  WebGadget(#web, 10, 10, 580, 280, "https://www.purebasic.com") 
  SetGadgetAttribute(#web, #PB_Web_NavigationCallback, @NavigationCallback())
  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Detect WebGadget URL change?

Post by wombats »

I wouldn't rely on the title changing because what if the new page happens to have the same title?

Change #web in NavigationCallback() back to Gadget and it'll work. Use Gadget to determine which gadget the event came from.
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Detect WebGadget URL change?

Post by BarryG »

wombats wrote:I wouldn't rely on the title changing because what if the new page happens to have the same title?
They won't; they're my local HTML files (actually the manual for my app). I'd love to know what triggers the #PB_EventType_TitleChange event though, because it doesn't work for me in my example above.

I'm resigned to using the callback now, but I still think it should be made easier.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Detect WebGadget URL change?

Post by JHPJHP »

Removed; post ignored.
Last edited by JHPJHP on Tue Jun 09, 2020 2:07 am, edited 5 times in total.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Detect WebGadget URL change?

Post by IdeasVacuum »

Hi BarryG

In your sample code, the Event isn't bound to the Web Gadget. Try this:

Code: Select all

Enumeration
#Win
#Web
EndEnumeration

If OpenWindow(#Win, 200, 200, 800, 600, "WebGadget", #PB_Window_SystemMenu)

         WebGadget(#Web, 10, 10, 780, 580, "file://index.html")
 
         Repeat
                      iEvent = WaitWindowEvent()
               Select iEvent
         
                         Case #PB_Event_Gadget
         
                                 Select EventGadget
                                 
                                        Case #Web
                                 
                                                If(EventType() = #PB_EventType_TitleChange)
                                 
                                                          Debug "changed" ; Never seen!
                                                EndIf
                                 EndSelect
               EndSelect

         Until iEvent = #PB_Event_CloseWindow
EndIf
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Detect WebGadget URL change?

Post by helpy »

You can use the event types #PB_EventType_DownloadStart and #PB_EventType_DownloadEnd to detect if loading of a url starts and ends.

I would use #PB_EventType_DownloadEnd as trigger:
==> Read the current url (with GetGadgetText()) and compare it to the last loaded page url.
This way you can detect a new url!
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Post Reply