Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Javascript Vars
#2
Hi Maddire,

You can’t read the values of whereyouat and lengthOfVideo via the Document interface functions OnRequestElementValueById and OnRequestElementValueByName methods as they are javascript variables, not forms.
Those methods are specificly for reading from form data.
There’s no document interface for reading out global variables.

That doesn’t mean you can’t get at that data, but that you should use another method.
Initially I was thinking via webmessage, but that means you would need a user actions such as a click on a button and it seems you want to read out a status.
So in that case you need a bit of custom javascript and run that via anonymous script (actually you can also use executescript, but the anonymous script has some advantages over passing variables and such)

Here’s an example I wrote for a customer, but translated into VB.

Code:
Private Sub readAppStyleVariable()
  Dim Script As String
  Dim Params As Variant
 
  Script = " () => {" & vbCrLf
  Script = Script & "  let appStyle = '';" & vbCrLf
  Script = Script & "  let app = document.getElementById('cip-hosted-payments-app');" & vbCrLf
  Script = Script & "  if (app !== null) {" & vbCrLf
  Script = Script & "    appStyle = app.style.display;" & vbCrLf
  Script = Script & "    if (appStyle !== null) {" & vbCrLf
  Script = Script & "        return appStyle;" & vbCrLf
  Script = Script & "    } else {" & vbCrLf
  Script = Script & "      return 'null';" & vbCrLf
  Script = Script & "    }" & vbCrLf
  Script = Script & "  } else {" & vbCrLf
  Script = Script & "    return '1';" & vbCrLf
  Script = Script & "  }" & vbCrLf
  Script = Script & "}" & vbCrLf
  ' connect document
  Document.BrowserDispatch (EdgeWebBrowser.Dispatch)
  ' run the above as anonymous function 1
  Document.RunAnonymousFunction 1, Params, Script
End Sub
And you then get the results like this:
Code:
Private Sub Document_OnRunAnonymousFunction(ByVal Id As Long, ByVal Params As Variant, ByVal Data As String, ByVal Error As Long)
  If Id = 1 Then
    Debug.Print "AppStyle = " & Data
  End If
End Sub

So if I would apply that to your example code then it could look like:
Code:
Private Sub readPlayerCurrentTime()
  Dim Script As String
  Dim Params As Variant
 
  Script = " () => {" & vbCrLf
  Script = Script & "  let playerTime = '';" & vbCrLf
  Script = Script & "  let app = document.getElementById('player');" & vbCrLf
  Script = Script & "  if (app !== null) {" & vbCrLf
  Script = Script & "    playerTime = app.currentTime();" & vbCrLf
  Script = Script & "    if (playerTime !== null) {" & vbCrLf
  Script = Script & "        return playerTime;" & vbCrLf
  Script = Script & "    } else {" & vbCrLf
  Script = Script & "      return 'null';" & vbCrLf
  Script = Script & "    }" & vbCrLf
  Script = Script & "  } else {" & vbCrLf
  Script = Script & "    return '1';" & vbCrLf
  Script = Script & "  }" & vbCrLf
  Script = Script & "}" & vbCrLf
  ' connect document
  Document.BrowserDispatch (EdgeWebBrowser.Dispatch)
  ' run the above as anonymous function 2
  Document.RunAnonymousFunction 2, Params, Script
End Sub
and
Code:
Private Sub Document_OnRunAnonymousFunction(ByVal Id As Long, ByVal Params As Variant, ByVal Data As String, ByVal Error As Long)
  If Id = 1 Then
    Debug.Print "AppStyle = " & Data
  End If
  If Id = 2 Then
    Debug.Print "PlayerTime = " & Data
  End If
End Sub

Attached is the test code I used (which doesn't have your control, but it does fire the event and returns "1" because it couldn't find the player.)

Hope this helps,
--
Wil


Attached Files
.zip   frmDocument.zip (Size: 2.72 KB / Downloads: 1)
Reply


Messages In This Thread
Getting Javascript Vars - by maddire - 2022-04-20, 09:50:40
RE: Getting Javascript Vars - by wila - 2022-04-20, 10:37:47
RE: Getting Javascript Vars - by maddire - 2022-04-20, 10:59:58
RE: Getting Javascript Vars - by wila - 2022-04-20, 11:33:07
RE: Getting Javascript Vars - by maddire - 2022-04-20, 13:43:25
RE: Getting Javascript Vars - by wila - 2022-04-20, 17:12:35
RE: Getting Javascript Vars - by maddire - 2022-04-20, 18:30:49
RE: Getting Javascript Vars - by maddire - 2022-04-22, 18:20:58
RE: Getting Javascript Vars - by wila - 2022-04-25, 08:40:43

Forum Jump:


Users browsing this thread: 1 Guest(s)