I noticed that when I run the JavaScript code below in the console of my browser, it works perfectly fine. However, when I try to use this code in CefSharp, it returns null. I am currently using CefSharp version 100.0.120-pre. Does CefSharp 100.0.120-pre support the promises of 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;
})();
I am trying to integrate this JavaScript code with CefSharp in C#. Can someone please review my code and help me understand why CefSharp is returning 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());