CEF3: How to intercept window.open?

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.

CEF3: How to intercept window.open?

Postby andybrown » Fri Aug 03, 2012 11:32 am

Hi,

I'd like to intercept the javascript window.open() call so I can wrap the popup in my custom frame window and still maintain the ability to pass the new window reference back to the calling javascript so it can call methods on the opened popup. What's the best way to go about this?

Regards,
- Andy
andybrown
Techie
 
Posts: 21
Joined: Thu May 03, 2012 10:55 am

Re: CEF3: How to intercept window.open?

Postby magreenblatt » Fri Aug 03, 2012 11:34 am

You can use CefLifeSpanHandler::OnBeforePopup.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF3: How to intercept window.open?

Postby andybrown » Mon Aug 06, 2012 9:14 am

I've got this working in a basic sense, but without this feature being implemented it's of limited use because the size, position and title of the popup cannot be set by the javascript:

browser_host_impl.cc
Code: Select all
  // TODO(cef): Figure out how to populate these values.
  // See: http://crbug.com/110510
  CefPopupFeatures features;


Do you have any idea where this one is in the stack of issues to be addressed?

For reference, to get a popup browser window hosted in my own custom parent I'm following this flow:

1. OnBeforePopup: Set windowInfo.style to something without WS_VISIBLE to avoid an ugly flash while reparenting takes place because it doesn't seem to be possible to call windowInfo.SetAsChild() at this point. Now create my custom parent and a new ClientHandler for the popup.

2. ClientHandler::OnAfterCreated (for the popup): Reparent the browser window into the custom parent, remove the WS_CAPTION style that all non-child windows automatically get, resize to fit within the custom parent and then show the browser window.

This all works but feels a little hacky. It could be simplified if windowInfo.SetAsChild() worked during the OnBeforePopup callback.

Cheers,
- Andy
andybrown
Techie
 
Posts: 21
Joined: Thu May 03, 2012 10:55 am

Re: CEF3: How to intercept window.open?

Postby xiaowu » Tue Oct 02, 2012 3:47 pm

Hi Andy:
Were you able to override CefLifeSpanHandler::OnBeforePopup(...) to actually host the new browser in your customized window?
I am trying to do the same thing.
Your knowledge must be helpful and I appreciate your help.
Sincerely,
Michael Zhu
xiaowu
Techie
 
Posts: 13
Joined: Tue Oct 02, 2012 3:41 pm

Re: CEF3: How to intercept window.open?

Postby andybrown » Thu Oct 04, 2012 5:04 am

xiaowu wrote:Hi Andy:
Were you able to override CefLifeSpanHandler::OnBeforePopup(...) to actually host the new browser in your customized window?
I am trying to do the same thing.
Your knowledge must be helpful and I appreciate your help.
Sincerely,
Michael Zhu


Yes I was, and it is working with the limitations described above regarding size, title, location.
andybrown
Techie
 
Posts: 21
Joined: Thu May 03, 2012 10:55 am

Re: CEF3: How to intercept window.open?

Postby xiaowu » Mon Oct 22, 2012 10:51 am

Hi Andy:
I was able to create my own CWnd, then host CEF by calling info.SetAsChild(hWnd, rect);
Also by handling CefLifeSpanHandler::OnBeforePopup(...) I was able to create a second CWnd-wrapped CEF launched by JavaScript window.open(...)
Thank you very much for your help.

But I have a situation I wish you can shed some light for me.
1. Create a CWnd-wrapped CEF, e.g. named as WCef1.
2. Create another a CWnd-wrapped CEF, e.g. named as WCef2.
WCef2 is created through JavaScript loaded into WCef1 (I click on "Open Window" button)
<head>
<script>
var myWindow;
function openWin(){myWindow=window.open('','');}
function closeWin(){myWindow.close();}
function writeWin(){myWindow.document.write('hello');}
</script></head>
<body>
<button onclick="openWin()">Open Window</button>
<button onclick="closeWin()">Close Window</button>
<button onclick="writeWin()">Write Window</button>
</body>

3. Then when I click on "Write Window" button, I expect 'hello' being written into WCef2, but it does not happen!

Same JS works for my CWnd-wrapped IE browser.
I am wondering if you can help me on this.
Thank you.
Michael
xiaowu
Techie
 
Posts: 13
Joined: Tue Oct 02, 2012 3:41 pm

Re: CEF3: How to intercept window.open?

Postby Tiph » Tue Aug 26, 2014 3:16 am

Hi

I up this topic cause its exactly what i want to do, and it seems calling SetAsChild cause the UI to freeze. The UI also freeze when calling ShowWindow. (in OnBeforePopup)

any information ?

Other question :
is it possible to get the "name" of the popup on the browser object when call like that : window.open(URL,name,specs,replace)

is correct to suppose that if OnBeforePopup is called on afterCreated will be call for that call or no ? (is it thread safe ?)
Tiph
Techie
 
Posts: 13
Joined: Mon Aug 11, 2014 12:01 pm

Re: CEF3: How to intercept window.open?

Postby magreenblatt » Wed Aug 27, 2014 3:35 pm

Tiph wrote:I up this topic cause its exactly what i want to do, and it seems calling SetAsChild cause the UI to freeze. The UI also freeze when calling ShowWindow. (in OnBeforePopup)

The popup window does not yet exist when OnBeforePopup is called. What are you calling ShowWindow on?

Tiph wrote:is it possible to get the "name" of the popup on the browser object when call like that : window.open(URL,name,specs,replace)

No.

Tiph wrote:is correct to suppose that if OnBeforePopup is called on afterCreated will be call for that call or no ?

OnAfterCreated will be called after the popup window is created.

Tiph wrote:(is it thread safe ?)

Is what thread safe?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 65 guests

cron