My question is, how to do this in Window/non-osr mode?
I can see that CefBrowser_N defines methods like sendMouseEvent, but I am unable to make effective use of them. Here is what I am trying.
Because methods like sendMouseEvent are protected and my code which needs to call this resides outside org.cef.browser, I have changed the specifier to public. Again, because the documentation says we should not create instances of CefBrowser_N directly and instead opt to use CefBrowserFactory.create, I am defining a method CefBrowserFactory.createWrBrowser which returns an instance of CefBrowserWr, as the CefBrowser interface doesn't expose sendMouseEvent. The new method is implemented like this:
- Code: Select all
public static CefBrowserWr createWrBrowser (
CefClient client, String url, CefRequestContext context) {
return new CefBrowserWr(client, url, context);
}
In a separate class, MyBrowserHandler, I am creating an instance of CefBrowserWr like this:
- Code: Select all
CefClient client = MainFrame.getCefInstance().createClient();
CefBrowserWr browser = CefBrowserFactory.createWrBrowser(client, Properties.URL, null);
It might be of interest that I am passing null for context. Not sure what that does but I did it the way it is done in CefClient class. Check the source of CefClient.createBrowser and notify if I should be doing it some other way. Now that I am able to use sendMouseEvent, I am doing this when the page (and the element to be clicked) is loaded:
- Code: Select all
MouseEvent mouseEvent = new MouseEvent(
browser.getUIComponent(),
MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis() + 1000, // wait 1 second
0,
300, 60, // x, y co-ordinates
1, false
);
browser.sendMouseEvent(mouseEvent);
I am unsure about the co-ordinates being accurate, but anyways, I am listening to document.onclick in JavaScript and logging "Clicked" when a click happens in the document. When I run my program, no click event is fired. When I manually click anywhere in the document, I see the message "Clicked". What am I doing wrong? Also, is there a cleaner way to send mouse clicks. Like an alternative to CefSharp's Browser.GetBrowser().GetHost().SendMouseClickEvent in Jcef?
I probably should mention that clicking the button should open a file dialog. When trying the same script in Jcef's dev console, file dialog opens as expected. How can I open the dialog without opening dev toolbar? Or if there is no way to do this without dev toolbar, how can I run my JS automatically from within devtoolbar?