Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#2

Re: EmbeddedWB "angezeigten" Text kopieren

  Alt 5. Dez 2008, 10:45
Hallo

In meinem Mini Webbrowser Demo habe ich es mal so gemacht:

Delphi-Quellcode:
procedure WB_GetDocumentSourceToStream(Document: IDispatch; Stream: TStream);
// Save a TWebbrowser Document to a Stream
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
begin
  Assert(Assigned(Document));
  Stream.Size := 0;
  Stream.Position := 0;
  if Document.QueryInterface(IPersistStreamInit,
    PersistStreamInit) = S_OK then
  begin
    StreamAdapter := TStreamAdapter.Create(Stream, soReference);
    PersistStreamInit.Save(StreamAdapter, False);
    StreamAdapter := nil;
  end;
end;

function WB_GetDocumentSourceToString(Document: IDispatch): string;
// Save a Webbrowser Document to a string
var
  Stream: TStringStream;
begin
  Result := '';
  Stream := TStringStream.Create('');
  try
    WB_GetDocumentSourceToStream(Document, Stream);
    Result := StringReplace(Stream.Datastring, #$A#9, #$D#$A, [rfReplaceAll]);
    Result := StringReplace(Result, #$A, #$D#$A, [rfReplaceAll]);
  finally
    Stream.Free;
  end;
end;


function WB_GetPlainText(WB: TWebbrowser; s: TStrings): string;
var
  IDoc: IHTMLDocument2;
  Strl: TStringList;
  sHTMLFile: string;
  v: Variant;
begin
  sHTMLFile := WB_GetDocumentSourceToString(WB.Document);
  Strl := TStringList.Create;
  try
    Strl.Add(sHTMLFile);
    Idoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;
    try
      IDoc.designMode := 'on';
      while IDoc.readyState <> 'completedo Application.ProcessMessages;
      v := VarArrayCreate([0, 0], VarVariant);
      v[0] := Strl.Text;
      IDoc.write(PSafeArray(System.TVarData(v).VArray));
      IDoc.designMode := 'off';
      while IDoc.readyState <> 'completedo Application.ProcessMessages;
      s.Text := IDoc.body.innerText;
    finally
      IDoc := nil;
    end;
  finally
    Strl.Free;
  end;
end;


Ohne Webbrowser (mit und Indy TidHTTP) könntest du es so machen:

Delphi-Quellcode:
uses
  MSHTML, ActiveX, ComObj;

procedure WB_GetPlainText(AURL: string): string;
var
  IDoc: IHTMLDocument2;
  Strl: TStringList;
  v: Variant;
  i: Integer;
begin
  Result := '';
  Strl := TStringList.Create;
  try
    Strl.Text := idHTTP1.Get(AURL);
    Idoc := CreateComObject(Class_HTMLDocument) as IHTMLDocument2;
    try
      IDoc.designMode := 'on';
      while IDoc.readyState <> 'completedo
        Application.ProcessMessages;
      v := VarArrayCreate([0, 0], VarVariant);
      v[0] := Strl.Text;
      IDoc.write(PSafeArray(System.TVarData(v).VArray));
      IDoc.designMode := 'off';
      while IDoc.readyState <> 'completedo
        Application.ProcessMessages;
      Result := IDoc.body.innerText;
    finally
      IDoc := nil;
    end;
  finally
    Strl.Free;
  end;
end;
Thomas
  Mit Zitat antworten Zitat