New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coming from Webbrowser , Winforms DOM Document Access in Webview2 Clearing of Cookies Inprivate #176
Comments
There isn't a DOM (Document) implementation for WebView2, but I am working on one here. |
There is no native way to access DOM objects from Winforms app ? |
Create a .Net DOM framework and update it from JS. |
The documentation does not explain how to get values out of the control into .net. I mean this is the most basic functionality of a webbrowser control ? How is this not explained in depth in the documentation? |
It is far more flexible and cross browser/platform and more secure to do it in JS. |
Hi @igotbass, thanks for these great questions! Looks like we need some documentation regarding how to interact with the DOM. @igotbass, Regarding interacting with the DOM, yes what @ukandrewc is true. You can use ExecuteScript to execute script and you'll get back the result of that script as JSON. Or you can use PostWebMessageAsJson/chrome.webview.addEventListener('message',...) to send messages into the document and chrome.webview.postMessage/WebMessageReceived to send messages from the document back to the host app. Regarding clearing cookies or other state, that's a great feature request which we don't support yet. To get a fresh browser session, when you create the CoreWebView2Environment you specify the user data folder. The user data folder holds all state including cookies, http cache, and so on. If you delete the user data folder or specify a new user data folder you will get a fresh environment. |
So if I want something as simple as the oldschool webbrowser.document.getelementbyid("xxx").value its now a massive process to get a element value ? |
I suppose that's part of the point, this is newschool ;-) but if you use this version that implements a DOM. It is still a work in progress, so don't expect too much, but you can access most of Window, Document, Element, Attribute, Style and Node. |
Ok thanks I will check it out. |
I just tried @ukandrewc browser and this is the functionality that MUST be built into webview2 from MS, not exactly sure how a 3rd party has managed to put this together but we should not have to inject javascript to get a simple value of a element. Webbrowser control 1 line of code string TheValue = webbrowser1.document.getelementbyid("xxx").value Now its setting up and receiving JSON messages ect, guys this should be basic functionality of the control, hands down. Thanks |
Ones and zeros, that's all there is to it. At least until we have quantum processing, then it will be ones and or possibly maybe zeros (ah now you've looked at it, so we'll have to start again ;-) |
100% agree that having DOM access without having to do a bunch of Javascript injecting is the right way to do it. I hope they incorporate what @ukandrewc has created. |
I need to have the touch keyboard popping up when a user tap (or mouse click) an input field. This is automatically performed when the computer has no physical keyboard, or when it's a slate configured in tablet mode. But since it's not always the case, i had to do the following with my former WebBrowser control :
And also hooking mouse clicks :
Question is : since no easy DOM with WebView2, is there any way of getting the html tag at click/tab coords without having to exec some JS (could probably be very intensive at each click/tap) ? |
This experimental DOM allows that: https://github.com/ukandrewc/Webview2.Winforms.DOM |
@ukandrewc thank you ! I will check this out |
I am in complete agreement with the person who initiated this thread. How can Microsoft release WebView2 without an equivalent function to "WebBrowser.Document"?? This is like GM producing a car without seats and expecting each consumer to implement their own custom seat solution. Some of us are not expert Java Script developers. We all will waste a lot of time trying to reproduce the basic functionality we enjoyed in WebBrowser. I for one spent the last three days trying to find a solution with no luck. My searches on the internet show me that I am not alone. I would like to request that someone provide a clear and concise example that illustrates how we would duplicate the WebServer.Document functionality. This should include any needed Java Script code, as well as, code for decrypting the data using the new System.Text.JSON libraries. I included my working source below. This is a wrapper class for interfacing with WebView2. I would appreciate any comments/suggestions you might have. 'Option Explicit On Imports Microsoft.Web.WebView2.Core ' This class serves as a wrapper for the Microsoft Edge browser.
"Document Title|" & m_browser.CoreWebView2.DocumentTitle & vbCrLf &
End Class |
@GoncerAl I do appreciate your pain, but there is no direct way to access the DOM from .Net. Here's a generic example to access Javascript, and wait for the result, that I use in this control.
|
@GoncerAl If you use the control, I have created, it will give you what you need. |
Oops, no DOM access anymore? (Last time I used this was with the IE-based control. I only come back to this when there's a proper HTML engine behind this, what WebView2 seems to be.) I need to disable JavaScript on the content for security reasons. How can I edit and manipulate the HTML content then? Is this impossible? If I wanted to use JavaScript to manipulate the HTML on the page, I'd be using Electron or another framework that lets me write my application in JavaScript altogether. |
No DOM - really??? 😭 😭 😭 |
There is an experimental one for .Net here: https://github.com/ukandrewc/Webview2.Winforms.DOM |
DOMClickEvent? |
Might be late but how can I use this? |
I don't think a ton of JavaScript knowledge is necessary. Break it down to the very simplest things you need JS to do (e.g. get an element, write to an element). I would probably just write a few small JavaScript wrapper functions that allow for reading and writing from the page and then I would leverage other libraries like HtmlAgilityPack if I wanted to use C# instead of JavaScript inspecting the DOM. As purely thrown together example here I load in a copy of jQuery slim, use it to select an element by ID and then return an Again, I understand this is limited in the sense that, it's a snapshot in time but I'm mainly sharing as a way to show different ways you can interact with the page.
|
Using this control, you get direct .Net access to the DOM: https://github.com/ukandrewc/Webview2.Winforms.DOM If you have any questions about using the control, please post in issues for the control, Rather than here which is for pure WebView2 |
Ugh. Ok... Rant ON
Reading the DOM is no more or less secure than JS injection, but it IS vastly easier to do and for many people the entire goal is just to READ the content, not programmatically change it. Sorry for sounding pissy, but I've been trying literally for weeks to find a sane solution to what should be a simple problem that WebBrowser actually could do except that Microsoft permanently glued it to IE11 and so can't run a lot of (wait for it) JavaScript routines that aren't properly formed and then they abandoned it. If they just took that control and updated it to use the WebView 2 engine - problem solved (And on that - really - we have to install a freaking second browser engine to make this work? They couldn't have made this use the Edge browser's engine to handle rendering? I mean crappy IE could do it...). It's cool that someone found a hacky way around this and it kinda works (I mean - come ON - injecting JS and then using Win32's messaging system to get the results back and you seriously brought up cross-platform compatibility?) but this is kinda sad, even by open source standards - and this isn't even open source! (If it were, I'd go fix this.) There. Rant OFF. Feel much better. Now, pardon me while I go bang my head against CEFSharp for a while. It's a cannon to swat a fly but at least it's massively feature complete. |
@TheWerewolf Hope you don't mind, but couldn't resist ;-)
One day we'll look back and consider pre webView2 as the good old days. While they were actually worse, nostalgia will make them seem better. It is possible to create a DOM wrapper that gives you good access to the DOM. I'd offer you mine, but it's Winforms and VB.Net, so you'd hate it ;-) |
@TheWerewolf |
Just wanted to give a thumbs up for clearing cookies which is a part of the original question. |
So, I came here finding out that I can't fully convert my WebBrowser control codes to WebView2 because of the ExecuteScript JS DOM implementation. Sucks though but I hope they implement the easy-DOM way. |
Sorry if this has been discussed before, i cannot find the subject anywhere.
webbrowser.document object what is the equivalent in Webview2 ?
Can we access the DOM like we did with webbrowser control ?
How can all of the cookies and user data be cleared when the winform program is run ?
All of the cookies and data are still in the browser from prior sessions ?
with Webbrowser control we used wininet method
public static bool SupressCookiePersist()
{
// 3 = INTERNET_SUPPRESS_COOKIE_PERSIST
// 81 = INTERNET_OPTION_SUPPRESS_BEHAVIOR
return SetOption(81, 3);
}
This does not work with Webview2, is there any way to get a fresh browser session when the program is executed ????
Any help would be appreciated.
AB#28556146
The text was updated successfully, but these errors were encountered: