This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWebViewHook.cs
282 lines (231 loc) · 6.85 KB
/
WebViewHook.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* WebHook 0.7 - https://github.com/willnode/WebViewHook/ - MIT */
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class WebViewHook : ScriptableObject
{
Object webView;
EditorWindow host;
object hostCache;
static Type _T;
static FieldInfo _Parent;
static MethodInfo _Show, _Hide, _Back, _Reload, _Forward;
static MethodInfo _SetSizeAndPosition;
static MethodInfo _InitWebView;
static MethodInfo _SetDelegateObject;
static MethodInfo _AllowRightClickMenu;
static MethodInfo _ShowDevTools;
static MethodInfo _DefineScriptObject;
static MethodInfo _SetHostView;
static MethodInfo _ExecuteJavascript;
static MethodInfo _LoadURL;
static MethodInfo _HasApplicationFocus;
static MethodInfo _SetApplicationFocus;
static Func<Rect, Rect> _Unclip;
static WebViewHook()
{
_T = typeof(Editor).Assembly.GetTypes().First(x => x.Name == "WebView");
_Parent = typeof(EditorWindow).GetField("m_Parent", Instance);
_Show = (_T.GetMethod("Show", Instance));
_Hide = (_T.GetMethod("Hide", Instance));
_Back = (_T.GetMethod("Back", Instance));
_Reload = (_T.GetMethod("Reload", Instance));
_Forward = (_T.GetMethod("Forward", Instance));
_InitWebView = (_T.GetMethod("InitWebView", Instance));
_SetSizeAndPosition = (_T.GetMethod("SetSizeAndPosition", Instance));
_SetHostView = (_T.GetMethod("SetHostView", Instance));
_AllowRightClickMenu = (_T.GetMethod("AllowRightClickMenu", Instance));
_SetDelegateObject = (_T.GetMethod("SetDelegateObject", Instance));
_ShowDevTools = (_T.GetMethod("ShowDevTools", Instance));
_DefineScriptObject = (_T.GetMethod("DefineScriptObject", Instance));
_ExecuteJavascript = (_T.GetMethod("ExecuteJavascript", Instance));
_LoadURL = (_T.GetMethod("LoadURL", Instance));
_HasApplicationFocus = (_T.GetMethod("HasApplicationFocus", Instance));
_SetApplicationFocus = (_T.GetMethod("SetApplicationFocus", Instance));
_Unclip = (Func<Rect, Rect>)Delegate.CreateDelegate(typeof(Func<Rect, Rect>), typeof(GUI).Assembly.GetTypes()
.First(x => x.Name == "GUIClip").GetMethod("Unclip", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(Rect) }, null));
}
~WebViewHook()
{
OnDisable();
}
void OnEnable()
{
if (!webView)
{
webView = CreateInstance(_T);
webView.hideFlags = HideFlags.DontSave;
this.hideFlags = HideFlags.DontSave;
}
}
void OnDisable()
{
if (webView)
{
Detach();
}
}
void OnDestroy()
{
DestroyImmediate(webView);
webView = null;
}
public bool Hook(EditorWindow host)
{
if (host == this.host) return false;
if (!webView)
OnEnable();
// initialization go here
Invoke(_InitWebView, _Parent.GetValue(hostCache = (this.host = host)), 0, 0, 1, 1, false);
Invoke(_SetDelegateObject, this);
Invoke(_AllowRightClickMenu, true);
return true;
}
public void Detach()
{
Invoke(_SetHostView, this.hostCache = null);
}
void SetHostView(object host)
{
Invoke(_SetHostView, this.hostCache = host);
Hide();
Show();
}
void SetSizeAndPosition(Rect position)
{
Invoke(_SetSizeAndPosition, (int)position.x, (int)position.y, (int)position.width, (int)position.height);
}
void OnGUI() { }
public void OnGUI(Rect r)
{
if (host)
{
var h = _Parent.GetValue(host);
if (hostCache != h)
SetHostView(h);
else
Invoke(_SetHostView, h);
}
SetSizeAndPosition(_Unclip(r));
}
public void AllowRightClickMenu(bool yes)
{
Invoke(_AllowRightClickMenu, yes);
}
public void Forward()
{
Invoke(_Forward);
}
public void Back()
{
Invoke(_Back);
}
public void Show()
{
Invoke(_Show);
}
public void Hide()
{
Invoke(_Hide);
}
public void Reload()
{
Invoke(_Reload);
}
public bool HasApplicationFocus()
{
return (bool)_HasApplicationFocus.Invoke(webView, null);
}
public void SetApplicationFocus(bool focus)
{
Invoke(_SetApplicationFocus, focus);
}
protected void ShowDevTools()
{
// This method may not work
Invoke(_ShowDevTools);
}
public void LoadURL(string url)
{
Invoke(_LoadURL, url);
}
public void LoadHTML(string html)
{
Invoke(_LoadURL, "data:text/html;charset=utf-8," + html);
}
public void LoadFile(string path)
{
Invoke(_LoadURL, "file:///" + path);
}
protected void DefineScriptObject(string path, ScriptableObject obj)
{
// This method has unknown behavior
Invoke(_DefineScriptObject, path, obj);
}
protected void SetDelegateObject(ScriptableObject obj)
{
// Only set into this object
Invoke(_SetDelegateObject, obj);
}
public void ExecuteJavascript(string js)
{
Invoke(_ExecuteJavascript, js);
}
void Invoke(MethodInfo m, params object[] args)
{
try
{
m.Invoke(webView, args);
}
catch (Exception) { }
}
const BindingFlags Instance = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
/* Default bindings for SetDelegateObject */
public Action<string> LoadError;
public Action InitScripting;
public Action<string> LocationChanged;
protected virtual void OnLocationChanged(string url)
{
if (LocationChanged != null)
LocationChanged(url);
}
protected virtual void OnLoadError(string url)
{
if (LoadError != null)
LoadError(url);
else
Debug.LogError("WebView has failed to load " + url);
}
protected virtual void OnInitScripting()
{
if (InitScripting != null)
InitScripting();
}
protected virtual void OnOpenExternalLink(string url)
{
// This binding may not work
}
protected virtual void OnWebViewDirty()
{
// This binding may not work
}
protected virtual void OnDownloadProgress(string id, string message, ulong bytes, ulong total)
{
// This binding may not work
}
protected virtual void OnBatchMode()
{
// This binding may not work
}
protected virtual void OnReceiveTitle(string title)
{
// This binding may not work
}
protected virtual void OnDomainReload()
{
// This binding may not work
}
}