by

## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
• Oct 10 2016 09:45
User @amaitland unbanned @aolko
• Oct 10 2016 09:36
@amaitland banned @aolko
• Feb 11 2016 05:56
User @amaitland unbanned @miyakedev from this room
• Feb 10 2016 19:59
@amaitland banned @miyakedev
Alex Maitland
@amaitland

If some web app page has hooked on to a JS event like this :

You can search through the Chromium code at https://cs.chromium.org/

2 replies
Alex Maitland
@amaitland

Is there an option to turn new windows into tabs? The reports i want to download open new windows. That is, when i click the button to generate the report, a new window opens with the report. Is there an option to turn that into a tab? I don't want it to steal focus.

@chacham2 Is the popup a GET or a POST request?

As @mitchcapper has stated there is no built in TAB functionality, CEF provides a no frills browser in terms of UI components. There are options, I just need some more information before I can make a meaningful suggestion.

There is only a single DownloadHandler per browser instance. You can use a UrlRequestClient to make a request independent of download functionality, get a byte[] as a result. You should be able to create multiple concurrent requests using this method. Again need specifics about what sort of request the report generation makes to say anything specific.

On the cefsharp winform, how to reload on the 503 error?

@quinguyenhuu You can use the LoadError event and then perform a Reload.

(In some instances you may need to call Load again passing in the same Url)

More specifically, is there a way to configure C# enums to be serialized to js strings instead of numbers?

@dperez3 Not easily, you can turn your object into a Json string using then return JsonString in your method and that will get turned into an object. We can look at adding some sort of property decorator attribute to customise the behaviour of object conversion.

Alex Maitland
@amaitland

Has anyone successfully integrated a IResourceHandler to handle a WebSocket or Server Side Events stream?

@andrewfinnell WebSockets definitely aren't supported using a ResourceHandler, this is a known limitation of CEF.

Unfortunately it appears that the Schema Handlers for Cef cannot handle a persistent connection. Is that true?

SchemeHandler is not for persistent connections (CEF would need to add support for that).

CEF does have a CefServer/CefServer.html) class that's capable of handling both Http and WebSocket requests, I just haven't found the time to add the relevant wrapper classes yet. Pull requests welcome.

That was what I did, create an object and in that object call a method where the form was closed or minimized but nothing happened

@andreivann17 As per https://github.com/cefsharp/CefSharp/wiki/Questions-&-Support#no-images-of-code please no images of code.

You may need to dispatch on the main thread, it may not being called on the primary dispatcher

The methods are executed on the ThreadPool, to access UI functions like Close and Minimaize you will need to invoke your method onto the UI Thread. In WinForms you can use https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.begininvoke?view=netcore-3.1

don't you have a little program or a program for me to understand a little bit? I would really appreciate it

For simple cases you should use CefSharp.PostMessage, if you only need to execute a few basic methods then this is the best option. See https://github.com/cefsharp/CefSharp/issues/2775#issuecomment-498454221

Alex Maitland
@amaitland

The JavascriptMessageReceived event will pass in a sender argument, you can cast that to ChromiumWebBrowser then call BeginInvoke to execute your code on the UI Thread.

The example on MSDN is pretty simple, really all you need https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.begininvoke?view=netcore-3.1#System_Windows_Forms_Control_BeginInvoke_System_Delegate_

Just posting an Image of an error without anything else isn't sufficient information.

help to dispose usercontrol in cefsharp?

Alex Maitland
@amaitland

thank you for giving me deeper understaning of this problematics. Just last question.

@Kebechet Just as a followup CEF has just added support for making DevTools calls without enabling remote debugging, this makes taking a screenshot pretty trivial using https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot

This will be available in the upcoming 83.4.x release (which should be out in the next few days).

but (cefsharp.winform)chromiumWebChromium.DrawtoBitMap() doesn't work, is there anything way to get

@mazhuping If you are still interested then the upcoming 83.4.x version will have the ability to take a screenshot through DevTools without having to enable remote debugging.

@Kebechet @mazhuping See cefsharp/CefSharp@eee134c for an example.

I will look at providing a nicer interface to wrap the CEF methods in the coming weeks, for now you can try something like code in the link.

Alex Maitland
@amaitland

But, in Chrome, if someone hooks like that, Chrome figures out dynamically that it needs to override it's CTRL+F and does not show Find box then.

• Execute the following in DevTools
let myFn = function (e)
{
e.preventDefault();
let charCode = String.fromCharCode(e.which).toLowerCase();
if(e.ctrlKey && charCode == 'f')
{
// do something with CTRL + F
}
};

window.addEventListener("keydown", myFn);
• Press Ctrl->F
The Find box isn't displayed.
Testing with Chrome 83.0.4103.116
Andrew T. Finnell
@andrewfinnell
Is anyone able to confirm that intercepting and processing Server-Side Events and/or WebSockets with CEF is impossible?
deAtog
@deAtog
@amaitland Thanks for the insight a couple months back.. I ended up customizing the CefSharp nuget packages to address the issue.. However I think #3161 would be a more appropriate long-term solution.
IvanPong
@IvanPong
@amaitland Hi, I've spent a couple days on this, but I have no clue what is happening. I have a fairly large project by now. I'm using SendMouseClickEvent, but nothing is happening. I didn't swap out dlls, I added a SchemeHandler and some other stuff, but nothing's happening. It just stopped working, and I can't figure out what change caused that. The mouse coordinates are still being fed to the browser though, mousing over something highlights it. I'm using Cefsharp.Offscreen. Everything else appears to be working fine. To debug, I tried sequencing SendMouseClick calls with delays between them to simulate mouse click. I modified CefEventFlags, but nothing happened.
chacham2
@chacham2

@mitchcapper Thank you. Will have to look at the lifespan manager in the wpf example code. I hope i get a grasp on it.

@amaitland Looking right now. FORM element is POST, the generate report button is BUTTON with an onClick that calls a js function on the page, which submits the form via:

      formObj.target = "_blank";

formObj.action = "<url fragment to run report and options>";

formObj.submit();

formObj.target = "";

formObj.action= "<url fragment to setup report and options>";


When i run it manually in chrome, the pdf opens in another tab. When i run it from cefsharp, it pops up a new window. Ideally, i would just want to capture the report and not show anything to the end user. (Then again, allowing for the option of seeing it, somehow, might be nice, but that is absolutely not my main concern right now.)

Andrew T. Finnell
@andrewfinnell
@amaitland I didn't see your previous comment, apology for spamming if I did. So with CefServer it appears it binds to a TCP Port. If that is the case, then it would be just as effective for us to create websocket server myself, yes? Or does it also work over something that doesn't require binding at the Network Layer?
IvanPong
@IvanPong
@amaitland Just to clarify, This could be a bug, in which case I'll remove it myself. If there is a common mistake that people make here, then any help is appreciated.
chacham2
@chacham2

@mitchcapper I'm having a hard time understanding the ILifeSpanHandler example, perhaps because i am using Forms instead of WPF and a couple of the methods belong to WPF. Not really sure, much of this is new to me. My specific issue is with chromiumWebBrowser.Dispatcher.Invoke() and Window.GetWindow() that do not seem to be available. When i started to search about these calls, i got the impression they are specific to WPF. If so, i am not sure what the Forms equivalent would be.

In any case, am i going about this wrongly? I want the pdf to be downloaded, and that is already done thanx to the advice here about the disable-pdf-extension option. My issue now is that the Window that hosts the pdf shows up. I'd rather it didn't show up at all, so it does not steal focus, and so that the application is quiet overall. Nothing more than the main form (which hosts the browser control) need be shown.

I'll reiterate the question so as to (hopefully!) be more clear. Chromium (Windows A) goes to a page with a button that opens Window B. Window B displays a generated PDF. By adding the disable-pdf-extension option and implementing IDownloadHandler, the PDF is saved to disk. Great! However, Window B still shows. Is there a way to have Window B not show? If it must show, making it invisible, minimizing it, or anything that keeps it from stealing focus should do as well.

Alex Maitland
@amaitland

I'm using Cefsharp.Offscreen. Everything else appears to be working fine. To debug, I tried sequencing SendMouseClick calls with delays between them to simulate mouse click. I modified CefEventFlags, but nothing happened.

@IvanPong The WPF version uses SendMouseClickEvent internally, far as I know everything is working as expected. Just saying it stopped working doesn't give me enough information to even speculate. I'd suggest you test the WPF example at the same version and compare the output (add some logging).

Alex Maitland
@amaitland

FORM element is POST, the generate report button is BUTTON with an onClick that calls a js function on the page, which submits the form via:

@chacham2 POST is much harder to deal with then GET unfortunately. How much of this code is under your control exactly? Is there much data in the POST body? If you change to a GET request you can intercept the OnBeforePopup call, cancel the popup creation then submit a URLRequest (you can parse the GET request and reconstruct the POST request if required).

Remember you can execute javascript and change just about anything on the page in question, so even if you don't have control of how the original page is constricted you can change things to suite your requirements.

When i run it from cefsharp, it pops up a new window.

CefSharp is just one of many Chromium Embedded Framework(CEF) wrappers, opening as a popup is the default CEF behaviour as it does not provide a tabs implementation currently. Based on your previous comments regarding providing multiple download handlers you will likely be better off using a URLRequest.

So with CefServer it appears it binds to a TCP Port. If that is the case, then it would be just as effective for us to create websocket server myself, yes? Or does it also work over something that doesn't require binding at the Network Layer?

@andrewfinnell It's my understanding it's a wrapper around the internal HttpServer implementation provided by Chromium, see https://bitbucket.org/chromiumembedded/cef/issues/2278/expose-internal-http-websockets-server

Requests would be made over TCP. Using any websocket server would achieve the same result.

You are welcome to ask on https://magpcss.org/ceforum for a second opinion, it's my understanding that there's still no means of intercepting websocket requests (https://magpcss.org/ceforum/viewtopic.php?f=6&t=16376#p40095)

Just to clarify, This could be a bug, in which case I'll remove it myself. If there is a common mistake that people make here, then any help is appreciated.

@IvanPong Without seeing you code I just don't know.

Alex Maitland
@amaitland

I'm having a hard time understanding the ILifeSpanHandler example, perhaps because i am using Forms instead of WPF and a couple of the methods belong to WPF. Not really sure, much of this is new to me. My specific issue is with chromiumWebBrowser.Dispatcher.Invoke() and Window.GetWindow() that do not seem to be available. When i started to search about these calls, i got the impression they are specific to WPF. If so, i am not sure what the Forms equivalent would be.

@chacham2 If you are using WinForms then you should look at the WinForms specific examples.See https://github.com/cefsharp/CefSharp/blob/cefsharp/83/CefSharp.WinForms.Example/Handlers/LifeSpanHandler.cs

Alex Maitland
@amaitland

However, Window B still shows. Is there a way to have Window B not show? If it must show, making it invisible, minimizing it, or anything that keeps it from stealing focus should do as well.

@chacham2 You can customise the popup creation in OnBeforePopup The
http://cefsharp.github.io/api/81.3.x/html/P_CefSharp_IWindowInfo_Style.htm and http://cefsharp.github.io/api/81.3.x/html/P_CefSharp_IWindowInfo_ExStyle.htm properties allow for standard Win32 styles.

You should be able to set the window to minimised, change it's width/height so it's hidden if required.
http://cefsharp.github.io/api/81.3.x/html/T_CefSharp_IWindowInfo.htm

It really depends on your requirements.

change it's width/height so it's hidden if required.

Actually removing the WS_VISIBLE style should be tried before this.

chacham2
@chacham2

@amaitland Thank you for all the attention.

How much of this code is under your control exactly?

The website code is not under my control.

Is there much data in the POST body?

I do not want to rewrite the request (unless i have to) because the javascript seems complicated and, should the report change, i would have to analyze the entire thing all over again. So, controlling that popup seems like the best approach to me. (Feel free to educate me otherwise.)

so even if you don't have control of how the original page is constricted you can change things to suite your requirements.

Based on some searches, i wonder if i could somehow just remove the one line of javascript from their code: formObj.target = "_blank"; Or, perhaps replace "_blank" with "_target".

That was one of the things i was looking at earlier, but unfortunately, was completely bewildered. I didn't understand where it was called or how. It hasn't "clicked" yet. Maybe i should take a second look.

Otoh, i could just download them all in serial. As long as i can tell when the report finishes loading, so i can move onto the next one without changing the file name or location too early. The idea of multiple handlers was to allow the program not to wait. I have no idea which is best. (Which is why i am asking for your generous help here.)

If you are using WinForms then you should look at the WinForms specific examples.

Oops! I made an assumption. The reply said to look at the wpf example, and i never bothered checking for a WinForms equivalent. I think i will look at that tomorrow.

Alex Maitland
@amaitland

i could just download them all in serial. As long as i can tell when the report finishes loading, so i can move onto the next one without changing the file name or location too early. The idea of multiple handlers was to allow the program not to wait. I have no idea which is best

@chacham2 Downloading them in sequence will be much much simpler. You can easily tell when a file has finished downloading in IDownloadHandler.

The simplest approach is to just

Based on some searches, i wonder if i could somehow just remove the one line of javascript from their code: formObj.target = "_blank"; Or, perhaps replace "_blank" with "_target".

You could try using a hidden iframe and specifying the target as the frame name, that's probably the minimum number of changes if you want to go with this approach.

If you are happen with a minimised window then that's the approach I'd try first, modifying the windowInfo.Style in OnBeforePopup should be pretty trivial.
code_wei
@zero1988
@amaitland I want to upload folder, so I use 'webkitdirectory' in the input tag like this : <input type="file" webkitdirectory /> . If the folder structure is like root\child\child\demo.txt, the relative path got is child\demo.txt. But in Chrome, the relative path got is root\child\child\demo.txt. Is this a bug ? How can I get the full relative path?
Alex Maitland
@amaitland

I want to upload folder, so I use 'webkitdirectory' in the input tag like this : <input type="file" webkitdirectory /> . If the folder structure is like root\child\child\demo.txt, the relative path got is child\demo.txt. But in Chrome, the relative path got is root\child\child\demo.txt. Is this a bug ? How can I get the full relative path?

@zero1988 Sounds like a bug, you should ask on https://magpcss.org/ceforum/viewforum.php?f=6 for clarification before opening an issue on https://bitbucket.org/chromiumembedded/cef/issues?status=new&status=open

ahmet hakan
@coromantee_gitlab

I have a problem that the virtual keyboard that opens when the text fields are clicked on the touch screen remains in front of the textboxes and the web page does not scroll up automatically

@coromantee_gitlab How does Chrome behave on the same website? This sort of behaviour is pretty common using Chrome in my experience and not really a CefSharp specific.

can I manually scroll upwards accordingly

Maybe you can use javascript to and scroll the element into view. The only option for scrolling the browser is to use javascript currently.

@amaitland First of all, thank you very much for trying answer everyone's question and helping.
There is no problem when I open the web page with chrome. The virtual keyboard does not stay on top of the textboxes and the page scrolls up automatically.
When I tried minimal.example and opened google.com, when I clicked the search box, the virtual keyboard remained at the top of the textbox. (The cursor automatically goes to the minimal.example's address bar, so I don't know if this finding is misleading.).
I created a table like this: (windows 10 pro is HP tablet, others pcpos witch touchscreen)
Cef v67 + windows 10 pro -> keyboard stays on top of the textbox.
Cef v81 + windows 10 pro -> keyboard stays on top of the textbox.
Cef v83 + windows 10 pro -> keyboard stays on top of the textbox.
Cef v81 + windows 10 LTSC -> keyboard stays on top of textbox.
Cef v83 + windows 10 LTSC -> keyboard stays on top of the textbox.
Cef v81 + windows 10 LTSB -> Keyboard goes / disappears after one or two seconds after Textbox is clicked
Cef v83 + windows 10 LTSB -> Keyboard goes / disappears after one or two seconds after Textbox is clicked
Cef v67 + windows 10 LTSB -> keyboard is ok, it doesn't stay on top of the textbox and the web page scrolls up automatically.
Thanks again.. Sorry for my bad english

IvanPong
@IvanPong
@amaitland I've verified that both onmouseup and onmousedown work, just not onclick.
chacham2
@chacham2

@amaitland I didn't realize that could be hidden. This seems to work quite well.

windowInfo.Style = ProcessWindowStyle.Hidden

Should i be closing the windows? If we download 60 or 120 reports, that'll be a lot of open windows. Or is not an issue since they are all hidden and blank?

Krish Puri
@krishpuri

Hi, I'm using the latest version of CefSharp for WinForms available via Nuget and am trying to simulate mouse clicks using the SendMouseClickEvent. Here's what my code looks like:

browser.GetBrowserHost().SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.LeftMouseButton);
browser.GetBrowser().GetHost().SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.LeftMouseButton);
Thread.Sleep(20);

The code above works correctly when the browser window is maximized and the element I'm trying to click is in view on the screen. However, whenever the element is out of view somewhere on a scroll-able browser page, the click is never registered at the right position. When I set the window state to normal instead of maximize, the mouse click always happens outside the application window on the screen.

This is the Javascript function I use to get the coordinates of the element I want to click:

function findPos(element)
{
var curleft = 0;
var curtop = 0;

if (element.offsetParent)
{
do
{
curleft += element.offsetLeft;
curtop += element.offsetTop;
} while (element = element.offsetParent);

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

The code above returns the correct coordinates on the page, but CEF simulates the click on the screen instead of inside the browser window.
Not sure if this was meant to be like this, but would really appreciate some help on this. Thank you!

Edit: To test the location of the clicks, I send a Right Click instead of Left Click.

Marcio Miguel
@MarcioMiguel
cleaner production environment of cefcharp, because I don't want to send my client the .pak, swiftshader and locale files to my client. Can someone help me?
chacham2
@chacham2
Where should i report spelling mistakes (if at all)? For example, WebBrowserExtensions.ExecuteScriptAsyncWhenPageLoaded Method has "The script will only be executed on first page load, subsiquent page loads will be ignored" "subsiquent" should be "subsequent" and there ought to be a period (full stop) on the end.
chacham2
@chacham2
I did some searching to find out how to wait for a page to load before continuing in code. I came up with using the LoadingStateChanged event to change a class variable, and looping (in the UI thread) until it changes (with some sleeping and Application.DoEvents so it doesn't freeze.) I see there is also a ChromiumWebBrowser.IsLoading property. Could i just use that instead of using a variable and the event?
Alex Maitland
@amaitland

The virtual keyboard does not stay on top of the textboxes and the page scrolls up automatically.

@coromantee_gitlab What build of Windows 10 are you using? Are you using tablet mode?

How exactly does Chrome behave? Is it resizing the whole window? If so then that's something you'd have to implement in your application.

I see you've already opened https://bitbucket.org/chromiumembedded/cef/issues/2807/when-the-on-screen-keyboard-is-turned-on

As the problem reproduces with cefclient it's best if you move the conversation to https://magpcss.org/ceforum/index.php

Post a link back here and I'll comment if required.

I've verified that both onmouseup and onmousedown work, just not onclick.

@IvanPong The WPF project uses SendMouseClickEvent internally, try loading your web page using the MimimalExample.

https://github.com/cefsharp/CefSharp.MinimalExample

If that doesn't work then I'd suggest you test with cefclient see https://github.com/cefsharp/CefSharp/blob/master/CONTRIBUTING.md#cefsharp-vs-chromium-embedded-frameworkcef
It could be a bug in CEF.

Alex Maitland
@amaitland

CefSharp for WinForms available via Nuget and am trying to simulate mouse clicks using the SendMouseClickEvent

@krishpuri The SendMouseClickEvent is intended to be used with the WPF/OffScreen implementations, whilst it should work in the WinForms version it's not something that I've tested for a long time.

The CEF/Chromium behaviour might be different than expected.

How does it behave if you use x = 0, y= 0? Does the mouse click occur in the upper left corner of the view port?

However, whenever the element is out of view somewhere on a scroll-able browser page, the click is never registered at the right position.

You can only send mouse click events to within the view port. If the element is not visible you will have to scroll it into view.

This is the Javascript function I use to get the coordinates of the element I want to click:

You need the position relative to the top left corner, not it's positin within the document

If you are simply trying to click a button then I'd generally recommend a much simpler approach, focus the element in javascript then send the Enter key press. See https://stackoverflow.com/a/53452094/4583726

cleaner production environment of cefcharp,

@MarcioMiguel What do you mean by this exactly?

because I don't want to send my client the .pak, swiftshader and locale files to my client

See https://github.com/cefsharp/CefSharp/wiki/Output-files-description-table-%28Redistribution%29 for a description. Whilst a number of files are marked as optional the browser will work though won't display correctly without the files.

In general I'd strongly advise against trying to pick and choose which files to include, just include them all.

You can have msbuild copy the files to a subfolder if you'd like to keep them out of your bin folder.

Can someone help me?

If you wish to proceed with this against my advice then I'd suggest you ask for help on https://magpcss.org/ceforum/index.php
It's not something that I support personally, it's time consuming and frankly not worth the effort. Disk space and bandwidth are cheap compared to my time.

has "The script will only be executed on first page load, subsiquent page loads will be ignored" "subsiquent" should be "subsequent" and there ought to be a period (full stop) on the end.

@chacham2 If you can please submit a PR with any proposed changes.

Alex Maitland
@amaitland

I did some searching to find out how to wait for a page to load before continuing in code. I came up with using the LoadingStateChanged event to change a class variable, and looping (in the UI thread) until it changes (with some sleeping and Application.DoEvents so it doesn't freeze.) I see there is also a ChromiumWebBrowser.IsLoading property. Could i just use that instead of using a variable and the event?

@chacham2 When asking a question please include some basic information, see https://github.com/cefsharp/CefSharp/wiki/Questions-&-Support#when-asking-a-question-please-include-the-following (you need to include at least the first few dot points to make my life easier)

Strongly discouraged to loop waiting for the page to load. I'd suggest something like

You can look at the source :smile: See https://github.com/cefsharp/CefSharp/blob/cefsharp/83/CefSharp.OffScreen/ChromiumWebBrowser.cs#L891

Alex Maitland
@amaitland

@/all Version 83.4.20 is now available on Nuget.org
You can now directly access the DevTools protocol without enabling remote debugging see #3158
Hi averyone!! Does anybody know why HTML5 validations is not working for me?? I know time ago this functionality wasn't implemented yet but not sure the status nowadays :S Example: <input type="number" class="form-control form-control-lg" placeholder="00" required>` Neither the required nor type validation is working for me