How to get a JavaScript variable on a WebViewGadget from PB procedure?

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to get a JavaScript variable on a WebViewGadget from PB procedure?

Post by infratec »

Code: Select all

#PB_Event_DataReceived
Is not a custom event.
It is already defined and I simply missused for this.
If I run

Code: Select all

Debug #PB_Event_DataReceived
It shows 2 (PB WIndows 6.20b3)

But I can not find out where it comes from.
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to get a JavaScript variable on a WebViewGadget from PB procedure?

Post by mk-soft »

Not exists on macOS and Linux ...
Better #MyEvent_DataReceived

Code: Select all

EnableExplicit

Enumeration CustomEvent #PB_Event_FirstCustomValue
  #MyEvent_DataReceived
EndEnumeration

; ----

Procedure AllocateString(String.s) ; Result = Pointer
  Protected *mem.string = AllocateStructure(String)
  If *mem
    *mem\s = String
  EndIf
  ProcedureReturn *mem
EndProcedure

Procedure.s FreeString(*mem.string) ; Result String
  Protected r1.s
  If *mem
    r1 = *mem\s
    FreeStructure(*mem)
  EndIf
  ProcedureReturn r1
EndProcedure

; ----

Procedure GetDataJS(JsonParameters$)
  
  PostEvent(#MyEvent_DataReceived, EventWindow(), EventGadget(), 0, AllocateString(JsonParameters$))
  
  ProcedureReturn #Null
  
EndProcedure

; ----

Define Html$, Result$

Html$ = ~"<input type=\"text\" id=\"userInput\" name=\"userInput\" value=\"Hello World\">\n"

OpenWindow(0, 100, 100, 400, 250, "Hello", #PB_Window_SystemMenu)

WebViewGadget(0, 0, 0, 400, 200, #PB_WebView_Debug)
SetGadgetItemText(0, #PB_WebView_HtmlCode, Html$)
ButtonGadget(1, 10, 210, 120, 25, "Get Data")
BindWebViewCallback(0, "getData", @GetDataJS())

Repeat 
  Select WaitWindowEvent()
    Case #MyEvent_DataReceived
      Result$ = FreeString(EventData())
      Debug Result$
      
    Case #PB_Event_Gadget
      If EventGadget() = 1
        WebViewExecuteScript(0, ~"window.getData(\"userInput\", document.getElementById(\"userInput\").value)")
      EndIf
      
    Case #PB_Event_CloseWindow
      Break
      
  EndSelect
ForEver
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
noxidderf
User
User
Posts: 18
Joined: Thu Oct 03, 2024 5:31 pm

Re: How to get a JavaScript variable on a WebViewGadget from PB procedure?

Post by noxidderf »

mk-soft wrote: Sun Jan 26, 2025 8:01 pm Not exists on macOS and Linux ...
Better #MyEvent_DataReceived

Code: Select all

EnableExplicit

Enumeration CustomEvent #PB_Event_FirstCustomValue
  #MyEvent_DataReceived
EndEnumeration

; ----

Procedure AllocateString(String.s) ; Result = Pointer
  Protected *mem.string = AllocateStructure(String)
  If *mem
    *mem\s = String
  EndIf
  ProcedureReturn *mem
EndProcedure

Procedure.s FreeString(*mem.string) ; Result String
  Protected r1.s
  If *mem
    r1 = *mem\s
    FreeStructure(*mem)
  EndIf
  ProcedureReturn r1
EndProcedure

; ----

Procedure GetDataJS(JsonParameters$)
  
  PostEvent(#MyEvent_DataReceived, EventWindow(), EventGadget(), 0, AllocateString(JsonParameters$))
  
  ProcedureReturn #Null
  
EndProcedure

; ----

Define Html$, Result$

Html$ = ~"<input type=\"text\" id=\"userInput\" name=\"userInput\" value=\"Hello World\">\n"

OpenWindow(0, 100, 100, 400, 250, "Hello", #PB_Window_SystemMenu)

WebViewGadget(0, 0, 0, 400, 200, #PB_WebView_Debug)
SetGadgetItemText(0, #PB_WebView_HtmlCode, Html$)
ButtonGadget(1, 10, 210, 120, 25, "Get Data")
BindWebViewCallback(0, "getData", @GetDataJS())

Repeat 
  Select WaitWindowEvent()
    Case #MyEvent_DataReceived
      Result$ = FreeString(EventData())
      Debug Result$
      
    Case #PB_Event_Gadget
      If EventGadget() = 1
        WebViewExecuteScript(0, ~"window.getData(\"userInput\", document.getElementById(\"userInput\").value)")
      EndIf
      
    Case #PB_Event_CloseWindow
      Break
      
  EndSelect
ForEver

I was looking for WebView Information so I tested all these on this post on Linux.
I am up late and didnt notice the example here said Not for Mac or Linux
I had already run iit on pop-os (basically ubuntu).
Just nothing that this example work on Linux.
Pop!_OS 22.04 LTS x86_64
gnome 42.9
PB 6.20
Post Reply