我正在尝试从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();