uses
MSHTML_TLB, SHDocVw, ShellAPI;
// function to execute a script functionfunction ExecuteScript(doc: IHTMLDocument2; script: string; language: string):
Boolean;
var
win: IHTMLWindow2;
Olelanguage: Olevariant;
beginif doc <> nilthenbegintry
win := doc.parentWindow;
if win <> nilthenbegintry
Olelanguage := language;
win.ExecScript(script, Olelanguage);
finally
win := nil;
end;
end;
finally
doc := nil;
end;
end;
end;
// 2 Examples how to login to gmx homepageprocedure FillInGMXForms(WB: ShDocVW_TLB.IWebbrowser2; IDoc1: IHTMLDocument2;
Document: Variant; AKennung, APasswort: string);
const
IEFields: array[1..4] ofstring = ("INPUT", "text", "INPUT", "password");
var
IEFieldsCounter: Integer;
i: Integer;
m: Integer;
ovElements: OleVariant;
beginif Pos("GMX - Homepage", Document.Title) <> 0 thenwhile WB.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;
// count forms on document and iterate through its forms
IEFieldsCounter := 0;
for m := 0 to Document.forms.Length - 1 dobegin
ovElements := Document.forms.Item(m).elements;
// iterate through elementsfor i := ovElements.Length - 1 downto 0 dobegintry// if input fields found, try to fill them outif (ovElements.item(i).tagName = IEFields[1]) and
(ovElements.item(i).type = IEFields[2]) thenbegin
ovElements.item(i).Value := AKennung;
Inc(IEFieldsCounter);
end;
if (ovElements.item(i).tagName = IEFields[3]) and
(ovElements.item(i).type = IEFields[4]) thenbegin
ovElements.item(i).Value := APasswort;
Inc(IEFieldsCounter);
end;
except// failed...end;
end; { for i...}end; { for m }// if the fields are filled in, submit.if IEFieldsCounter = 3 then
ExecuteScript(iDoc1, "document.login.submit()",
"JavaScript");
end;
function LoginGMX_IE(AKennung, APasswort: string): Boolean;
var
ShellWindow: IShellWindows;
WB: ShDocVW_TLB.IWebbrowser2;
spDisp: IDispatch;
IDoc1: IHTMLDocument2;
Document: Variant;
k: Integer;
begin
ShellWindow := CoShellWindows.Create;
// get the running instance of Internet Explorerfor k := 0 to ShellWindow.Count dobegin
spDisp := ShellWindow.Item(k);
if spDisp = nilthen
Continue;
// QueryInterface determines if an interface can be used with an object
spDisp.QueryInterface(iWebBrowser2, WB);
if WB <> nilthenbegin
WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
if iDoc1 <> nilthenbegin
WB := ShellWindow.Item(k) as ShDocVW_TLB.IWebbrowser2;
Document := WB.Document;
// if GMX page...
FillInGMXForms(WB, IDoc1, Document, AKennung, APasswort);
end; { idoc <> nil }end; { wb <> nil }end; { for k }end;
// Example 1: Navigate to the gmx homepage in the IE browser an loginprocedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(Handle,
"open",
"?wurl=www.gmx.ch",
nil,
nil,
SW_SHOW);
Sleep(2000);
LoginGMX_IE("user@gmx.net", "pswd");
end;
// Example 2: navigate to the gmx homepage in the Webbrowser an loginprocedure TForm1.Button2Click(Sender: TObject);
var
IDoc1: IHTMLDocument2;
Web: ShDocVW_TLB.IWebBrowser2;
begin
Webbrowser1.Navigate("?wurl=www.gmx.ch");
while Webbrowser1.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;
Webbrowser1.Document.QueryInterface(IHTMLDocument2, iDoc1);
Web := WebBrowser1.ControlInterface;
FillInGMXForms(Web, iDoc1, Webbrowser1.Document, "user@gmx.net", "pswd");
end;
Если Вас заинтересовала или понравилась информация по разработке на Delph - "Как выполнить JavaScript функцию", Вы можете поставить закладку в социальной сети или в своём блоге на данную страницу: Так же Вы можете задать вопрос по работе этого модуля или примера через форму обратной связи, в сообщение обязательно указывайте название или ссылку на статью!