wpf 如何使用cefsharp将输入传递给javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25269534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:20:04  来源:igfitidea点击:

How to pass inputs to javascript functions with cefsharp

c#wpfcefsharp

提问by zaknotzach

I am trying to add a CefSharp WebView to my WPF application in place of the original WebBrowsers that we used. The WebBrowser has an InvokeScript function (http://msdn.microsoft.com/en-us/library/cc452443(v=vs.110).aspx) Which allows you to invoke a JavaScript function and optionally pass in an obj array of inputs to that JS function.

我正在尝试将 CefSharp WebView 添加到我的 WPF 应用程序中,以代替我们使用的原始 WebBrowsers。WebBrowser 有一个 InvokeScript 函数(http://msdn.microsoft.com/en-us/library/cc452443(v=vs.110).aspx)它允许您调用 JavaScript 函数并可选择传入一个 obj 数组该 JS 函数的输入。

Is there any way to do something similar with the CefSharp WebView where I can pass input parameters to the JavaScript function? For example, I can do:

有什么方法可以对 CefSharp WebView 做类似的事情,我可以在其中将输入参数传递给 JavaScript 函数吗?例如,我可以这样做:

this.webBrowser.InvokeScript("scriptName", input0, input1, input2);

with the WebBrowser, is there any equivalent function, or multiple functions, that would allow for this with the CefSharp WebView?

使用 WebBrowser,是否有任何等效的功能或多个功能可以使用 CefSharp WebView 实现这一点?

采纳答案by zaknotzach

I figured out how to do what I wanted... and it was relatively straightforward.

我想出了如何做我想做的事……而且相对简单。

If you want the functionality of the WebBrowser's InvokeScript function

如果您想要 WebBrowser 的 InvokeScript 函数的功能

this.webBrowser.InvokeScript("functionName", input0, input1, input2);

with the CefSharp WebView then you just have to do something like this:

使用 CefSharp WebView 那么你只需要做这样的事情:

webView.ExecuteScript(String.Format("functionName({0},{1},{2});", input0, input1, input2));

You will have to make sure to escape any string parameters correctly since you are calling a javascript function where the inputs will be filled in with your values and therefore will be treated as string literals.

您必须确保正确转义任何字符串参数,因为您正在调用一个 javascript 函数,其中输入将填充您的值,因此将被视为字符串文字。

If you really don't want to change your code and you are swapping out the WebBrowser with a WebView then you could make an extension method that adds "InvokeScript" to the WebView.

如果您真的不想更改您的代码并且您正在用 WebView 换出 WebBrowser,那么您可以创建一个将“InvokeScript”添加到 WebView 的扩展方法。

回答by XiaoXiao Zhang

I tried out above function, didn't work. Finally figured it out, missing quotes for each placeholders.

我试过上面的功能,没有用。终于弄清楚了,每个占位符都缺少引号。

Something like this:

像这样的东西:

webView.ExecuteScript(String.Format("functionName('{0}','{1}','{2}');", input0, input1, input2));

回答by user3934404

You can execute JavaScrip from webView

您可以从 webView 执行 JavaScrip

webView.ExecuteScript("document.getElementById...");