Page 1 of 1
WebGadget: Fetch Page Title
Posted: Mon Mar 17, 2025 7:44 pm
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
Re: WebGadget: Fetch Page Title
Posted: Mon Mar 17, 2025 9:40 pm
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
Re: WebGadget: Fetch Page Title
Posted: Tue Mar 25, 2025 9:41 pm
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.
Re: WebGadget: Fetch Page Title
Posted: Wed Mar 26, 2025 1:59 pm
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.
Re: WebGadget: Fetch Page Title
Posted: Wed Mar 26, 2025 9:36 pm
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?
Re: WebGadget: Fetch Page Title
Posted: Thu Mar 27, 2025 9:33 pm
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
Re: WebGadget: Fetch Page Title
Posted: Sun Mar 30, 2025 10:37 am
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?
Re: WebGadget: Fetch Page Title
Posted: Sun Mar 30, 2025 2:52 pm
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
! });
Re: WebGadget: Fetch Page Title
Posted: Mon Mar 31, 2025 6:59 pm
by Techmas
It works, thanks.