项目作者: ukandrewc

项目描述 :
Experimental DOM implementation for MS Winforms WebView2
高级语言:
项目地址: git://github.com/ukandrewc/Webview2.Winforms.DOM.git
创建时间: 2020-05-15T14:47:41Z
项目社区:https://github.com/ukandrewc/Webview2.Winforms.DOM

开源协议:

下载


Webview2.Winforms.DOM

Experimental DOM implementation for Microsoft.Webview2.Winforms

An implementation for a WebView2 DOM, that can be accessed from .Net

Note

You should be aware that Microsoft can use WebView2 to gather data from the end user, exactly what data they gather and what they do with it is currently unknown.

DOM access

It implements Window, Document, Element, Attribute, Style and Node.
Also implements QuerySelector/QuerySelectorAll and Evaluate/EvaluateAll (XPath) on WebView2

You can perform most operations on the DOM, e.g:

  1. Document.GetElementById("elem_id").InsertAdjacentHTML("afterbegin", "<div>Content</div>")
  2. Dim x = Document.Evaluate("//input[@id='form_input']")

Event handling

Because script cannot be executed during a synchronous event, two events are raised, one Sync, and one Async.
The Sync event is raised first. It can prevent default handling in javascript and the Async event in WVBrowser.

To make the decision to cancel, an array of script to evaluate, is passed into AddEventHandler, and the results passed to the Sync event.

Add the event handler to browser

  1. Private WithEvents Body As WVHTMLElement
  2. Private Sub Web_DOMDocumentComplete(sender As Object) Handles Web.DOMDocumentComplete
  3. 'Add click event handler, pass script to return: e.target.tagName, e.x, e.y to Sync event
  4. Body = Web.document.body
  5. Body.AddEventHandler("click", "e.target.tagName", "e.x", "e.y")
  6. End Sub
  7. 'Can't execute script or use DOM in Sync event
  8. Private Sub Body_DOMEventSync(Type As String, ByRef e As WVEventHost.SyncEventArgs) Handles Body.DOMEventSync
  9. 'Prevent default if BODY
  10. e.PreventDefault = e.Values(0) = "BODY"
  11. 'Prevent Async if not BODY
  12. e.PreventAsync = e.Values(0) <> "BODY"
  13. End Sub
  14. 'Can execute script and call DOM in Async event
  15. Private Sub Body_DOMEventAsync(Type As String, e As WVEvent) Handles Body.DOMEventAsync
  16. 'Get BODY InnerHTML
  17. If Type = "click" Then
  18. Dim h = Body.InnerHTML
  19. End If
  20. End Sub

Add the event handler to element

  1. Private WithEvents Div as WVElement
  2. Private Sub Web_DOMDocumentComplete(sender As Object) Handles Web.DOMDocumentComplete
  3. Div = Web.Document.GetElementById("myElement")
  4. 'Add click event handler
  5. Div.AddEventHandler("click")
  6. End Sub
  7. Private Sub Div_DOMEventAsync(Type As String, e As WVEvent) Handles Div.DOMEventAsync
  8. 'Process event
  9. End Sub

Support for DevTools Protocol with events

Note: WebView2.Protocol.GetCookies({"https://google.co.uk"}) needs to supply full url

  1. Private Sub WebView2_CoreWebView2Ready(sender As Object, e As EventArgs) Handles WebView2.CoreWebView2Ready
  2. WebView2.CoreWebView2.Navigate("https://google.co.uk")
  3. With WebView2.Protocol
  4. .EnableDomainEvents("Page", True)
  5. .EnableDomainEvents("Log", True)
  6. .SubscribeEvent("Page.loadEventFired")
  7. .SubscribeEvent("Page.windowOpen")
  8. .SubscribeEvent("Log.entryAdded")
  9. AddHandler .EventReceived, AddressOf Protocol_EventRaised
  10. End With
  11. End Sub
  12. Private Sub Protocol_EventRaised(Name As String, Json As String)
  13. Console.WriteLine(Name)
  14. End Sub

Updated for pre-release 0.9.628

Added sample project with tabbed browser

Added download notifications using DevTools Protocol

Added ability to use CorewebView2Environment that doesn’t require Async

  1. Dim Env = CoreWebView2Environment.CreateAsync("", "My Folder", MyOptions).Await
  2. WVBrowser.EnsureWebView2Async(Env).Await

Major update with breaking changes

Default DOM events have changed and now need to be added using AddEventHandler as described above

Added support for accessing iFrames, including FrameLoaded and FrameCompleted events

Updated for pre-release 1.0.674.0

Added specific WVIFrameElement class for iFrames
Any function that returns a WVElement, will now return a WVIFrameElement, when the element is an iFrame

Updated for pre-release 1.0.707.0

Added more specific types for TABLE, FORM and elements that have special functionality

WVTable, WVTableSection, WVTableRow, WVTableCell | WVForm, WVinput | WVAnchor

Added DeferReflow() and ApplyReflow() to defer page reflow when updating:

  1. Dim Table = Web.Document.Evaluate("//table")
  2. 'Defer reflow
  3. Table.DeferReflow()
  4. For Each Row in Table.TBodies(0).Rows
  5. For Each Cel in Row.Cells
  6. Cel.Style("borderLeft") = $"4px solid {Choose(1 + Rnd() * 5, "red", "green", "yellow", "blue", "orange")}"
  7. Next
  8. Next
  9. 'Apply update
  10. Table.ApplyReflow()

Updated for pre-release 1.0.824.0

Started adding support for Selection and Range

Removed internal Autofill implementation pending WebView2 API.

Breaking Changes

Updated DOM structure to more closely follow JS DOM.

WVDOMBase renamed WVElement, WVElement renamed WVHTMLElement

TabStrip renamed WVTabstrip, NavStrip renamed WVNavstrip

Changed version to follow WebView2

The version of WVBrowser will now reflect the version of WebView2, it works with.

Changed names of properties and methods to match JS camelCase.

Finished adding Selection and Range support.

Breaking Change - Event Support

Removed event support on WVBrowser, it is now only available for WVElement and WVHTMLElement.
To add events you need to add a WVElement or WVHTMLElement reference and use that, e.g.

  1. Private WithEvents Body As WVElement
  2. Private Sub WvBrowser1_DOMDocumentComplete(sender As Object) Handles WvBrowser1.DOMDocumentComplete
  3. Body = WvBrowser1.Document.body
  4. Body.AddEventHandler("mouseover")
  5. Body.AddEventHandler("mouseup")
  6. End Sub
  7. Private Sub Body_DOMEventAsync(Type As String, e As WVEvent) Handles Body.DOMEventAsync
  8. If Type = "mouseover" Then
  9. ElemLabel.Text = WvBrowser1.document.elementFromPoint(New Point(e.pageX, e.pageY)).tagName
  10. End If
  11. End Sub

Added Await extension for Async Tasks

There is now an Await extension for all Tasks, so you can call Async methods Synchronously, without needing to use Async and Await, e.g

  1. WvBrowser1.EnsureCoreWebView2Async().Await

Update 0.0.865.0

No major changes, just released against WebView2 1.0865.0

Update 0.0.865.1

Added: functionality to WVWindow

Fixed: Issues with Boolean properties

Update 0.0.865.2

Fixed: Issue with missing DLL in previous version

Added: JSHelper extensions to WVBrowser