Ver Mensaje Individual
  #2  
Antiguo 24-06-2008
Avatar de xEsk
[xEsk] xEsk is offline
Miembro Premium
 
Registrado: feb 2006
Posts: 454
Reputación: 19
xEsk Va por buen camino
Hola,

Te he preparado una de las más grandes chapuzas que he escrito en mucho tiempo xDDDD (esta subido al FTP del club porqué habia problemas con mostrar el HTML en el mensaje)

Para probarlo, métele un TEdit, un TButton, un TTimer y un TWebBrowser, y aquí la chapuza:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, mshtml, ExtCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    WebBrowser1: TWebBrowser;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure WebBrowser1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Left:=-MaxInt;
  Timer1.Enabled:=False;
  Timer1.Interval:=500;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  html: String;
  htmlFichero: TStringList;

begin
  // html del fichero de salida
  html:='<_script type="text/javascript" src="http://www.google.com/jsapi">' +
        '<_script type="text/javascript">google.load("language", "1");' +
        '
<_script type="text/javascript">'
+ 'function Traducir() { var text = "%s"; var srcLang = "es"; var dstLang = "en"; ' + 'google.language.translate(text, srcLang, dstLang, function(result) ' + '{ if (!result.error) { var resultado = document.getElementById("result"); ' + 'resultado.innerHTML = result.translation; } else alert(result.error.message);});}' + ''; // asignamos el texto a traducir html:=Format(html, [Edit1.Text]); // guardamos esto en un fichero htmlFichero:=TStringList.Create; try htmlFichero.Text:=html; htmlFichero.SaveToFile('tmp.html'); finally htmlFichero.Free; end; // cargamos este fichero al webbrowser WebBrowser1.Navigate(ExtractFilePath(Application.ExeName) + 'tmp.html'); end; procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin Timer1.Enabled:=True; end; procedure TForm1.Timer1Timer(Sender: TObject); var iall : IHTMLElement; begin Timer1.Enabled:=False; if Assigned(WebBrowser1.Document) then begin iall:=(WebBrowser1.Document AS IHTMLDocument2).body; if Assigned(iall) then begin while Assigned(iall.parentElement) do iall:=iall.parentElement; ShowMessage(iall.outerText); end; end; // ahora ya podemos borrar el ficher temporal DeleteFile('tmp.html'); end; end.

Lo que hace este código, es crear un fichero temporal html, donde se le mete el texto a traducir usando tu HTML (le he quitado el title, porqué sinó también me lo mostraba xD). Luego carga este HTML y captura el texto resultante del TWebBrowser xD

Si te preguntas para qué se necesita el TTimer, pues es otra chapuza... xD Ya que si el código de obtener el texto lo metia justo después de cargar la página, éste no me devolvía bien el "outerText"...

Para ocultar el TWebBrowser, como el "visible:=false" y el "Height y Width a 0" no funcionaba, pues lo he mandando a tomar viento... sacándolo de la ventana xD

Ya te digo, que es una cadena de chapuzas, que juntas realizan lo que tu querías xDDDD

Saludos.

P.D.: Si el mensaje que te devuelve es '', sube el tiempo del Timer... xD
Responder Con Cita