[SOLVED] Cef OnBeforePopup doesn't fire when targetname same

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

[SOLVED] Cef OnBeforePopup doesn't fire when targetname same

Postby salihcantekin » Mon Apr 17, 2017 3:24 am

Hi,

I want to explaint the situation with an example,

I have report form and i call it with "REPORT_FORM" as targetname (window.open(...,"REPORT_FORM"); with the first click, OnBeforePopup event fires and i create my new browser form with SetAsPopup and SetAsChild methods.
when i click at the second the event doesn't fire and i can't open new my form. It is problem with me. I want to open new my browser form even targetname is same the previous one.

If i make it Child form, i can't open. If i don't make it, i can open but at this time, "window.opener" or "window.parent" codes aren't working on the javascript.
So i need both of them. New browser but working js codes :)


In this situation, what way do i use?
Last edited by salihcantekin on Thu May 04, 2017 3:06 am, edited 1 time in total.
salihcantekin
Newbie
 
Posts: 8
Joined: Mon Apr 17, 2017 3:17 am

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby HarmlessDave » Mon Apr 17, 2017 11:50 am

You should use the OnBeforeNavigate event, you could cancel that navigation and open a new window instead. You need extra bookkeeping though, since OnBeforePopup is followed by OnBeforeNavigate. You need to know to let that OnBeforeNavigate go through untouched.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby salihcantekin » Wed Apr 19, 2017 1:35 am

Thanks for answering but,

i can't open new window for every link. It may navigate a page which isn't popup. What should i do in this situation.

Also, i couldn't find the "OnBeforeNavigate" event. There is "OnBeforeBrowse" in IRequestHandler
salihcantekin
Newbie
 
Posts: 8
Joined: Mon Apr 17, 2017 3:17 am

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby HarmlessDave » Wed Apr 19, 2017 12:49 pm

OnBeforeNavigation is the render process version of OnBeforeBrowse:
Code: Select all
http://magpcss.org/ceforum/apidocs3/projects/(default)/CefRenderProcessHandler.html


We use it together with the OnLoadingStateChange event as replacements for the IE web control BeforeNavigate and DocumentComplete events.

You can use OnBeforeBrowse instead, but the point is if you want to override normal HTML behavior and force open new windows sometimes then you can use one of the "before" events to do it.

>> when i click at the second the event doesn't fire and i can't open new my form

To be clear, are you trying to have multiple child windows of a single parent, all with the same name "REPORT_FORM" ?

What about having your logic that opens the child window, open it with a different name each time - REPORT_FORM_1, REPORT_FORM_2, etc.?

If you control the server or are willing to inject JavaScript to re-write the page then you can do this with the HTML. If not, you might be able to do it in the OnBeforePopup
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby salihcantekin » Thu Apr 20, 2017 7:14 am

Hello Again,

>> To be clear, are you trying to have multiple child windows of a single parent, all with the same name "REPORT_FORM" ?
- Yes that is exactly what i wanted but without the target name must be same.

For example, i have a report page and there are 5 link to open report detail page. Whenever i click any link it opens new windows with name as "REPORT_FORM" . OnBeforePopup fires and i make it child form and popup form.
After that i have parent form and child form which comes from parent. I come back to main form and click another report form link and then OnBeforePopup doesn't fire because of i made it child. If i didn't make it child and popup, the event would be fired. when i click that link "OnBeforePupup" doesn't fire but the link opens on the child form. so i still have one child form. If i don't make them child, the event would be fired and i would open new popup form.

In Scenario that i make it child, i can have only one popup form and all javascript codes between parent and child will be worked.
In Scenario that i don't make it child, i can have more than one popup form but javascript codes won't be worked.

That i want is have more than one popup forms evet targetname was same and at the same time i want all javascript codes between parent and its childiren working :)

In this situation, if i can, change the targetname as different for each. What event can i change it in ?

Note: I can't change web pages contents.


the following code is i use in OnBeforePopup

public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName,
WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings,
ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;

ChromiumWebBrowser chromiumBrowser = null;

var windowX = windowInfo.X;
var windowY = windowInfo.Y;
var windowWidth = (windowInfo.Width == int.MinValue) ? 600 : windowInfo.Width;
var windowHeight = (windowInfo.Height == int.MinValue) ? 800 : windowInfo.Height;

chromiumWebBrowser.Invoke(new Action(() =>
{
var owner = chromiumWebBrowser.FindForm();
chromiumBrowser = new ChromiumWebBrowser(targetUrl)
{
LifeSpanHandler = this
};

chromiumBrowser.SetAsPopup();
chromiumBrowser.CreateControl();

var popup = new BrowserForm
{
Left = windowX,
Top = windowY,
Width = windowWidth,
Height = windowHeight
};


popup.Adjust(targetUrl, chromiumBrowser); // Navigate to new Url and show form.

var rect = chromiumBrowser.ClientRectangle;
windowInfo.SetAsChild(chromiumBrowser.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);

}));

newBrowser = chromiumBrowser;

return false;
}
salihcantekin
Newbie
 
Posts: 8
Joined: Mon Apr 17, 2017 3:17 am

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby amaitland » Thu Apr 20, 2017 5:12 pm

If you don't need post data or access to the parent window from javascript then cancel the popup creation and load the url in a new browser instance.

Code: Select all
popup.Adjust(targetUrl, chromiumBrowser); // Navigate to new Url and show form.


This block is code is strange, is the comment correct? Why are you performing a navigation?
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby HarmlessDave » Fri Apr 21, 2017 2:24 pm

If you do need the parent-child relationship you could build and execute your own JavaScript string on the browser object, doing a window.open .. REPORT_FORM_ %d where %d is a counter variable in your own code that you ++ each time. sprintf ( buffer, "REPORT_FORM_%d", my_window_unique_id_counter ); my_window_unique_id_counter++ ;
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby salihcantekin » Sat Apr 22, 2017 3:46 pm

How can i change the targetname ?
salihcantekin
Newbie
 
Posts: 8
Joined: Mon Apr 17, 2017 3:17 am

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby HarmlessDave » Sun Apr 23, 2017 8:04 pm

To do your own window.open:

Code: Select all
int global_form_index = 1;
...

void do_open(CefRefPtr<CefBrowser> browser)
{
const char* base_script = "window.open('https://www.google.com/', 'REPORT_FORM_%d')";
char jscript[8192];
sprintf_s(jscript, base_script, global_form_index);

global_form_index++;  // so next time is 2, 3, 4...

CefRefPtr<CefFrame> frame = browser->GetMainFrame();
frame->ExecuteJavaScript(jscript, frame->GetURL(), 0);   // no return value to say whether it worked
}


With ExecuteJavaScript you can either run your own code, you can build script code that re-writes the existing source of the page -- getElementByName / Id, set the target or the inner HTML, or have submit or onclick call your own function instead of the existing one, ...
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Cef OnBeforePopup doesn't fire when targetname same

Postby salihcantekin » Mon Apr 24, 2017 6:55 am

I think i doesn't work because there are many windows on it,

if i check it for all of them, the performance goes down.

Buy the way, .Net WebBrowser has a property as "RegisterAsBrowser" if it is true, you can only one window with the same targetname, if it isn't you can open as many windows as you want.

I need the property on CefBrowser
salihcantekin
Newbie
 
Posts: 8
Joined: Mon Apr 17, 2017 3:17 am

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 32 guests