首页  编辑  

追加脚本到Webbrowser

Tags: /超级猛料/OS.操作系统/IE.扩展/TWebBrowser/页面内容/   Date Created:

// 追加脚本到Webbrowser

procedure TForm1.Button1Click(Sender: TObject);

begin

 webbrowser1.Navigate('http://kingron.g4soft.net/bbs/);

 while WebBrowser1.ReadyState <READYSTATE_COMPLETE  do

     Application.ProcessMessages;

    AppendToWB(WebBrowser1,'<a href="javascript:alert(''Hello World'')">追加脚本</a>');

end;

procedure AppendToWB(WB: TWebBrowser; const html: widestring) ;

var

  Range: IHTMLTxtRange;

begin

  while not Assigned((WB.Document AS IHTMLDocument2).body) do Application.ProcessMessages;

  Range := ((WB.Document AS IHTMLDocument2).body AS IHTMLBodyElement).createTextRange;

  Range.Collapse(False) ;

  Range.PasteHTML(html) ;

end;