View Web Console When Using TWebBrowser

Viewed 351

I am using a TWebBrowser to display a web page that is created in Vue, but I currently have no way to debug situations where functionality of the page works in a browser (IE and Chrome) but does not work from the TWebBrowser. I am looking for a way to access the web console or dev tools from within Delphi when displaying a TWebBrowser so that I can determine what exactly is preventing functionality of a page from working.

I have tried to implement a TDeveloperConsoleMessageReceiver class that uses the IDeveloperConsoleMessageReceiver interface. This class is created when the TWebBrowser is initialized. Then on TWebBrowserDocumentComplete I implemented the following function:

procedure TTTWebBrowser.WebBrowserDocumentCompleted(ASender: TObject; const pDisp: IDispatch; const URL: OleVariant);
var
   Target : IOleCommandTarget;
   Action : Cardinal;
   Params : OleVariant;
const
   IDM_ADDCONSOLEMESSAGERECEIVER = 3800;
   CGID_MSHTML: TGUID = '{DE4BA900-59CA-11CF-9592-444553540000}';
begin
   CallUserDocumentCompletedEvent(pDisp, URL);
   if Assigned(WebBrowser.Document) then begin
     Target := IOleCommandTarget(WebBrowser.Document);
     Action := IDM_ADDCONSOLEMESSAGERECEIVER;
     Params := EmptyParam;
     Target.Exec(@CGID_MSHTML, Action, OLECMDEXECOPT_DODEFAULT, IDeveloperConsoleMessageReceiver(DeveloperConsole), Params);
   end;
end;

Where DeveloperConsole is the DeveloperConsoleMessageReceiver class created on initialization. When the above function is run I get an access violation on the Exec.

How would I properly utilize DeveloperConsoleMessageReceiver or is there another way to debug a TWebBrowser from within Delphi?

1 Answers

I use NavigateComplete2 event and it works. Here is my code:

procedure TMainForm.WBNavigateComplete2(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
const
  CGID_MSHTML: TGUID = '{DE4BA900-59CA-11CF-9592-444553540000}';
  IDM_ADDCONSOLEMESSAGERECEIVER = 3800;
var
  vin, vout: OleVariant;
begin
  if FDebugConsole then // Call it only once
    Exit;
  vout := Null;
  vin := Self as IDeveloperConsoleMessageReceiver;
  (WB.Document as IOleCommandTarget).Exec(@CGID_MSHTML, IDM_ADDCONSOLEMESSAGERECEIVER, OLECMDEXECOPT_DODEFAULT
    ,vin, vout);
  FDebugConsole := True;
end;

You have to call IDM_ADDCONSOLEMESSAGERECEIVER only once and later you can navigate to other pages and console messages will continue to arrive. My main form implements IDeveloperConsoleMessageReceiver interface.

Related