javascriptiframecorscefsharpdevtools

Is there a way to tell cefsharp which iframe to send javascript to?


I am creating my own app using cefsharp and using ExecuteScriptAsync to send javascript to the website being viewed. However, I ran into the problem where the element I need to work with is nested within an iframe which is nested in another iframe.

I tried doing the following:

var firstIframe = document.querySelector("iframe[title='First Iframe']"); var firstIframeWindow = firstIframe.contentWindow; var secondIframe = firstIframeWindow.document.querySelector('iframe[title="Second Iframe"]');

example of error That gets blocked by cross origin due to the iframe coming from a different domain. At first I thought I could get around this by messing with CORS. I tried all kinds of options of disabling CORS protection. Even with --disable-web-security argument and verifying CORS is letting everything through using https://webbrowsertools.com/test-cors/ I decided to try attacking this a different away.

I noticed in chrome devtools, you can select the iframe from a dropdown to change the javascript console context. Hey doing so, I can select the iframe I need to interact with and send the javascript code and interact with the DOM element. However, I am too much of a newbie to know how to tell cefsharp which iframe to send the javascript using ExecuteScriptAsync. HELP!!!

Example of devtools selecting iframe

I am 100% open to ideas. Ether fixing the cross origin error or to a method of telling cefsharp which iframe to 'focus' or whatever you want to call it for selecting the iframe to send the javascript to. Going to laugh at how simple it most likely is but figured someone could point me in the right direction.

Edit: I found something that I thought would work by implementing Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e).

    private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
    {
        if (e.Frame.IsMain)
        {
            // Execute script in the main frame
            // browser.ExecuteScriptAsync("alert('Hello from main frame');");
        }
        else if (!string.IsNullOrEmpty(e.Frame.Name) && e.Frame.Name.Contains("init_timestamp") && !e.Frame.Name.Contains("sandbox"))
        {
            // Execute script in a specific iframe
            browser.ExecuteScriptAsync("alert('Hello from iframe');");
        }
    }

I feel like this is close but comes back to needing to send the javascript to the correct iframe and not Top. Cefsharp seems to be able to detect the iframe, but when it sends to console, it sending it to Top not the iframe detected.

I did find a work around by cheating. Edited the hosts file to block that single domain (rokt.com) where this nested AD is coming from. However, I do want to display the AD. This doesnt solve the root issue of being able to send javascript to a different frame then Top.


Solution

  • While I did adjust the code, the final code that I went with is

    private static void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { 
        if (!string.IsNullOrEmpty(e.Frame.Url) && e.Frame.Url.Contains("apps.rokt.com")) { 
            string script = "setTimeout(function() {document.querySelector('button[data-e2e=\"creative.response\"][data-e2e-variant=\"negative\"]').click(); }, 3000);"; 
            e.Frame.ExecuteJavaScriptAsync(script); 
        } 
    }