Saving a stupid title text. struggling for 2 days now

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

Saving a stupid title text. struggling for 2 days now

Postby andla71 » Sun Aug 28, 2022 8:04 pm

So it ended with me giving up.

The problem occurs when I choose a different tab in my custom application.
It is similar to a standard browser where you can open new tabs.
I can then run a script in one tab, open another, and run a different one.
Each tab with its own browser window.

So I try this on a blog service where I've created a blog.
When I run the script while I'm watching (tab active) it just works fine. Title is saved fine when exit the page and open it up again.
BUT!
When I run the script and quickly select another tab it just doesn't work. It is specifically for the title for some reason. If I insert text below the title the text is saved but not the title.
I figure it has to do with cefsharp mouseclick event. It behaves differently when the tab is not active.

I use these two functions for mouse click
Code: Select all
        public void MouseLeftDown(ChromiumWebBrowser _browser, int x, int y)
        {
            _browser.GetBrowser().GetHost()
                .SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
            Thread.Sleep(15);
        }

        public void MouseLeftUp(ChromiumWebBrowser _browser, int x, int y)
        {
            _browser.GetBrowser().GetHost()
                .SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
            Thread.Sleep(15);
        }


Here is the code I use to figure out the element position.
Code: Select all
function findPos(el)
{
const rect = el.getBoundingClientRect();
  return {
    X: rect.left + window.scrollX + (rect.width / 2),
    Y: rect.top + window.scrollY + (rect.height / 2)
  };

}
function findScroll(obj)
    {
        var curleft = 0;
        var curtop = 0;

        if (obj.parentElement)
        {
            do
            {
                curleft += obj.scrollLeft;
                curtop += obj.scrollTop;
            } while (obj = obj.parentElement);

            return { X: curleft,Y: curtop};
        }
    }


I hope someone has some good advice to share because these 2 days of struggle have been frustrating. ;)
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Saving a stupid title text. struggling for 2 days now

Postby amaitland » Sun Aug 28, 2022 10:37 pm

You can only send mouse and keyboard events to an active browser.

What are you emulating the mouse for exactly?
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Saving a stupid title text. struggling for 2 days now

Postby andla71 » Mon Aug 29, 2022 7:39 am

amaitland wrote:You can only send mouse and keyboard events to an active browser.

What are you emulating the mouse for exactly?


Hi Amaitland!
All browsers in every tab are active. I have done tests on for example Canva that require a mouse click to upload a file. I manage to run multiple tabs for Canva upload. All platforms that have an upload feature require mouse clicks. I have never made it work with dispatching click events. I think it is because of a security layer in the browser.

What I do in this particular case is that I'm sending mouse events to activate a field. This particular blog platform has tantrums. Dispatch events work in some cases but do not set focus on the contenteditable field for the Title.
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Saving a stupid title text. struggling for 2 days now

Postby amaitland » Mon Aug 29, 2022 2:38 pm

Are you using WinForms or WPF?
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Saving a stupid title text. struggling for 2 days now

Postby andla71 » Mon Aug 29, 2022 3:06 pm

It's a windows form app so I believe it is WinForm.
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Saving a stupid title text. struggling for 2 days now

Postby amaitland » Mon Aug 29, 2022 3:26 pm

You'll need to keep the tab active until the operation has completed. Disabling UI elements temporarily to prevent user interaction is not uncommon.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Saving a stupid title text. struggling for 2 days now

Postby andla71 » Mon Aug 29, 2022 5:06 pm

Thank you Amaitland.
How does the WebHost detect that I'm in another tab?
Is there a way to simulate that I'm accessing the tab when I'm not?
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Saving a stupid title text. struggling for 2 days now

Postby andla71 » Mon Aug 29, 2022 7:28 pm

Did this test
Code: Select all
window.focus();
alert(document.hasFocus());


It is true when the tab is focused and false when I switched tabs.
Even if I simulate mouse click it doesn't activate.

Is there a way to override this behavior in cefsharp?
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm

Re: Saving a stupid title text. struggling for 2 days now

Postby andla71 » Mon Aug 29, 2022 7:48 pm

Maybe I can do this kind of?
Code: Select all
document.hasFocus=null;//Prevent host to detect focus


Well, actually I think we need to have a function that returns true.
I have not tried this on the problematic host because it is just a waste of time.
But in case someone faces the same problem I'll just give you some ideas.
Please share ideas so that we can get smarter.
andla71
Techie
 
Posts: 28
Joined: Sat Oct 31, 2020 12:19 pm


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 127 guests

cron