CefSharp 是否支持 JavaScript 的承诺?

Does CefSharp support the promises of the JavaScript?

JavaScript 下面的代码在浏览器的控制台中运行。 但是当我将此代码放入 CefSharp 时,CefSharp returns 为空。 我正在使用 CefSharp 100.0.120-pre。 CefSharp 100.0.120-pre 是否支持 JavaScript 的承诺?

(function()
{
var a = document.querySelector('#dle-content > div.section > ul > li:nth-child(3)');
a.scrollIntoView();
document.querySelector('#dle-content > div.section > ul > li:nth-child(3)').click();    
var returnArray = new Array();
function wait(selector) {
return new Promise((resolve) => {
const listener = () => {
const node = document.querySelector(selector);
if (node) {
document.removeEventListener('DOMNodeInserted', listener);
resolve(node);
}
};
document.addEventListener('DOMNodeInserted', listener);
});
}
wait('.cdn_download_item')
.then(()=>
{
var elements = Array.from(document.querySelectorAll('.cdn_download_item span:first-child'));
var linksArray = new Array();
for (element of elements) 
{
linksArray.push(element.innerText);
}
returnArray=console.log(linksArray);
})
return returnArray;
})();

这就是我在 CefSharp 中使用 JavaScript 代码的方式 请检查我的代码为什么 CefSharp returns null

JavaScript + CefSharp + C#

string jsScript = @"
(function()
{
var returnArray = new Array();
function wait(selector) {
return new Promise((resolve) => {
const listener = () => {
const node = document.querySelector(selector);
if (node) {
document.removeEventListener('DOMNodeInserted', listener);
resolve(node);
}
};
document.addEventListener('DOMNodeInserted', listener);
});
}
wait('.cdn_download_item')
.then(()=>
{
var elements = Array.from(document.querySelectorAll('.cdn_download_item span:first-child'));
var linksArray = new Array();
for (element of elements) 
{
linksArray.push(element.innerText);
}
returnArray=console.log(linksArray);
})
return returnArray;
})();
                ";

            var task = chrome.EvaluateScriptAsync(jsScript5);
            await task.ContinueWith(x =>
            {
                if (!x.IsFaulted)
                {
                    var response = x.Result;
                    if (response.Success == true)
                    {
                        var final = (List<object>)response.Result;
                        foreach (var el in final)
                        {
                            textHtml.Text += el.ToString() + Environment.NewLine;
                        }
                    }
                }

            }, TaskScheduler.FromCurrentSynchronizationContext());

是的,支持承诺。您需要使用 EvaluateScriptAsPromiseAsync

而不是 EvaluateScriptAsync

Evaluate Javascript in the context of this Browsers Main Frame. The script will be executed asynchronously and the method returns a Task encapsulating the response from the Javascript. The result of the script execution in javascript is Promise.resolve so even no promise values will be treated as a promise. Your javascript should return a value. The javascript will be wrapped in an Immediately Invoked Function Expression. When the promise either trigger then/catch this returned Task will be completed.

EvaluateScriptAsPromiseAsync 与 EvaluateScriptAsync 略有不同,您必须return一个正确等待承诺的值。

var script = "return new Promise(function(resolve, reject) { setTimeout(resolve.bind(null, { a: 'CefSharp', b: 42, }), 1000); });"
JavascriptResponse javascriptResponse = await browser.EvaluateScriptAsPromiseAsync(script);

有关其他示例,请参阅 https://github.com/cefsharp/CefSharp/wiki/General-Usage#2-how-do-you-call-a-javascript-method-that-returns-a-result