Autor Beitrag
FriFra
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 557

Win XP Prof, Win XP Home,Win Server 2003,Win 98SE,Win 2000,Win NT4,Win 3.11,Suse Linux 7.3 Prof,Suse Linux 8.0 Prof
D2k5 Prof, D7 Prof, D5 Standard, D3 Prof, K3 Prof
BeitragVerfasst: Sa 26.04.03 21:19 
ich will eine Javascriptfunktion einer Seite in einem TWebBrowser aufrufen:
ausblenden Quelltext
1:
WebBrowser1.OleObject.Document.MyShow('Toastbrot');					

dies führt allerdings immer zu einem OLE-Fehler, obwohl die Funktion "MyShow" existiert...


Es ist jedoch möglich HTML-Elemente direkt zu manipulieren z.B.:
ausblenden Quelltext
1:
WebBrowser1.OleObject.Document.all.meinAbsatz.innerHTML :='<a href="JavaScript:MyShow(''Testttt'');">Test</a>t';					
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: Sa 26.04.03 21:31 
Hi,

Mit ExecuteScript() kannst du eine JavaScript Funktion ausführen:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
uses 
  MSHTML_TLB, SHDocVw; 

function ExecuteScript(doc: IHTMLDocument2; script: string; language: string): Boolean; 
var 
  win: IHTMLWindow2; 
  Olelanguage: Olevariant; 
begin 
  if doc <> nil then 
  begin 
    try 
      win := doc.parentWindow; 
      if win <> nil then 
      begin 
        try 
          Olelanguage := language; 
          win.ExecScript(script, Olelanguage); 
        finally 
          win := nil; 
        end; 
      end; 
    finally 
      doc := nil; 
    end; 
  end; 
end;


Hab die Funktion mal zum Einloggen in den GMX Account
verwendet:

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
function LoginGMX(AKennung, APasswort: string): Boolean;
const
  IEFields: array[1..4] of string = ('INPUT', 'text', 'INPUT', 'password');
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
  IEFieldsCounter: Integer;
begin
  ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        Document := WB.Document;
        // if GMX page...
        if Pos('GMX - Homepage', Document.Title) <> 0 then
          // count forms on document and iterate through its forms
          IEFieldsCounter := 0;
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;

            // iterate through elements
            for i := ovElements.Length - 1 downto 0 do
            begin
              try
              // if input fields found, try to fill them out
                if (ovElements.item(i).tagName = IEFields[1]) and
                  (ovElements.item(i).type = IEFields[2]) then
                  begin
                    ovElements.item(i).Value := AKennung;
                    inc(IEFieldsCounter);
                  end;

                if (ovElements.item(i).tagName = IEFields[3]) and
                  (ovElements.item(i).type = IEFields[4]) then
                  begin
                    ovElements.item(i).Value := APasswort;
                    inc(IEFieldsCounter);
                  end;
              except
                // failed...
              end;
            end; { for i...}
          end;  { for m }
      end;  { idoc <> nil }
    end; { wb <> nil }
    // if the fields are filled in, submit.
    if IEFieldsCounter = 3 then ExecuteScript(iDoc1, 'document.login.submit()', 'JavaScript');
  end;  { for k }
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  LoginGMX('username@gmx.net', 'pswd');
end;
FriFra Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 557

Win XP Prof, Win XP Home,Win Server 2003,Win 98SE,Win 2000,Win NT4,Win 3.11,Suse Linux 7.3 Prof,Suse Linux 8.0 Prof
D2k5 Prof, D7 Prof, D5 Standard, D3 Prof, K3 Prof
BeitragVerfasst: Sa 26.04.03 22:48 
:D Danke für den Lösungsansatz...

:idea: Nach Durchsicht Deines Codes bin ich auf eine wesentlich einfachere Lösung gekommen:
ausblenden Quelltext
1:
WebBrowser1.OleObject.Document.parentWindow.MyShow('Toastbrot');