CefGlue有一个现有的解决方案:Call .Net from javascript in CefSharp 1 - wpf
我想要这一点,但对于CefGlue:我想使用JavaScript与应用程序进行通信。因此,当我单击HTML站点中的一个按钮时,我希望应用程序能够处理这个问题(例如:启动一个tcp服务器)。
我尝试注册自己的CefV8Handler,但没有成功,处理程序上的Execute函数永远不会被调用。我现在要做的就是
protected override void OnWebKitInitialized()
{
Console.WriteLine("Registering testy extension");
Xilium.CefGlue.CefRuntime.RegisterExtension("testy", "var testy;if (!testy)testy = {};(function() {testy.hello = function() {};})();", new V8Handler());
base.OnWebKitInitialized();
}我的V8Handler代码如下所示:
public class V8Handler : Xilium.CefGlue.CefV8Handler
{
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
{
if (name == "testy")
Console.WriteLine("CALLED TESTY");
else
Console.WriteLine("CALLED SOMETHING WEIRED ({0})", name);
returnValue = CefV8Value.CreateNull();
exception = null;
return true;
}
}我处于多进程模式,没有控制台窗口显示“名为TESTY”,也没有显示“称为WEIRED”。
发布于 2015-06-16 16:01:28
找到了解决办法。诀窍是创建一个CefV8Value (CreateFunction)并将其分配给V8Handler。然后将此值分配给全局上下文。看上去是这样的:
internal class RenderProcessHandler : CefRenderProcessHandler
{
protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
{
CefV8Value global = context.GetGlobal();
CefV8Value func = CefV8Value.CreateFunction("magic", new V8Handler());
global.SetValue("magic", func, CefV8PropertyAttribute.None);
base.OnContextCreated(browser, frame, context);
}
}出现了另一个问题:在呈现程序过程中调用了它,但是我需要浏览器进程中的回调。在CefV8Handlers执行函数中,我执行了以下操作:
var browser = CefV8Context.GetCurrentContext().GetBrowser();
browser.SendProcessMessage(CefProcessId.Browser, CefProcessMessage.Create("ipc-js." + name));通过这种方式,我可以在OnProcessMessageReceived实现中检索CefClient函数中的消息。
https://stackoverflow.com/questions/30856305
复制相似问题