Page 1 of 1

open popup in new tab

Posted: Tue Dec 01, 2020 10:39 pm
by ericktux
hello friends, greetings.
I am practicing with the demo "TabbedBrowser2", my question is how can I open the popup web pages in a new tab.

I have reached the event "TBrowserFrame.Chromium1BeforePopup" and I have put "Result: = true" (to avoid showing a window), but now how can I send the order to the main process so that it creates a new tab with the new link. because I guess I can't do it directly from such an event.

please, any help I appreciate

Re: open popup in new tab

Posted: Wed Dec 02, 2020 10:16 am
by salvadordf
Hi,

You can cancel the new popup window and save the url in a class field or variable. Then send a windows message to the main form to create a new tab using the saved url.

This is a quick fix for your problem but it will have problems in case the new popup window had POST information.

The perfect solution would be to create a hidden tab with an uninitialized browser. You would initialize it by calling CreateClientHandler in the TChromium.OnBeforePopup event. The PopupBrowser2 demo do something very similar to handle new popup windows.

Re: open popup in new tab

Posted: Wed Dec 02, 2020 2:53 pm
by ericktux
hello friend, thank you very much for answering.
I could achieve the cancellation of the pop-up window and save the url by putting it like this:

Code: Select all

procedure TBrowserFrame.Chromium1BeforePopup(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
  targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
  userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
  var windowInfo: TCefWindowInfo; var client: ICefClient;
  var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue;
  var noJavascriptAccess, Result: Boolean);
var
  ID_addtab: integer;
begin
  Result:=true;             // cancel popup
  mi_url_popup:=targetUrl;  // capture link URL in variable  "mi_url_popup"  (string)

  // here how can I send a message to the main form to create a new tab?
  
end;  
to send a message to the main form I have seen that they do it with "postmessage" or "sendmessage" but I don't know how to do it.

please help. :(

Re: open popup in new tab

Posted: Thu Dec 03, 2020 9:59 am
by salvadordf
You have most of the code you need in the PopupBrowser2 demo.

There are many tutorials in Internet about PostMessage. This one comes as one of the first google results when you search for "Delphi postmessage tutorial" :
https://www.cryer.co.uk/brian/delphi/ho ... essage.htm

Re: open popup in new tab

Posted: Thu Dec 03, 2020 2:52 pm
by ericktux
Thanks friend for your advice, I will review it and tell you how it went, greetings

Re: open popup in new tab

Posted: Sat Dec 05, 2020 11:42 pm
by ericktux
hello friends, I could already create a new tab from the event "TBrowserFrame.Chromium1BeforePopup" the steps I followed are:

Code: Select all

const
  CEF_INITIALIZED        = WM_APP + $100;
  CEF_DESTROYTAB      = WM_APP + $101;
  NEW_TAB                   = WM_APP + $102;   // add this code

  HOMEPAGE_URL         = 'https://www.google.com';
  DEFAULT_TAB_CAPTION = 'New tab    ';

......

    public
    procedure new_tab_crear(var aMessage : TMessage); message NEW_TAB;  // add this code
    procedure nuevo_tab(taburl: string);  // add this code
    end;

var
  MainForm: TMainForm;
  mi_url_popup: string;   // add this code

.....

procedure TMainForm.nuevo_tab(taburl: string); // create new tab
var
  TempNewTab : TBrowserTab;
  ID_addtab: integer;
begin
  // I use this code to order the tabs, because the last tab has the symbol "+" (new tab)
  MainForm.BrowserPageCtrl.HandleNeeded;
  ID_addtab:=MainForm.BrowserPageCtrl.PageCount-1;

  ///////////////// this is the main code /////////////////////////////////////////////////////////////
  TempNewTab             := TBrowserTab.Create(self, NextTabID, DEFAULT_TAB_CAPTION);
  TempNewTab.PageControl := BrowserPageCtrl;
  BrowserPageCtrl.ActivePageIndex := pred(BrowserPageCtrl.PageCount);  // esto hace que cuando abramos nuevo tab el foco pase aqui
  TempNewTab.CreateBrowser(taburl);
  /////////////////////////////////////////////////////////////////////////////////////////////////

  // I use this code to order the tabs, because the last tab has the symbol "+" (new tab)
  MainForm.BrowserPageCtrl.TabIndex:=MainForm.BrowserPageCtrl.PageCount-1;
  MainForm.BrowserPageCtrl.Pages[ID_addtab].PageIndex := MainForm.BrowserPageCtrl.PageCount-1;
end;  

procedure TMainForm.new_tab_crear(var aMessage : TMessage);
begin
     nuevo_tab(mi_url_popup);
end;
now in the event "TBrowserFrame.Chromium1BeforePopup" I use this code:

Code: Select all

procedure TBrowserFrame.Chromium1BeforePopup(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
  targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
  userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
  var windowInfo: TCefWindowInfo; var client: ICefClient;
  var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue;
  var noJavascriptAccess, Result: Boolean);
var
  ID_addtab: integer;
begin
  Result:=true;
  mi_url_popup:=targetUrl;

  //PostMessage(Handle, NEW_TAB, 0, 0);          // I do not work
  PostMessage(MainForm.Handle, NEW_TAB, 0, 0);   // It works well  :)
end;
At the moment it is working well for me, but if something I am doing wrong or can be improved, I appreciate your advice.