Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

c# - CefSharp LoadHtml

Could someone explain to me how the CefSharp LoadHtml function works?

LoadHtml(string html, string url)

What do the html and url parameters represent?

I am interested in loading a page from a raw HTML string into the CefSharp browser.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update: CefSharp has a new LoadHtml(string html) method that loads the HTML as a base64-encoded data URI. It is more reliable that the LoadHtml(string html, string url) method described below.

In LoadHtml(string html, string url):

html is your HTML string, e.g. "<html><body>Hello world</body></html>". Actually, you can even put other content in the string, such as SVG markup, as long as Chromium can understand it.

url is needed because your HTML code may contain JavaScript that tries to perform AJAX calls, and the web browser needs to understand what security restrictions apply. The scheme (e.g. "http:", "about:") and domain (e.g. "localhost", "google.com") affect behaviour such as clicking on links, AJAX requests, iframes, etc.

If you want to simply render static HTML, make the url something unique such as http://rendering/ (so that the resource handler does not overlap with a real url on the web). If you need to load the HTML and then interact with it or perform AJAX calls, choose a url that matches the domain you want to interact with - for example, if you want to make an alternative Google home page and perform AJAX search queries, you will want to use https://www.google.com/ as your URL so you can communicate with it.


You can see the source code for LoadHtml here.

What CefSharp does is:

  1. Register a resource handler for the given url.
  2. Call Load(url) to tell Chromium to load the given url.

Then, under the hood:

  1. Chromium requests the url.
  2. The resource handler intercepts the request, and returns your html.
  3. Chromium renders your html instead of the real content of the URL.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...