-2

I have a problem that is really annoying when I use Twebbrowser on my application, after few minutes of surfing the web , some ads popup and when I try to leave them it shows me this message :

Are you sure you want to leave this page?

enter image description here

The question is:

How can I prevent Twebbrowser from showing such dialogs? I tried to set webbrowser.silent := true; and it just stopped the annoying javascripts errors.

I want to completely surf the net with twebbrowser silently without any popups and alerts.

If you have a solution for this please add a source example because I don't know much "not expert enough".

Do you think installing another webbrowser components will fix that? If it's please suggests me a webbrowser components cause I searched and I didn't found.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
M0HX
  • 31
  • 7
  • Stop browsing to sites like that!! – David Heffernan Jul 12 '14 at 08:26
  • Why should i stop browsing sites like that !!???? – M0HX Jul 12 '14 at 08:34
  • Browse to sites like that, and get your just desserts. Perhaps if you opted in to the latest IE version, popups could be blocked. You probably still in IE6 mode. But would be easier not to visit dodgy sites. – David Heffernan Jul 12 '14 at 08:36
  • I'm not trying to visit dodgy sites , all i am trying to do is prevent twebbrowser from showing these alerts, i don't want to see them anymore in my application ! – M0HX Jul 12 '14 at 08:42
  • If you say so. I'm sure it's a perfectly respectable website that is showing that popup. Like the BBC, or the NY Times, or Stack Overflow. Right. Anyway, if you opt in to the latest IE then you should get popup blocking. http://msdn.microsoft.com/en-us/library/ie/ee330730(v=vs.85).aspx – David Heffernan Jul 12 '14 at 08:45
  • These aren't "dodgy sites". This is something that a lot of internet marketers use to incentivize people to stick around. Many times if you click the "Stay on this page" button they'll show something different, either a discounted offer, different details, or sometimes even a totally different sales page. It's just marketing. I know lots of folks hate marketers, but it's just where web-based "special offers" has gotten to today. – David Schwartz Jul 13 '14 at 02:38

2 Answers2

1

The WebBrowser control itself does not provide the possibility to block these dialogs, so Silent does not help. I cannot provide a reference right now, but I'm sure I have read it from MSDN somewhere.

Fortunately, you can observe Windows messages and block these dialogs as you want. Here is a famous component TEmbeddedWB that implements all you need as a VCL. If you think it is too huge, you can extract related code into your project.

procedure TEmbeddedWB.FormWndProc(var AMsg: Messages.TMessage);
begin
  if AMsg.Msg = WM_ACTIVATE then
  begin
    HandleDialogBoxes(AMsg);
  end;
  FOldWindowProc(AMsg);
end;

procedure TEmbeddedWB.HandleDialogBoxes(var AMsg: Messages.TMessage);
var
  PopHandle: Integer;
  DlgCaption, DlgClss: string;
  Msg: TWMActivate;
  WI: TWindowInfo;
begin
  Msg := TWMActivate(AMsg);
  if Msg.Active = 0 then
  begin
    PopHandle := Msg.ActiveWindow;
    FillChar(WI, SizeOf(WI), 0);
    if PopHandle <> 0 then
    begin
      WI.dwStyle := Abs(GetWindowLong(PopHandle, GWL_STYLE));
      WI.dwExStyle := Abs(GetWindowLong(PopHandle, GWL_EXSTYLE));
    end;

    DlgClss := GetWinClass(PopHandle);
    if (DlgClss = 'Internet Explorer_TridentDlgFrame') or ((DlgClss = '#32770') and
      ((GetWinClass(Windows.GetParent(PopHandle)) <> 'TApplication') and
      (FindControl(Windows.GetParent(PopHandle)) = nil))) then
    begin
      DlgCaption := GetWinText(PopHandle);
      if (PopHandle <> 0) and Assigned(FOnShowDialog) then
        FOnShowDialog(Self, PopHandle, WI.dwExStyle, DlgCaption, FDialogBoxes.FNewCaption, FDialogBoxes.FDisableAll);

      if FDialogBoxes.FDisableAll then
        SendMessage(PopHandle, WM_CLOSE, 0, 0);
      if FDialogBoxes.FReplaceIcon then
        SendMessage(PopHandle, WM_SETICON, ICON_SMALL, Forms.Application.Icon.Handle);

      if FDialogBoxes.FReplaceCaption then
      begin
        DlgCaption := StringReplace(DlgCaption, 'Microsoft ', '', []);
        DlgCaption := StringReplace(DlgCaption, 'Internet Explorer', FDialogBoxes.FNewCaption, []);
        SetWindowText(PopHandle, PChar(DlgCaption));
      end;

      if FDisableErrors.FScriptErrorsSuppressed then
      begin
        if (AnsiPos('SCRIPT', AnsiUpperCase(DlgCaption)) <> 0) then
        begin
          PostMessage(PopHandle, WM_LBUTTONDOWN, 0, 0);
          PostMessage(PopHandle, WM_LBUTTONUP, 0, 0);
          SendMessage(PopHandle, WM_CLOSE, 0, 0);
          Forms.Application.ProcessMessages;
          Exit;
        end;
        if (AnsiPos('ERROR', AnsiUpperCase(DlgCaption)) <> 0) or (WI.dwExStyle = 4260097) then
        begin
          DestroyWindow(PopHandle);
          Exit;
        end;
      end;
      if FPrintOptions.FEnabled then
      begin
        bPrintOptionsEnable := True;
        if bInvokingPageSetup then
        begin
          bInvokingPageSetup := False;
          if PrintingWithOptions then
          begin
            SetWindowPos(0, 0, -4400, 0, 0, 0, 0); //SetWindowPos(Wnd, 0, -600, 0, 0, 0, 0);
            PrintingWithOptions := False;
          end;
          if FPrintOptions.FOrientation = poPortrait then
            SendDlgItemMessage(PopHandle, $0420, BM_CLICK, 0, 0)
          else
            SendDlgItemMessage(PopHandle, $0421, BM_CLICK, 0, 0);
          SetDlgItemText(PopHandle, $1FD3, PChar(FPrintOptions.FHeader));
          SetDlgItemText(PopHandle, $1FD5, PChar(FPrintOptions.FFooter));
          SetDlgItemText(PopHandle, $0483, PChar(PrintMarginStr(FPrintOptions.FMargins.FLeft)));
          SetDlgItemText(PopHandle, $0484, PChar(PrintMarginStr(FPrintOptions.FMargins.FTop)));
          SetDlgItemText(PopHandle, $0485, PChar(PrintMarginStr(FPrintOptions.FMargins.FRight)));
          SetDlgItemText(PopHandle, $0486, PChar(PrintMarginStr(FPrintOptions.FMargins.FBottom)));
          if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4) then
            PostMessage(FindWindowEx(PopHandle, 0, 'Button', nil), BM_CLICK, 0, 0) //Win2000
          else
            SendDlgItemMessage(PopHandle, 1, BM_CLICK, 0, 0);
        end;
      end;
    end;
  end;
end;
stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
  • This method does not work for ActiveX dialog boxes ; I'm disabling ActiveX and i receive this message all the time : "'One or more ActiveX controls could not be displayed because either: 1) Your current security settings prohibit running ActiveX controls on this page, or 2) You have blocked a publisher of one of the controls. As a result, the page might not display correctly.'" – delphirules Dec 11 '14 at 11:55
  • Maybe the FEATURE_RESTRICT_ACTIVEXINSTALL is what you are looking for. Check here http://msdn.microsoft.com/en-us/library/ms537169.aspx If I remember correctly, some FeatureControl flag can control ActiveX. – stanleyxu2005 Dec 11 '14 at 12:50
0

To prevent popup ads that display URL pages, you have to use the TWebBrowser's OnNewWindow2 and OnBeforeNavigate2 events to cancel the action. You have to cancel it in both events, because when OnNewWindow2 is cancelled, TWebBrowser will try to load the same URL again in the main window. So your OnBeforeNavigate2 handler needs to be coded to know when OnNewWindow2 was cancelled beforehand and for which URL. Otherwise, you would not be able to visit any site at all if you cancel everything.

As for script alerts, setting Silent to true should handle those.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770