Awesomium WebControl load string - c#

Is there a way to load a html as a string in webControl?
Something like:
webControl.Load("<!DOCTYPE html><html>...");
Like used in the normal wpf webControl:
webControl.NavigateToString("<!DOCTYPE html><html>...");

Actually now I found the answer in the tutorials for C++ (not on .net wpf) in Awesomium site.
Here is my solution:
var uri = new Uri("data:text/html,<!DOCTYPE html><html>...", UriKind.Absolute);
webControl.Source = uri;

I know it is an old question but here is how I menaged to do it :
var page = new WebControl
{
ViewType = WebViewType.Window,
};
page.NativeViewInitialized += (s, e) =>
{
page.LoadHTML("<html>SOME TEXT</html>");
};

Instead of using a URL in the source just put your HTML in there
loaded from Awesomium tutorials

Here is my solution:
Load html string to a file and then load page using webControl.Source property.
public static string WriteHtmlToTempFile(string html)
{
var fileName = GetTempFileName("html");
System.IO.File.WriteAllText(fileName, html);
return fileName;
}
var strHtml = "<HTML> Hello World</HTML>";
var file = Common.WriteHtmlToTempFile(strHtml);
var wUri = new Uri(string.Format(#"file://{0}", file ));
webControl2.Source = wUri;

Related

How can I show an HTML-Code as a Website inside an app with Xamarin.Forms, C#?

I have following problem: I wrote a HTML-Code which shows a variable List as table when I press the Button. I did so far, that the table get showed in PDF in a Print Service. But the table has some sort functions and on a PDF one can't use them. So I try to show the HTML as a Website in Webview in the App, where one can use the function. The code for the code behind is the following:
public ICommand PrintCommand => new AsyncCommand(Print);
private async Task Print()
{
// New up the Razor template and set the model property
var printTemplate = new ListPrintTemplate
{
Model = FilteredList,
};
// Generate the HTML
var htmlString = printTemplate.GenerateString();
// Create a source for the webview
var htmlSource = new HtmlWebViewSource{ Html = htmlString };
// Create and populate the Xamarin.Forms.WebView
var browser = new WebView { Source = htmlSource };
var printService = Xamarin.Forms.DependencyService.Get<IPrintService>();
printService.Print(browser, $"{Res.Probe}-{FilteredList}");
}
I have still the printService in the last lines. I looked over the libraries and the Microsoft documentation but can't help myself.

How to load another JQuery version from GeckoWebBrowser control

I use Gecko Browser in C#. The following code shows that jQuery is loaded.
GeckoWebBrowser GWB = new GeckoWebBrowser .....
bool JSExec;
string JSresult = "";
string JStext = #"alert(jQuery.fn.jquery);";
using (AutoJSContext JScontext = new AutoJSContext(GWB.Window.JSContext))
{
JSExec = JScontext.EvaluateScript(JStext, (nsISupports)GWB.Window.DomWindow, out JSresult);
}
Alert box display 1.4.4
Is it possible to load a newer version of jQuery? Eg 2.0.2 => https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js
Edit to replay #John
Yes I'm using GeckoFx. To to include a library, in
GeckoBrowser_DocumentCompleted()
I do this :
bool JSExec;
string JSresult = "";
GeckoScriptElement scriptJQuery = GWB.Document.CreateElement("script") as GeckoScriptElement;
scriptJQuery.Type = "text/javascript";
scriptJQuery.Src = "https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js";
GWB.Document.Head.AppendChild(scriptJQuery);
string JStext = #"alert(jQuery.fn.jquery);";
using (AutoJSContext JScontext = new AutoJSContext(GWB.Window.JSContext))
{
JSExec = JScontext.EvaluateScript(JStext, (nsISupports)GWB.Window.DomWindow, out JSresult);
}
Hélas ! Alert display 1.4.4 !
Assuming you're using https://bitbucket.org/geckofx, it doesn't have a version of jquery, or any other library. It's just a browser control.
So to include a library, you specify it on the html page you're loading, just like you would if you were using any other browser:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

Issue in Parsing Json image in C#

net C#. I am trying to parse Json from a webservice. I have done it with text but having a problem with parsing image. Here is the Url from where I m getting Json
http://collectionking.com/rest/view/items_in_collection.json?args=122
And this is My code to Parse it
using (var wc = new WebClient()) {
JavaScriptSerializer js = new JavaScriptSerializer();
var result = js.Deserialize<ck[]>(wc.DownloadString("http://collectionking.com/rest/view/items_in_collection.json args=122"));
foreach (var i in result) {
lblTitle.Text = i.node_title;
imgCk.ImageUrl = i.["main image"];
lblNid.Text = i.nid;
Any help would be great.
Thanks in advance.
PS: It returns the Title and Nid but not the Image.
My class is as follows:
public class ck
{
public string node_title;
public string main_image;
public string nid; }
Your problem is that you are setting ImageUrl to something like this <img typeof="foaf:Image" src="http://... and not an actual url. You will need to further parse main image and extract the url to show it correctly.
Edit
This was a though nut to crack because of the whitespace. The only solution I could find was to remove the whitespace before parsing the string. It's not a very nice solution but I couldn't find any other way using the built in classes. You might be able to solve it properly using JSON.Net or some other library though.
I also added a regular expression to extract the url for you, though there is no error checking what so ever here so you'll need to add that yourself.
using (var wc = new WebClient()) {
JavaScriptSerializer js = new JavaScriptSerializer();
var result = js.Deserialize<ck[]>(wc.DownloadString("http://collectionking.com/rest/view/items_in_collection.json?args=122").Replace("\"main image\":", "\"main_image\":")); // Replace the name "main image" with "main_image" to deserialize it properly, also fixed missing ? in url
foreach (var i in result) {
lblTitle.Text = i.node_title;
string realImageUrl = Regex.Match(i.main_image, #"src=""(.*?)""").Groups[1].Value; // Extract the value of the src-attribute to get the actual url, will throw an exception if there isn't a src-attribute
imgCk.ImageUrl = realImageUrl;
lblNid.Text = i.nid;
}
}
Try This
private static string ExtractImageFromTag(string tag)
{
int start = tag.IndexOf("src=\""),
end = tag.IndexOf("\"", start + 6);
return tag.Substring(start + 5, end - start - 5);
}
private static string ExtractTitleFromTag(string tag)
{
int start = tag.IndexOf(">"),
end = tag.IndexOf("<", start + 1);
return tag.Substring(start + 1, end - start - 1);
}
It may help

Simple web crawler in C#

I have created a simple web crawler but I want to add the recursion function so that every page that is opened I can get the URLs in this page, but I have no idea how I can do that and I want also to include threads to make it faster.
Here is my code
namespace Crawler
{
public partial class Form1 : Form
{
String Rstring;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WebRequest myWebRequest;
WebResponse myWebResponse;
String URL = textBox1.Text;
myWebRequest = WebRequest.Create(URL);
myWebResponse = myWebRequest.GetResponse();//Returns a response from an Internet resource
Stream streamResponse = myWebResponse.GetResponseStream();//return the data stream from the internet
//and save it in the stream
StreamReader sreader = new StreamReader(streamResponse);//reads the data stream
Rstring = sreader.ReadToEnd();//reads it to the end
String Links = GetContent(Rstring);//gets the links only
textBox2.Text = Rstring;
textBox3.Text = Links;
streamResponse.Close();
sreader.Close();
myWebResponse.Close();
}
private String GetContent(String Rstring)
{
String sString="";
HTMLDocument d = new HTMLDocument();
IHTMLDocument2 doc = (IHTMLDocument2)d;
doc.write(Rstring);
IHTMLElementCollection L = doc.links;
foreach (IHTMLElement links in L)
{
sString += links.getAttribute("href", 0);
sString += "/n";
}
return sString;
}
I fixed your GetContent method as follow to get new links from crawled page:
public ISet<string> GetNewLinks(string content)
{
Regex regexLink = new Regex("(?<=<a\\s*?href=(?:'|\"))[^'\"]*?(?=(?:'|\"))");
ISet<string> newLinks = new HashSet<string>();
foreach (var match in regexLink.Matches(content))
{
if (!newLinks.Contains(match.ToString()))
newLinks.Add(match.ToString());
}
return newLinks;
}
Updated
Fixed: regex should be regexLink. Thanks #shashlearner for pointing this out (my mistype).
i have created something similar using Reactive Extension.
https://github.com/Misterhex/WebCrawler
i hope it can help you.
Crawler crawler = new Crawler();
IObservable observable = crawler.Crawl(new Uri("http://www.codinghorror.com/"));
observable.Subscribe(onNext: Console.WriteLine,
onCompleted: () => Console.WriteLine("Crawling completed"));
The following includes an answer/recommendation.
I believe you should use a dataGridView instead of a textBox as when you look at it in GUI it is easier to see the links (URLs) found.
You could change:
textBox3.Text = Links;
to
dataGridView.DataSource = Links;
Now for the question, you haven't included:
using System. "'s"
which ones were used, as it would be appreciated if I could get them as can't figure it out.
From a design standpoint, I've written a few webcrawlers. Basically you want to implement a Depth First Search using a Stack data structure. You can use Breadth First Search also, but you'll likely come into stack memory issues. Good luck.

How to verify a Hyperlink exists on a webpage?

I have a need to verify a specific hyperlink exists on a given web page. I know how to download the source HTML. What I need help with is figuring out if a "target" url exists as a hyperlink in the "source" web page.
Here is a little console program to demonstrate the problem:
public static void Main()
{
var sourceUrl = "http://developer.yahoo.com/search/web/V1/webSearch.html";
var targetUrl = "http://developer.yahoo.com/ypatterns/";
Console.WriteLine("Source contains link to target? Answer = {0}",
SourceContainsLinkToTarget(
sourceUrl,
targetUrl));
Console.ReadKey();
}
private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
{
string content;
using (var wc = new WebClient())
content = wc.DownloadString(sourceUrl);
return content.Contains(targetUrl); // Need to ensure this is in a <href> tag!
}
Notice the comment on the last line. I can see if the target URL exists in the HTML of the source URL, but I need to verify that URL is inside of a <href/> tag. This way I can validate it's actually a hyperlink, instead of just text.
I'm hoping someone will have a kick-ass regular expression or something I can use.
Thanks!
Here is the solution using the HtmlAgilityPack:
private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
{
var doc = (new HtmlWeb()).Load(sourceUrl);
foreach (var link in doc.DocumentNode.SelectNodes("//a[#href]"))
if (link.GetAttributeValue("href",
string.Empty).Equals(targetUrl))
return true;
return false;
}
The best way is to use a web scraping library with a built in DOM parser, which will build an object tree out of the HTML and let you explore it programmatically for the link entity you are looking for. There are many available - for example Beautiful Soup (python) or scrapi (ruby) or Mechanize (perl). For .net, try the HTML agility pack. http://htmlagilitypack.codeplex.com/

Categories

Resources