0

Delphi 10.4.2

I'm working with TEdgeBrowser for the first time and am playing with some ideas in a small demo app I created. I have so far been able to programmatically Navigate to the website, login (fill in the User Name and Password and click the Log In button), click a button (My Cases) to be taken to another page, then filled in an edit field and a combo box to lookup a specific case. So far all is well. Now that the case is available I have the option to click either "History" or "Service information". I clicked on the "History" button and want the new window to open in another TEdgeBrowser component. Here is the code I'm using to attempt to do this but it is not working.

procedure TfrmMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
  Args: TNewWindowRequestedEventArgs);
begin
  //* Natigate to the history page
  pgeMain.ActivePage := tabHistory;

  //* Open link in another WebView2 (new tab):
  Args.ArgsInterface.Set_NewWindow(EdgeBrowserHistory.DefaultInterface);
end;

Please note that I have the following in the FormShow event.

procedure TfrmMain.FormShow(Sender: TObject);
begin
  EdgeBrowser.UserDataFolder := 'C:\Temp\';

  EdgeBrowserHistory.CreateWebView;
end;

Note: My main browser is call EdgeBrowser

procedure TfrmMain.btnGoClick(Sender: TObject);
begin
  pgeMain.ActivePage := tabBrowser;
  EdgeBrowser.Navigate(edtWebSiteURL.Text);
end;

Reading other post on this site, I thought maybe it had to do with having a TEdgeBrowser on another page of a TPageControl, so I moved it off to the main form and it still does not work.

If I do not have any code in the EdgeBrowserNewWindowRequested event it will open to a new window just fine. Anyone know what I need to do to get it to work?

STWizard
  • 31
  • 4

3 Answers3

0

You just need to navigate the second TEdgeBrowser to the URL you want it to go to. (Also, note that when you use the Args interface to get a PWideChar, you need a matching call to CoTaskMemFree). And, assuming you may have other "tabs" or any other buttons/links that could trigger NewWindowRequest, you might need some logic to handle those other cases as the code below will go to the History tab for any NewWindowRequest. I'm not sure if you need to set EdgeBrowserHistory.UserDataFolder as well or if it will default to the same as the first.

Try something like this:

procedure TfrmMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser; Args: TNewWindowRequestedEventArgs);
var
  PUri: PWideChar;
  Url: string;
begin
  //* Navigate to the history page
  pgeMain.ActivePage := tabHistory;

  //* Open link in another WebView2 (new tab):
  Args.ArgsInterface.Get_uri(PUri);
  Url := PUri;
  CoTaskMemFree(PUri);
  EdgeBrowserHistory.Navigate(Url);
  Args.ArgsInterface.Set_Handled(1);
end;
Greg Dawson
  • 145
  • 8
0

Greeting Greg,

Thank you for your reply. I did try to do as you suggested yesterday while trying to do anything I could think of to make this work, but, because I have to log into this site, it does not like trying to access this page using Navigate().

Here you can see the main TEdgeBrowser on the left and the second one on the right. And yes, this time it did populate it with something at least (A Warning Message). Any other ideas on getting this to work in a another EdgeBrowser?

enter image description here

If we cannot get this to work in another EdgeBrowser which is the preferred way, I'm ok with the page displaying in the current EdgeBrowser using the following code:

procedure TfrmMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
  Args: TNewWindowRequestedEventArgs);
begin
  //* Open link in current WebView2:
  Args.ArgsInterface.Set_NewWindow(Sender.DefaultInterface);
end;

Although it works to open "History" in current EdgeBrower, there is now another issue, the back button is disabled, and if the back button is disabled, I cannot go back to the previous page and click the "Service Information" button.

If I use MS Edge to go to this website, lookup the case and click the "History" button, the website opens the page in a new tab and if I go back to the main tab of course the back button is disabled there, so I do not see anyway around this issue, unless you know of one.

So that leaves only one option, which I really don't care for much and that is letting EdgeBrower do it's own thing and open the History in a new window. This presents it own set of issues.

  1. Can the size of the new window be controlled?
  2. Can the placement of the new window be controlled?
  3. If I close my form, the two new windows (History and Service information) remain open. This is not good.

So bottom line, if this is my only option, I really need to be able to have control over those windows. Any ideas if that is possible?

Thanks again for any assistance you can provide.

STWizard
  • 31
  • 4
  • Ahh, I failed to consider that the new window might require login in. Well, I think your original approach is supposed to work as well and I saw it recommended and someone else said they got that working after initially failing. And the person did not say what they did to make it work. You might just need to add a call to Args.ArgsInterface.Set_Handled(1). – Greg Dawson Dec 22 '21 at 00:20
  • One more thing... The TEdgeBrowser component is lacking alot of interfaces added to WebView2 since it was introduced. As for me, I do not use it anymore, I use Webview4Delphi by salvadorf. link -> https://github.com/salvadordf/WebView4Delphi. Webview4Delphi is a much better, much more complete alternative, and it has plenty of examples. It will give you access to all the current Webview2 interfaces and capabilities that you will eventually need. – Greg Dawson Dec 22 '21 at 00:26
  • Greg, thanks for your reply. I currently am using CEF4Delphi by Salvadorf and it is working well. My issue with that particular product is that it requires 13 distributables files (pak, dll, dat, bin) to be shipped with it and the need to implement CEFSUBPROCESS.exe just to get it to work efficiently and the fact that each user that uses my app will open these files even if they never use the web brower part of the app. I was hoping to to use TEdgeBrowser as all of that would go away. I will try Set_Handled(1) and look into WebView4Delphi as well. If you think of anything else... – STWizard Dec 22 '21 at 12:07
  • Greg, I downloaded WebView4Delphi and see what you mean. I think it has the potential to be much better than TEdgeBrowser since it's developed by Salvadordf knowing his work on the CEF4Delphi project. I converted my demo project to use WebView4Delphi and now if I can just figure out how to use aArgs.Set_NewWindow(???); – STWizard Dec 22 '21 at 15:19
  • STWizard, maybe it was you, but your exact question was answered over in salvadorf's developer forums. https://www.briskbard.com/forum/viewtopic.php?f=13&t=1874 – Greg Dawson Dec 22 '21 at 21:27
  • Greg, LOL, Why yes, that was me. I decided to go with WebView4Delphi and I will be asking a lot more questions there. Salvadordf has always been very helpful to guide. Thanks for pointing me in that direction. – STWizard Dec 23 '21 at 15:23
0

The first approach was already correct:

Args.ArgsInterface.Set_NewWindow(EdgeBrowserHistory.DefaultInterface);

It is important that the new browser has been initialised beforehand. So call up any website beforehand with EdgeBrowserHistory.Navigate. Then it works.

Felix
  • 1