Einzelnen Beitrag anzeigen

berens

Registriert seit: 3. Sep 2004
431 Beiträge
 
Delphi 2010 Professional
 
#6

AW: alerts im Webbrowser unterdrücken

  Alt 27. Mär 2019, 10:20
Bitte verzeiht mir das Ausgraben dieses uralten Threads, aber da ich zu diesem Thema hier nicht fündig geworden bin, möchte ich gerne unsere Community an meinen Erkenntnissen teilhaben lassen.

Ich selbst habe gerade das Problem, dass eine Website, die ich mit TWebBrowser darstellen lassen möchte, einen StackÜberlauf (in JavaScript?) erzeugt. Da ich keinen Einfluss auf die Homepage habe, ist es sehr ärgerlich, dass mein Programm folgende Meldung anzeigt und so lange stehen bleibt, bis ich sie beantworte:

Zitat:
---------------------------
Meldung von Webseite
---------------------------
Stapelüberlauf bei Zeile: 1
---------------------------
OK
---------------------------
Die "Silent"-Option und weitere Tipps hier (Quelltext nach "alert(" durchsuchen etc.) haben mir nicht weitergeholfen. Auch die Tatsache, dass diese Website den original Internet Explorer (also ohne Delphi) für ~10 Sekunden zum "einfrieren" bringt und dann aber *ohne* Fehlermeldung weitermacht, ist sehr ennervierend: Das "Original" verhält sich mal wieder anders als die "Kopie" (die eingebettete Version).

Die aktuelle Lösung habe ich auf https://stackoverflow.com/questions/...after-an-error gefunden.
Letztendlich muss man TWebbrowser ableiten und die Prozedur "Exec" überschreiben, um vorzugeben, dass alle Fehler "erfolgreich behandelt" wären.

Nachfolgend der Code, aber eine Warnung und Denkanstoß: Unter https://blogs.msdn.microsoft.com/iei...e-silent-flag/ wird darauf hingewiesen, dass das gnadenlose Unterdrücken von Popups Probleme bereiten kann, z.B. wenn der PC sich innerhalb des Browsers an einem Proxy oder WLAN-Controller anmelden muss (öffentliches WLAN o.ä.). Bitte behaltet das im Hinterkopf!

Delphi-Quellcode:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SHDocVw, ActiveX;

type
  TMeinWebBrowser = class(SHDocVw.TWebBrowser, IOleCommandTarget)
  private
    function QueryStatus(CmdGroup: PGUID; cCmds: Cardinal; prgCmds: POleCmd;
      CmdText: POleCmdText): HRESULT; stdcall;
    function Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
      const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
  end;

  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    wb: TMeinWebBrowser;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

function TMeinWebBrowser.QueryStatus(CmdGroup: PGUID; cCmds: Cardinal;
  prgCmds: POleCmd; CmdText: POleCmdText): HRESULT; stdcall;
begin
  Result := S_OK;
end;

function TMeinWebBrowser.Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD;
  const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
begin
  // presume that all commands can be executed; for list of available commands
  // see SHDocVw.pas unit, using this event you can suppress or create custom
  // events for more than just script error dialogs, there are commands like
  // undo, redo, refresh, open, save, print etc. etc.
  // be careful, because not all command results are meaningful, like the one
  // with script error message boxes, I would expect that if you return S_OK,
  // the error dialog will be displayed, but it's vice-versa
  Result := S_OK;

  // there's a script error in the currently executed script, so
  if nCmdID = OLECMDID_SHOWSCRIPTERROR then
  begin
    // if you return S_FALSE, the script error dialog is shown
    // Result := S_FALSE;
    // if you return S_OK, the script error dialog is suppressed
    Result := S_OK;
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  wb := TMeinWebBrowser.Create(Self);
  wb.Silent := True;
  TWinControl(wb).Parent := Self;
  wb.SetBounds(10,10,400,400);

  wb.Navigate('http://www.homepage.sample');
end;

end.
Hier noch ein wenig Keyword-Spam für zukünftige Suchanfragen:
TWebbrowser Alert Popup unterdrücken verhindern JavaScript Debug deaktivieren Fehlermeldung abfangen SilentMode ScriptErrorsSuppressed Meldung von Webseite
Delphi 10.4 32-Bit auf Windows 10 Pro 64-Bit, ehem. Delphi 2010 32-Bit auf Windows 10 Pro 64-Bit
  Mit Zitat antworten Zitat