How to load another JQuery version from GeckoWebBrowser control - c#

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>

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.

C# Windows Store WebView get user-agent

Is there a way to extract the user-agent string that the WebView control uses? If so, I would greatly appreciate it if anyone can give me a method to do so. Using the following does not seem to work:
var userAgent = new StringBuilder(256);
int length = 0;
UrlMkGetSessionOption(UrlMonOptionUserAgent, userAgent, userAgent.Capacity - 1, ref length, 0);
I take that back, using UrlMkGetSessionOption as mentioned in the code above does work.
I currently use this method, adapted from a method given for windows phone originally. It gives the correct result, and gets it straight from a real instance of a WebView object, so gives me more confidence in it having the correct value.
private static string s_userAgent = null;
// Get the default UserAgent which webviews use on this platform.
public async Task<string> GetUserAgent()
{
if (s_userAgent == null)
{
const string Html = #"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">
<html>
<head>
<script language=""javascript"" type=""text/javascript"">
function notifyUA() { window.external.notify(navigator.userAgent); }
</script>
</head>
<body onload=""notifyUA();""></body>
</html>";
SemaphoreSlim signal_done = new SemaphoreSlim(0, 1);
var wv = new WebView();
wv.ScriptNotify += (sender, args) =>
{
s_userAgent = args.Value;
// set signal, to show we've done
signal_done.Release();
};
wv.NavigateToString(Html);
// wait for signal
await signal_done.WaitAsync();
Debug.WriteLine("GetUserAgent() called. User agent from WebView: \n{0}", s_userAgent);
}
return s_userAgent;
}
This started as a comment but became too long. To expand on his (#Rexfelis) own answer:
I've found that there can be a difference in what UrlMkGetSessionOption returns depending on where you are in the application lifecycle and if a WebView has been initialized yet in a XAML view.
If you call it before component initialization, it will be missing WebView/3.0 (at least in Windows 10); after initialization it will have that text and results in the same string as the answer by #SimonTillson.
If you need to know the right user agent before component initialization, you have to new up a WebView and navigate before querying UrlMkGetSessionOption; e.g. var wv = new WebView(); wv.NavigateToString(...);. It seems that the user agent is modified on first navigation to include WebView/3.0.

Awesomium WebControl load string

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;

The difference between "IE9 debug tools" HTML output and webpage source HTML that I got via C#

I'm not the first time here with questions like this.
I have a Volvo auto parts catalog that is implemented as a client application to a local database and works only in IE8/9. I need to find and get some positions displayed in IE.
Here's an example of IE output:
It's just a table and nothing more.
And here's what I see in IE9 debug tools:
IE shows me full layout of a page where I can see a target table and rows with the data I need to get.
I wrote a simple class that should walk through all IE tabs and get HTML from the target page:
using System.Globalization;
using System.Text.RegularExpressions;
using SHDocVw;
namespace WebpageHtmlMiner
{
static class HtmlMiner
{
public static string GetWebpageHtml(string uriPattern)
{
var uriRegexPattern = uriPattern;
var regex = new Regex(uriRegexPattern);
var shellWindows = new ShellWindows();
InternetExplorer internetExplorer = null;
foreach (InternetExplorer ie in shellWindows)
{
Match match = regex.Match(ie.LocationURL);
if (!string.IsNullOrEmpty(match.Value))
{
internetExplorer = ie;
break;
}
}
if (internetExplorer == null)
{
return "Target page is not opened in IE";
}
var mshtmlDocument = (mshtml.IHTMLDocument2)internetExplorer.Document;
var webpageHtml = mshtmlDocument.body.parentElement.outerHTML.ToString(CultureInfo.InvariantCulture);
return webpageHtml; //profit
}
}
}
It seems to work fine but instead of what I see in IE debug tools I get HTML code with tons of javascript functions and no data in target table.
Is there any way to get exactly what I see in IE debug tools?
Thanks.
You can get the original source (the one sent by the server) in "Script" tab (this works both on my IE8 and my IE10).
If you do not use AJAX, I think you can right-click on the page and choose Display Souce option too.

Removing the user interaction from a Javascript pop up

In my application I'm showing a javascript pop up with a web page in it with the help of the following code:
popwin = window.open(URL, '" + id + "',
'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=750,
height=600,left = 262,top = 84');
which is giving me the required pop up with the web page in it.
I want to know whether there is any way to remove user interaction from this pop up and to remove the close button as well.
I will close this pop up using some timer ,but i don't want that user to be able to control the pop up. Is there any way for it?
I googled a bit but haven't got the relevant way to do it.
Note: Can i do this using Modal Pop Up?
Please suggest any good ways .
You can try following code.
popwin = window.open(URL, '" + id + "','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=750, height=600,left = 262,top = 84');
SetTimeOut(function(){ popwin.close()},2000);
Here '2000' is time in milliseconds. 2000 ms = 2sec.
Let me know if any error occurs.
You can try Telerik Radwindows.
You can load your content in a Fancy Box and customize the fancybox as per your needs.
I think it could be more appropriate to use a CSS-like popup instead.
Working example:
http://www.pat-burt.com/csspopup.html#
As for your timer you could change the css attribute with your timer or use some jquery framework plugin like impromptu:
http://trentrichardson.com/Impromptu/
Which cost less time to deploy than the css solution I think.
hey man this is my popup. its not a window but acts like a window and is in jquery and javascript both.
$("document").ready( function() {
var link = ""; // your link here
var d = document.createElement("div");
d.style.display = "block";
d.style.border = "1px solid #282828";
d.style.background = "#ffffff";
d.style.width = "750px";
d.style.height = "450px";
d.setAttribute("id", "popup");
var m = document.createElement("iframe");
m.setAttribute("src", link);
m.style.width = "750px";
m.style.height = "450px";
m.style.border = "0";
document.body.appendChild(d);
d.appendChild(m);
var s=0;
var n = setInterval( function() { s=s+1; if (s == 5) {
$("#popup").remove();
} }, 1000);
});
http://jsfiddle.net/APKTX/

Categories

Resources