Intercept target=_top requests

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.

Intercept target=_top requests

Postby saversa » Thu Oct 12, 2017 7:40 am

Hi,
I'm using CEF as an embedded html & js engine in my app;
when a web page has an iframe with an href pointing to target=_top, when clicking to this link the main web page is replaced by the new link.

I want to intercept this request and redirect it to the iframe instead of main frame.
I tried the CefRequestHandler::OnBeforeBrowse / CefLoadHandler::OnLoadStart, but are called when the url is already loading into main frame.

Is it possible to intercept the request to the target=_top before it's loaded to the main frame?

Thanks
saversa
Techie
 
Posts: 11
Joined: Mon Jul 31, 2017 2:49 am

Re: Intercept target=_top requests

Postby Czarek » Thu Oct 12, 2017 9:11 am

OnBeforeBrowse should be called before loading url, you should test it again. Return an appropriate value to cancel navigation.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Intercept target=_top requests

Postby saversa » Thu Oct 12, 2017 9:49 am

Ok,
I tried this code to detect if the request comes from the iframe or the main frame, and redirect the request to the iframe

Code: Select all
bool
WrapperClient::OnBeforeBrowse (CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool isRedirect)
{
   if ((request->GetResourceType() == RT_MAIN_FRAME) && (!frame->IsMain()))
   {
      // a request comes from iframe and redirected to main frame,
      // cancels it, and load to iframe
      frame->LoadURL(request->GetURL());
      return true;
   }
   return false;
}


when the iframe contains <a href=... target="_self" />, GetResourceType() returns RT_SUB_FRAME and frame->IsMain() returns false => so i think that the request comes from the iframe
instead when the iframe contains <a href=... target="_top"/>, GetResourceType() returns RT_MAIN_FRAME and frame->IsMain() return true, assuming the request comes from the top level frame instead of the iframe; i was expeting that when the target=_top request comes from the iframe, the frame->IsMain() was false...

(I hope you understand the usecase :) )

Is there any other method to detect if the url loaded by the iframe will be loaded into the toplevel frame?
saversa
Techie
 
Posts: 11
Joined: Mon Jul 31, 2017 2:49 am

Re: Intercept target=_top requests

Postby Czarek » Thu Oct 12, 2017 9:58 am

Call LoadURL after returning true. Use CefPostDelayedTask or similar.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Intercept target=_top requests

Postby saversa » Mon Oct 16, 2017 3:04 am

mmm,
after some testing, I saw that OnBeforeBrowser is called always with a reference on the main frame and not on the iframe that containts the link.

I "resolved" by converting all <a href=... target="_top"> into <a href=... target="_self"> on every iframe, every time a page is loaded.

Thanks
saversa
Techie
 
Posts: 11
Joined: Mon Jul 31, 2017 2:49 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 89 guests