Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Internet
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 23-06-2008
Avatar de acertij022
acertij022 acertij022 is offline
Miembro
 
Registrado: may 2003
Ubicación: Argentina-Bs. As.
Posts: 233
Poder: 21
acertij022 Va por buen camino
Cool Invocar html y leer resultado

Como se pude invocar un html y leer su resultado mi idea es traducir por medio de google con el siguiente código:
Código PHP:
<html>
<
head>
  <
title>Traductor</title>
  <
script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript">  
    google.load("language", "1");        
  </script>
</head>

<body onload="Traducir()">
  <div id="result">
  </div>
</body>
<script type="text/javascript">
function Traducir()
{
  // obtenemos el texto y los idiomas origen y destino
  var text    = "Hola mundo";
  var srcLang = "es";
  var dstLang = "en";

  // llamada al traductor
  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);
    }
  );
}
</script>
</html> 
Desde ya gracias anticipadas
Responder Con Cita
  #2  
Antiguo 24-06-2008
Avatar de xEsk
[xEsk] xEsk is offline
Miembro Premium
 
Registrado: feb 2006
Posts: 454
Poder: 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
  #3  
Antiguo 24-06-2008
Avatar de acertij022
acertij022 acertij022 is offline
Miembro
 
Registrado: may 2003
Ubicación: Argentina-Bs. As.
Posts: 233
Poder: 21
acertij022 Va por buen camino
Gracias xEsk me sirvió a la perfección
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Invocar Funcion PHP desde form html jfontane PHP 6 14-11-2016 22:11:23
Invocar archivo html vpepen Varios 2 11-04-2008 17:53:52
leer archivos HTML jmlifi Internet 15 06-09-2005 23:19:50
Leer archivos HTML JulioGO Varios 4 20-09-2004 21:38:14
Leer desde una página <HTML> AGAG4 Varios 2 20-08-2004 02:56:43


La franja horaria es GMT +2. Ahora son las 17:10:08.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi