'Does CefSharp support the promises of the JavaScript?

JavaScript code below works in the console of the Browser. But when i put this code in CefSharp, CefSharp returns null. Im using CefSharp 100.0.120-pre. Does CefSharp 100.0.120-pre supprort the promises of the 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;
})();

This is how i use JavaScript code in CefSharp Please check my code why 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());


Solution 1:[1]

Yes, promises are supported. Instead of EvaluateScriptAsync you need to use EvaluateScriptAsPromiseAsync

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 differs from EvaluateScriptAsync slightly in that you must return a value for the promise to be awaited correctly.

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

See https://github.com/cefsharp/CefSharp/wiki/General-Usage#2-how-do-you-call-a-javascript-method-that-returns-a-result for additional examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1