如何使用webkit使js函数与c#交互?
我用运行在mono c#中的webkit制作了一个webbrowser。我希望允许页面在浏览器中调用函数,然后浏览器根据传递的参数执行操作。我似乎做不到 html页面是下一个页面如何使用webkit使js函数与c#交互?,c#,mono,webkitgtk,C#,Mono,Webkitgtk,我用运行在mono c#中的webkit制作了一个webbrowser。我希望允许页面在浏览器中调用函数,然后浏览器根据传递的参数执行操作。我似乎做不到 html页面是下一个页面 <html> <head> <title>ola</title> </head> <body> <button onclick="window.external.Test('called from script code');">tes
<html>
<head>
<title>ola</title>
</head>
<body>
<button onclick="window.external.Test('called from script code');">test</button>
</body>
</html>
如果你想要c#项目,它就在这里
提前感谢。您需要从旧的webview类派生一个新的webview类,并覆盖:
public class MyDerived: WebView
{
...
public const string COM_COMMAND_INIT = "http://mypresetcommand/?";
protected override void OnResourceRequestStarting(WebKit.WebFrame web_frame, WebKit.WebResource web_resource, WebKit.NetworkRequest request, WebKit.NetworkResponse response)
{
// check if this is a command
if (request.Uri.ToLower().StartsWith(COM_COMMAND_INIT))
{
// this is a com command. Responding with responce.
string cmnd = web_resource.Uri.Substring(COM_COMMAND_INIT.Length);
// HANDLE COMMAND HERE.
return;
}
base.OnResourceRequestStarting(web_frame, web_resource, request, response);
}
}
完成此操作后,只需在网页内对C#应用程序进行ajax调用即可。将“myresetcommand/?”替换为您自己的不可检查url。您可以通过WebView中的ExecuteScript进行响应
using System;
using Gtk;
using GLib;
using WebKit;
using System.Timers;
//using Notifications;
using System.Text.RegularExpressions;
public partial class MainWindow: Gtk.Window
{
private Timer notifications = new Timer(2000);
private ExtendedWebView webView;
private bool recent_notification = false;
public MainWindow (string url): base (Gtk.WindowType.Toplevel)
{
Build ();
url ="http://127.0.0.1/csharp/";
webView = new ExtendedWebView ();
ExtendedWebSettings settings = new ExtendedWebSettings ();
settings.g_object_set ("user-agent", new GLib.Value ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0"));
settings.g_object_set ("enable-spell-checking", new GLib.Value (true));
webView.TitleChanged += HandleTitleChanged;
webView.Settings = settings;
webView.NewWindowPolicyDecisionRequested += HandleNewWindowPolicyDecisionRequested;
webView.CreateWebView += HandleCreateWebView;
webView.Open(url);
webView.LoadFinished += new LoadFinishedHandler (OnLoadFinished);
VBox vbox1 = new VBox();
vbox1.PackStart(webView);
this.Add(vbox1);
this.ShowAll();
}
private void OnLoadFinished (object o, LoadFinishedArgs args)
{
String testVar = "[ { \"id\" : \"4578\" },{ \"id\" : \"4579\" }]";
Console.WriteLine("Executei isto por aqui....");
webView.ExecuteScript("scriptTeste(" + testVar + ");");
Console.WriteLine("Executei isto por aqui....");
}
void HandleCreateWebView (object o, CreateWebViewArgs args)
{
Window info = new Window("");
info.DefaultWidth = 1000;
info.DefaultHeight = 700;
VBox vbox2 = new VBox();
WebView child = new WebView();
child.NavigationRequested += HandleNavigationRequested1;
vbox2.PackStart(child);
info.Add (vbox2);
info.ShowAll();
args.RetVal = child;
}
void HandleNavigationRequested1 (object o, NavigationRequestedArgs args)
{
// Destroy the window if it was already opened in the browser
if (args.Request.Uri.Contains ("&URL=")) {
WebView self = (WebView)o;
VBox container = (VBox)self.Parent;
Window parent = (Window)container.Parent;
parent.Destroy();
args.RetVal = 1;
}
}
void HandleNewWindowPolicyDecisionRequested (object o, NewWindowPolicyDecisionRequestedArgs args)
{
string URL = System.Web.HttpUtility.UrlDecode(Regex.Split(args.Request.Uri, "&URL=")[1]);
System.Diagnostics.Process.Start(URL);
}
void HandleElapsed (object sender, ElapsedEventArgs e)
{
if (recent_notification) {
notifications.Interval = 5000;
recent_notification = false;
}
/* if (!recent_notification && webView.SearchText ("Reminders", true, true, true)) {
Notification notify = new Notification ("Outlook Notification", "");
notify.Urgency = Urgency.Normal;
notify.Show ();
recent_notification = true;
notifications.Interval = 30000;
}*/
}
void HandleTitleChanged (object o, TitleChangedArgs args)
{
this.Title = args.Title;
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
}
public class MyDerived: WebView
{
...
public const string COM_COMMAND_INIT = "http://mypresetcommand/?";
protected override void OnResourceRequestStarting(WebKit.WebFrame web_frame, WebKit.WebResource web_resource, WebKit.NetworkRequest request, WebKit.NetworkResponse response)
{
// check if this is a command
if (request.Uri.ToLower().StartsWith(COM_COMMAND_INIT))
{
// this is a com command. Responding with responce.
string cmnd = web_resource.Uri.Substring(COM_COMMAND_INIT.Length);
// HANDLE COMMAND HERE.
return;
}
base.OnResourceRequestStarting(web_frame, web_resource, request, response);
}
}