-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unit1.pas
73 lines (64 loc) · 2.3 KB
/
Unit1.pas
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
72
73
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, MsHtml;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
procedure FormShow(Sender: TObject);
procedure WebBrowser1NavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject);
begin
WebBrowser1.Navigate('https://www.facebook.com');
end;
procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
Doc2 : IHtmlDocument2;
Script : IHtmlDOMNode;
HTMLWindow: IHTMLWindow2;
jsloader : TStringList;
begin
Doc2 := Webbrowser1.Document as IHtmlDocument2;
if Assigned(Doc2.body) then
begin
Script := Doc2.createElement('script') as IHTMLDOMNode;
jsloader := TStringList.Create();
jsloader.Add('function loadScript(url, callback){');
jsloader.Add('var script = document.createElement("script")');
jsloader.Add('script.type = "text/javascript";');
jsloader.Add('if (script.readyState){ //IE');
jsloader.Add(' script.onreadystatechange = function(){');
jsloader.Add(' if (script.readyState == "loaded" || ');
jsloader.Add(' script.readyState == "complete"){');
jsloader.Add(' script.onreadystatechange = null;');
jsloader.Add(' callback();');
jsloader.Add(' }');
jsloader.Add(' };');
jsloader.Add(' } else { //Others');
jsloader.Add(' script.onload = function(){');
jsloader.Add(' callback();');
jsloader.Add(' };');
jsloader.Add(' }');
jsloader.Add(' script.src = url;');
jsloader.Add(' document.getElementsByTagName("head")[0].appendChild(script);');
jsloader.Add('}');
(Script as IHTMLScriptElement).text := jsloader.GetText();
(Doc2.body as IHtmlDomNode).appendChild(Script);
HTMLWindow := Doc2.parentWindow;
if Assigned(HTMLWindow) then
HTMLWindow.execScript('loadScript("https://code.jquery.com/jquery-2.2.4.min.js", function(){alert("jQuery version " +jQuery.fn.jquery);})', 'JavaScript')
end;
end;
end.