Page 1 of 1

WebViewGadget for do a search using Google

Posted: Mon Aug 05, 2024 4:24 pm
by ricardo
Hi,

Im just testing WevViewGadget and i tried to do a simple test: write a seacrh ternm in Google and make a search, using javascript.

I tried

Code: Select all

WebViewExecuteScript(0,~"document.querySelector('input[name=\"q\"]').value = \"purebasic\"")
But it does not work.

I am just trying to run this action using javascript:

Code: Select all

document.querySelector('input[name="q"]').value = "ai";
document.querySelector('input[name="btnK"]').click();
Thanks

Re: WebViewGadget for do a search using Google

Posted: Mon Aug 05, 2024 7:23 pm
by Kiffi
You have to wait until the page has loaded completely. Only then can you perform the search.

Code: Select all

EnableExplicit

#Window = 0
#WebViewGadget = 0

Procedure DocumentIsLoaded(JsonParameters.s)
  
  WebViewExecuteScript(#WebViewGadget, ~"document.querySelector('textarea').value = \"purebasic\"")
  WebViewExecuteScript(#WebViewGadget, ~"document.querySelector('input[name=\"btnK\"]').click();")
  
  ProcedureReturn UTF8(~"")
  
EndProcedure

OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 1000, 800, "Hello", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebViewGadget(#WebViewGadget, 0, 0, 1000, 800, #PB_WebView_Debug)

BindWebViewCallback(#WebViewGadget, "DocumentIsLoaded", @DocumentIsLoaded())

SetGadgetText(#WebViewGadget, "https://www.google.com/")

WebViewExecuteScript(#WebViewGadget, "document.onreadystatechange = function () {" + 
                        "  if (document.readyState == 'interactive') {" + 
                        "    DocumentIsLoaded();" + 
                        "  }" + 
                        "}")

Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: WebViewGadget for do a search using Google

Posted: Mon Aug 05, 2024 8:10 pm
by ricardo
Kiffi wrote: Mon Aug 05, 2024 7:23 pm You have to wait until the page has loaded completely. Only then can you perform the search.

Code: Select all

EnableExplicit

#Window = 0
#WebViewGadget = 0

Procedure DocumentIsLoaded(JsonParameters.s)
  
  WebViewExecuteScript(#WebViewGadget, ~"document.querySelector('textarea').value = \"purebasic\"")
  WebViewExecuteScript(#WebViewGadget, ~"document.querySelector('input[name=\"btnK\"]').click();")
  
  ProcedureReturn UTF8(~"")
  
EndProcedure

OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 1000, 800, "Hello", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebViewGadget(#WebViewGadget, 0, 0, 1000, 800, #PB_WebView_Debug)

BindWebViewCallback(#WebViewGadget, "DocumentIsLoaded", @DocumentIsLoaded())

SetGadgetText(#WebViewGadget, "https://www.google.com/")

WebViewExecuteScript(#WebViewGadget, "document.onreadystatechange = function () {" + 
                        "  if (document.readyState == 'interactive') {" + 
                        "    DocumentIsLoaded();" + 
                        "  }" + 
                        "}")

Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
Really thanks. !!!