如何从Cefsharp EvaluateScriptAsync中获取返回值



我正在尝试从CefSharp EvaluateScriptAsync获取Base64中的图像数组

代码:

JavascriptResponse response = await browser.EvaluateScriptAsync(@"
function foo(){
var img = document.getElementsByTagName('IMG');
var finalArray = {};
for ( var i=0; i < img.length; i++ )
{
  if (img[i].hasAttribute('indexvalue')){
          var c = document.createElement('canvas');
          var ctx = c.getContext('2d');
          ctx.drawImage(img[i], 10, 10);
      
          finalArray.push(c.toDataURL());
          $('canvas').remove();
        }
}
return finalArray;
}
var res = foo();
");

这个response.Result给了我CefSharp.JavascriptResponse.Result.get returned null。那么,我如何从scrip中获得返回数组,或者任何简单的例子都可以

这里的问题是EvaluateScriptAsync的脚本,其中函数被分配给var res = foo();,而应该是仅foo();

更新的代码

JavascriptResponse response = await browser.EvaluateScriptAsync(@"
function foo(){
var img = document.getElementsByTagName('IMG');
var finalArray = [];
for ( var i=0; i < img.length; i++ )
{
if (img[i].hasAttribute('indexvalue')){
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
ctx.drawImage(img[i], 10, 10);
var dataURL = c.toDataURL('image/png');
finalArray.push(dataURL.replace(/^data:image/(png|jpg);base64,/, ''));
$('canvas').remove();
}
}
return finalArray;
}
foo();
              ");
//converting your response.Result into string array 
string[] arr = ((IEnumerable)response.Result).Cast<object>()
.Select(c => c.ToString())
.ToArray();

相关内容

  • 没有找到相关文章

最新更新