Results 1 to 40 of 555

Thread: VB6 WebView2-Binding (Edge-Chromium)

Threaded View

  1. #10

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: VB6 WebView2-Binding (Edge-Chromium)

    Quote Originally Posted by softv View Post
    ...When trying to add a .js file (say 'simple.js') in the above manner,
    I found that it was working only if there are no jQuery calls in my 'simple.js' file.
    With the now available browser-built-in selector-functions, there's rarely a need for jQuery anymore...

    That said - jQuery is an external js-lib - and therefore needs to be (pre)loaded as well -
    ideally before you add your own js-scripts.

    You can ensure that either in the HTML (via the usual <script>-tags) -
    or use AddScriptOnDocumentCreate as well for jQuery...

    Below is some Demo-Code:
    (which downloads jQuery dynamically "as String" via a http51-helperfunction - but once downloaded via wgetJQ(),
    you can of course put the retrieved string into your own local js-file - and load it from there in future-runs)...

    Code:
    Option Explicit
     
    Private WithEvents WV As cWebView2
     
    Private Sub Form_Load()
      Me.Visible = True
     
      Set WV = New_c.WebView2(hWnd)
          WV.AddScriptToExecuteOnDocumentCreated wgetJQ("https://code.jquery.com/jquery-3.6.4.js")
          WV.AddScriptToExecuteOnDocumentCreated "function setColor_SelectorBased(sel,clr){" & _
                                                 "   $(sel).each(function(){$(this).css('background-color',clr)})" & _
                                                 "}"
      
      WV.NavigateToString "<p>first Para</p><p>second Para</p><p>last Para</p>"
      
      WV.jsRun "setColor_SelectorBased", "p:first, p:last", "magenta"
    End Sub
    
    Public Function wgetJQ(URL) As String
      With CreateObject("WinHttp.WinHttpRequest.5.1")
          .open "GET", URL: .send: wgetJQ = .responseText
          
          'hot-fixes for two edge/chromium-related bugs in the current jQuery-release
          wgetJQ = Replace(wgetJQ, "el = document", "el = window.document")
          wgetJQ = Replace(wgetJQ, "documentElement.getRootNode", "documentElement && documentElement.getRootNode")
      End With
    End Function
    If all works well, the first and the last of the 3 <p>-Tags should be colored magenta.

    Edit: Seems, that the WebView2-method: AddScriptToExecuteOnDocumentCreated -
    does not live up to its "second symbolname-part" anymore...
    (hope, they are fixing this soon)...

    In the interim, you can enclose all such added scripts with your own js-event-listener:
    (example is based on the working one above, but now not even the "jQuery-hotfixes" are needed anymore,
    when the script-adding is delayed until the document "is sitting properly in the DOM")
    Code:
    Option Explicit
     
    Private WithEvents WV As cWebView2
     
    Private Sub Form_Load()
      Me.Visible = True
     
      Set WV = New_c.WebView2(hWnd)
      With New_c.StringBuilder
          .AddNL wget("https://code.jquery.com/jquery-3.6.4.js")
     
          .AddNL "$('p:first, p:last').each(function(){$(this).css('background-color','magenta')})"
       
          WV.AddScriptToExecuteOnDocumentCreated "window.addEventListener('load',(e)=>{" & .ToString & "})"
      End With
     
      WV.NavigateToString "<p>first Para</p><p>second Para</p><p>last Para</p>"
    End Sub
    
    Public Function wget(URL) As String 'generic text-retrieval "by URL"
      With CreateObject("WinHttp.WinHttpRequest.5.1")
          .open "GET", URL: .send: wget = .responseText
      End With
    End Function
    Olaf
    Last edited by Schmidt; Mar 24th, 2023 at 08:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width