简体   繁体   English

如何从js中获取返回值

[英]How to get returned value from js

I have js script on loaded page by cefsharp browser, it is GetText(args) and it is return text.我在cefsharp浏览器加载的页面上有js脚本,它是GetText(args) ,它是返回文本。 I have this code:我有这个代码:

var script = chromiumWebBrowser.ExecuteScriptAsync("GetText", args);
string text = script.ToString();

But this dont work, because it is show error on creating script, implicitly typed variable cant assign void.但这不起作用,因为它在创建脚本时显示错误,隐式类型的变量不能分配 void。

Also tryed get value by this code还尝试通过此代码获取价值

object[] args = new string[0];
var d = chromiumWebBrowser.EvaluateScriptAsync("GetText", args).Result;
string b = d.ToString();

But b returns CefSharp.JavascriptResponse, not text.但是 b 返回 CefSharp.JavascriptResponse,而不是文本。 How I can get this returned value?我怎样才能得到这个返回值?

Well, the docs say ExecuteScriptAsync returns void .好吧, 文档说ExecuteScriptAsync返回void

In fact, they say (emphasis mine)事实上,他们说(强调我的)

Execute some Javascript code in the context of this WebBrowser.在此 WebBrowser 的上下文中执行一些 Javascript 代码。 As the method name implies, the script will be executed asynchronously, and the method therefore returns before the script has actually been executed.正如方法名称所暗示的,脚本将异步执行,因此该方法在脚本实际执行之前返回。

so there can't be a return value.所以不可能有返回值。

You would probably need EvaluateScriptAsync , which returns a Task whose completion you can wait for;您可能需要EvaluateScriptAsync ,它返回一个您可以等待其完成的Task you can then read the Result from the JavascriptResponse the Task wraps.然后,您可以从 Task 包装的JavascriptResponse中读取Result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM