Sunday, 15 May 2022

cefSharp How to realize Action#2 without using delays

I want to do three actions on the web site by using cefSharp.

Action#1 is clicking on "Download" link.

Action#2 is waiting for loading links in the same document.

Action#3 is getting the list

Please take a look this actions here

In my code below there is not "Action#2", if i put some delay in C# between two actions its works! Also when im debugging step by step by putting breakpoint its works too.

Im newbie in JavaScript and i dont know how to do it by using MutationObserver or DOMInserted. Please help me to realize "Action#2" without using delays

private async void Button5_Click(object sender, EventArgs e)
{
    //First action
    string jsScript1 = @"
    document.querySelector('#dle-content > div.section > ul > li:nth-child(3)').click();
    ";
    await Task.WhenAll(chrome.EvaluateScriptAsync(jsScript1));

    //Third action
    string jsScript2 = @"
    (function()
    {
    var elements = Array.from(document.querySelectorAll('.cdn_download_item span:first-child'));
    var linksArray = new Array();
    for (element of elements) {
    linksArray.push(element.innerText);
    }
    return linksArray;
    })();
    ";

    var task = chrome.EvaluateScriptAsync(jsScript2);
    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());
}

Here is my final JavaScript code

It works in the standard browser

But in cefSharp, How to return value from this line console.log(linksArray); As i know cefSharp does not return value from "console.log" is not it?

var target = document.body;
const config = { 
    childList: true,
    attributes: true, 
    characterData: true, 
    subtree: true, 
    //attributeFilter: ["id"],
    attributeOldValue: true, 
    characterDataOldValue: true 
}

const callback = function(mutations)
{
    var linksArray = new Array();
    if(document.querySelector(".cdn_download_item"))
    {
    var elements = Array.from(document.querySelectorAll('.cdn_download_item span:first-child'));
    for (element of elements) {
        linksArray.push(element.innerText);
    }
    console.log(linksArray); //HERE!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
}

const observer = new MutationObserver(callback);
observer.observe(target, config);
document.querySelector('#dle-content > div.section > ul > li:nth-child(3)').click();


from cefSharp How to realize Action#2 without using delays

No comments:

Post a Comment