WebGadget: Fetch Page Title

Just starting out? Need help? Post your questions and find answers here.
User avatar
Techmas
Posts: 5
Joined: Mon Mar 17, 2025 10:35 am

WebGadget: Fetch Page Title

Post by Techmas »

Hi there,

I'm trying to fetch the title of the webpage that is displayed in the WebGadget (like it is possible in PureBasic) but GetGadgetItemText(0,#PB_Web_PageTitle) is returning 'undefined' (see below example).

Can somebody help me out with this?

Thanks!

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebView", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  WebGadget(0, 0, 0, 600, 300,"https://www.spiderbasic.com/")
  Debug GetGadgetItemText(0,#PB_Web_PageTitle)
EndIf
plouf
Posts: 295
Joined: Tue Feb 25, 2014 6:01 pm
Location: Athens,Greece

Re: WebGadget: Fetch Page Title

Post by plouf »

are you in the same domain ?
i.e upload you script to techmas.com/script AND requesting a techmas.com/xxx page ?

because thats the only that works due to CORS
Christos
User avatar
Techmas
Posts: 5
Joined: Mon Mar 17, 2025 10:35 am

Re: WebGadget: Fetch Page Title

Post by Techmas »

Thanks for your reply!

Yes I am.
I suspect, it tries to get the title before the page is fully loaded... How can I check whether the page is loaded or not?

In PureBasic I use the following loop to wait until the title is there:

Code: Select all

While Len(GetGadgetItemText(0, #PB_Web_PageTitle)) = 0
	WindowEvent()
Wend
...but I couldn't figure out how it can be done here.

I'm grateful for any advice.
User avatar
Caronte3D
Posts: 189
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: WebGadget: Fetch Page Title

Post by Caronte3D »

Not a time problem, I think It's a browser limitation (CORS) as plouf pointed.
You can run the code in the web browser and push F12 to see the error in the developer Console.
User avatar
Techmas
Posts: 5
Joined: Mon Mar 17, 2025 10:35 am

Re: WebGadget: Fetch Page Title

Post by Techmas »

No errors are thrown in the dev console but I made the following observation:

The following code shows 'undefined' in the TextGadget...

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebView", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If WebGadget(0, 0, 25, 600, 275,"https://www.blablah.com/index.html")
    TextGadget(#PB_Any,0,0,600,25,GetGadgetItemText(0,#PB_Web_PageTitle))
  EndIf
EndIf
...whereas the following code shows 'undefined' in the MessageRequester and the actual page title in the TextGadget, which means the title CAN be fetched.

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebView", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If WebGadget(0, 0, 25, 600, 275,"https://www.blablah.com/index.html")
  
    MessageRequester(GetGadgetItemText(0,#PB_Web_PageTitle))
    
    TextGadget(#PB_Any,0,0,600,25,GetGadgetItemText(0,#PB_Web_PageTitle))
  EndIf
EndIf
The title can be fetched if interrupted by the message requester... but i hope there is a better solution for this. Any other ideas?
User avatar
Caronte3D
Posts: 189
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: WebGadget: Fetch Page Title

Post by Caronte3D »

Try to do a pause:

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebView", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If WebGadget(0, 0, 25, 600, 275,"https://www.blablah.com/index.html")  
   ! setTimeout(() => {    
    TextGadget(#PB_Any,0,0,600,25,GetGadgetItemText(0,#PB_Web_PageTitle))
   ! }, 2000);  
  EndIf
EndIf
User avatar
Techmas
Posts: 5
Joined: Mon Mar 17, 2025 10:35 am

Re: WebGadget: Fetch Page Title

Post by Techmas »

It works with the suggested pause, but possibly not always.

I doubt the reliability as the required pause can be different for each run. It may work, but may fail if loading takes longer than expected.
How to find the min. required pause? How to make it fail-safe?
User avatar
Caronte3D
Posts: 189
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: WebGadget: Fetch Page Title

Post by Caronte3D »

Instead of a pause, try to wait until everything is loaded:

Code: Select all

! window.onload = function() {
    ! console.log("WebPage loaded");
    ! // Your code here
! };
Or:

Code: Select all

! document.addEventListener("DOMContentLoaded", function() {
    ! console.log("DOM loaded");
    ! // Your code here
! });
User avatar
Techmas
Posts: 5
Joined: Mon Mar 17, 2025 10:35 am

Re: WebGadget: Fetch Page Title

Post by Techmas »

It works, thanks.
Post Reply