WebView2 show html variable instead of url - c#

Hey guys I have a program that still uses the old WebBrowser Xaml control. Now I wanted to exchange this on WebView2 here I have problems. Currently, the body of an email is pulled from a database as html code and written to a variable, which is linked to the WebBrowser control. Only WebView2 wants a URL to be passed in the source function. How can I pass a variable containing HTML code instead of a link to a web page? Or are there other alternatives for this?

I would suggest you try NavigateToString
await webView21.EnsureCoreWebView2Async();
webView21.NavigateToString(anEmailHTMLVariable);

Related

How to embed a part of a website in webview UWP

Is it possible to embed only a specific portion inside of a page(i.e. div) on the WebView control in UWP?
The webview doesn't include an address bar and so will only show pages that you load or can be navigated to from those pages. If you can control what is loaded you can show only the pages you wish.
If you don't have control over the HTML of the site, you could prevent access to pages you don't want by handling the NavigationStarting event and canceling the navigation if it's to a page you don't want displaying.
No, assuming by a "portion" of a website you mean a portion within a page. However as Matt said, if you only want to show a certain page, you can of course navigate to the relevant web page and then control what navigation links you allow through his method.
If you did indeed mean only a portion within a page, the options aren't attractive. Theoretically you could pull down the html of a site with an Http request and then parse it to filter out what you didn't want, store it locally, and then load that local page in the webview. However I think it would add significant overhead, could create conflicts with javascript or whatever else the site might be using, among other things. But hey maybe if it's a very targeted use-case you might be able to play with it.
If in your scenario though you also control said website the app is accessing, then I'd just enable specific URLs to offer up the desired versions of your site when the app requests 'em.
Note 100% sure, but i can give a hit to embed external part of a any html content to a website opened in webview control.
var result = await this.webView.InvokeScriptAsync("eval", new[] { "document.documentElement.outerHTML;" });
string result = await this.webView.InvokeScriptAsync("eval", new [] {"document.getElementById('tablePrint').innerHTML = myTable;" });
Add HTML content through java script.
Hope that will help

Get output of web page in C#

I am attempting to get the resulting web page content so I can extract the display text. I have attempted the code below but it gets me the source html and not the resulting html.
string urlPath = "http://www.cbsnews.com/news/jamar-clark-protests-follow-decision-not-to-file-charges-in-minneapolis-police-shooting/";
WebClient client = new WebClient();
string str = client.DownloadString(urlPath);
Compare the text in the str variable with the html in the Developer Tools in the Chrome browser and you will get different results.
Any recommendations will be appreciated.
I'm assuming you mean that you want the article text. If so you will need to follow a different course of action. The page you refer to is loaded with client script that injects loads of content into the base HTML document. This is done by executing the client-side script. You will need to parse the DOM after the script is executed to get the content you're interested in.
As others have pointed out, an actual web browser will parse the downloaded HTML and execute javascript against it, potentially altering its content. While you could try to do that parsing yourself, the easiest route is to ask a real web browser to do it for you and then grab the results.
The easiest solution specifically in C# would be to use the WebBrowser Control from Windows Forms, which essentially exposes IE to your program, allowing you to control it. Use the Navigate method to load the URL in question, then use the Document property to navigate the DOM. You can, at that point, get the outerHTML to get the final content of the DOM as HTML.
If you're not writing a Windows program and are interested more in headless operation, have a look at PhantomJS. It's a headless Webkit browser that is scriptable from javascript and would give you similar capability, although not in C#.

Find URL Responses? Alternative To Default WebBrowser Control?

Hello guys I have an issue bugging me for the past few weeks.
What I'm trying to accomplish: I need a webbrowser control with the ability to change user agent (once at start) and referrer. But most important The ability to see the urls responses. What I mean by that for example if you navigate to a website you get back Images/Javascripts files/Dyanmic URLS in response I need access to those urls which some of them have dynamic variables (Regular Webbrowser Control will not show you those & you can't access it in any way beside using fiddler core).
I was able to do that with webbrowser + fiddlercore I can see and do what ever with those urls addresses. The problem was if you run few instances of this program (or sometimes once if the program has some automation to work with the url responses) It gets stuck or doesn't work. I tried fixing it and making it work but it's kind of a hacky solution that doesn't work right. I need a simple way to access those urls just as if you used httpwebrequest but as a webbrowser. Why I need it as a webbrowser? The way I work I need the execution of all the tracking pixels and scripts and images etc.. a normal webbrowser behaivor in httpwebrequest you can't just navigate and all the scripts will be execute as webbrowser, or can you?
Using the System.Windows.Forms.WebBrowser control in a WinForms app, set the webBrowser.URL property to the URL of the page you're interested in.
The webbrowser's DocumentCompleted event fires after the page has loaded. Any dynamically loaded JavaScript should be done by then. Hook the DocumentCompleted event and use the webbrowser.Document.Images to get a list of all image elements on the page. From those images you can get their SRC attributes which contains their URLs including any query parameters hanging off the end. You can use webbrowser.Document.Links to get a list of all hyperlinks on the page. For other HTML elements of interest, you can use GetElementsByTagName("foo") to fetch all elements with that tag name from the page, then dig into their attributes to pull out URL properties.
With webbrowser.Document you can get to any HTML element, whether it is statically or dynamically created.
What you can't get to through webbrower.Document is data that is loaded asynchronously using XMLHttpRequest(), because this data is not part of the browser Document Object Model. Web pages with scripted false buttons will be difficult to intercept.
However, if you know where the data is stored by the JavaScript executing on the page, you may be able to access it using webbrowser.Document.InvokeScript(). If the JavaScript on the page stores URLs in a mydata property of the window object, for example, you could try webbrowser.Document.InvokeScript("window.mydata") or some variation to retrieve the value of mydata into the C# app.

Deployment.Current.Dispatcher.BeginInvoke Wont do anything

I am having trouble displaying html onto a web browser. I am using the method "Deployment.Current.Dispatcher.BeginInvoke" but it wont displaying anything. What I am trying to do is getting html from a server as a string and displaying in my web browser. I can display messagebox but i cant send the string that contains html onto my web browser.
Here is the snippet of code that is giving me trouble:
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
WebBrowser webBrowser1 = new WebBrowser();
//MessageBox.Show(responseString);
webBrowser1.NavigateToString(responseString);
});
I think the reason for this error is because you're creating the WebBrowser-instance inside your callback code - which you shouldn't do anyways (because how are you going to display it on the screen? If you are trying to go from your application into the main WebBrowser-app (IE), you should use WebBrowserTask instead.
So either:
you are trying to show a WebBrowser inside your application view (hybrid style). If so, you should call .NavigateToString(string html) on the WebBrowser you're displaying instead of creating another instance.
you are trying to show downloaded HTML in the main browser app on your phone (Internet Explorer). This can't be done by handing over the HTML directly, you would have to send it the URI-object where the browser can find it itself.
Or of course, the problem is somewhere else in your code. This is the best I could answer with the information and code you have provided.

How to get this file prompted to the user?

Hi Im working on a MVC3 Razor project and I have been stuck on this problem for a few hours now...
I'm trying to get a html to pdf converter to serve a document to the user...
What I want is the following..
A page is rendered and displayed. On the bottom of the page there should be a little icon display something like download as pdf. and what that does is where my problem lies..
All the data that I want is dynamically created within a $("#content").html();
So what I have tried is a jquery/ajax function passing $("#content").html(); as a paramater to my function which creates the pdf (works but I have no clue how to prompt the created file to the user?)
Other solution was #(Html.ActionLink()) but I dont know how to pass the data ($("#content").html()) within that link?
And when trying to work with the functionallity to use the converter to go to the url was a dead end cuz it got its own session and got redirected to the loginpage..
Any help would be appriciated!
I am not familiar with JavaScript, but what if you save the PDF in the temp file first by calling you web service method, and then after it completed you can use JavaScript to navigate to the URL where generated PDF will be returned as content.
Since the XmlHTTPRequest Object cannot handle datatypes other than html, text, json, jsonp and xml you will need to redirect to the pdf location.
I'm not sure what exactly you're doing in your Ajax Request, but once that is completed you can just redirect the window(Form-Action) to the location of the created PDF. This wouldn't actually redirect the browser but only prompt for saving the file.
Why just create a function that returns the created PDF (during ajax request) as File result and then set the window's location to point to this action once the ajax request is completed successfully?
Edit: That means you are not saving the PDF anywhere. So the workaround, is to either use this jquery download plugin or append an iframe dynamically and then post the data through it. Hope this helps.
Figured out a workaround, what I did was write an extension to my HtmlHelper and use that to render my control into a html string instead of a view. Therefor I could use a actionlink to say render this page and get all the html data that way.
http://msug.vn.ua/Posts/Details/3301
Thanks for help tho!

Categories