-
Notifications
You must be signed in to change notification settings - Fork 78
/
unChromeMessage.pas
86 lines (67 loc) · 1.57 KB
/
unChromeMessage.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
74
75
76
77
78
79
80
81
82
83
84
85
86
unit unChromeMessage;
interface
uses
Contnrs, uCEFChromium, Classes, utils_strings, SysUtils, Generics.Collections,
Windows, utils_dvalue, unConfig, Dialogs;
type
TWatchSubject = class(TObject)
public
listChome: TList<TChromium>;
// 注册消息监听
procedure attach(observer: TChromium);
procedure unAttach(observer: TChromium);
// 通知所有监听
procedure notifyAll(aMsg, aFrameName: string);
constructor Create; overload;
destructor Destroy; override;
end;
var
subject: TWatchSubject;
implementation
{ TSubject }
procedure TWatchSubject.attach(observer: TChromium);
var
iIndex: Integer;
begin
iIndex := listChome.IndexOf(observer);
if iIndex = -1 then
listChome.Add(observer);
end;
constructor TWatchSubject.Create;
begin
inherited;
listChome := TList<TChromium>.Create;
end;
destructor TWatchSubject.Destroy;
begin
listChome.Free;
inherited;
end;
procedure TWatchSubject.notifyAll(aMsg, aFrameName: string);
var
i: Integer;
observer: TChromium;
strFunc: string;
begin
aMsg := StringReplace(aMsg, '"', '\"', [rfReplaceAll]);
strFunc := 'if(window.onMsg) window.onMsg("' + aMsg + '")';
for i := 0 to listChome.Count - 1 do
begin
observer := listChome.Items[i];
observer.ExecuteJavaScript(strFunc,
observer.DocumentURL, aFrameName);
end;
end;
procedure TWatchSubject.unAttach(observer: TChromium);
var
iIndex: Integer;
begin
iIndex := listChome.IndexOf(observer);
if iIndex > -1 then
listChome.Delete(iIndex);
end;
initialization
subject := TWatchSubject.Create;
finalization
subject.Free;
end.