TadvWebbrowser GetInnerHtml of Html page

Trying to get the innerhtml with javascript I do:

  1. Tried using function GetHTML() as explained by Pieter Scheldeman: but his results in unreadable code (UTF8?)
  2. I found:
    https://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it (to get readable code)
  3. Tried:
    procedure TForm1.GetHtmlDemo;
    begin
    AdvWebBrowser1.ExecuteJavascript('function decodeHtml() {var txt = document.createElement("textarea"); txt.innerHTML = html; return txt.value;} decodeHtml();',
    //This works but gives UTF8
    //AdvWebBrowser1.ExecuteJavascript('function GetHTML(){return document.documentElement.innerHTML;} GetHTML();',
    procedure(const AValue: string)
    begin
    memo1.Lines.Text:= AValue;
    end
    );
    end;

But that does not work.

  1. How to debug this ?
  2. What's wrong ?
  3. Is there a :
  • demo ?
  • more elaborate manual for tadvWebbrowser
  • collection of snippets ? (would be very helpful)
  • a repository ?

Further to share:
I made the mistake of

  • mixing Lowercase and Uppercase in Javascript
  • breaking up the javascript as Delphicode: The Javascript is a string so use 'this'+'that'

Thanks for Help

Hi,

You can actually use JSON to parse the text:

uses
  JSON;

procedure TForm48.Button1Click(Sender: TObject);
begin
  AdvWebBrowser1.ExecuteJavascript('function GetHTML(){return unescape(document.documentElement.innerHTML);} GetHTML();',
  procedure(const AValue: string)
  begin
    memo1.Lines.Text := TJSONObject.ParseJSONValue(AValue).Value;
  end
  );
end;

We are working on a Demo, and are expanding our documentation, however, we cannot cover every piece of JavaScript code. The browser itself also doesn't expose a way to access the DOM. If that is possible, then JavaScript wouldn't be necessary to get the inner HTML of the document.

Again a blazing fast answer: and it works! Thanks!

1 Like