-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapp.cpp
More file actions
256 lines (209 loc) · 6.61 KB
/
app.cpp
File metadata and controls
256 lines (209 loc) · 6.61 KB
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
#include "app.h"
#include "cmdline.h"
#include "path.h"
#include <wke.h>
#include <stdio.h>
#include <stdlib.h>
#include <shellapi.h>
#pragma comment(lib, "shell32.lib")
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
BOOL FixupHtmlFileUrl(LPCWSTR pathOption, LPWSTR urlBuffer, size_t bufferSize)
{
WCHAR htmlPath[MAX_PATH + 1] = { 0 };
if (pathOption[0] == 0)
{
do
{
GetWorkingPath(htmlPath, MAX_PATH, L"index.html");
if (PathFileExistsW(htmlPath))
break;
GetWorkingPath(htmlPath, MAX_PATH, L"main.html");
if (PathFileExistsW(htmlPath))
break;
GetWorkingPath(htmlPath, MAX_PATH, L"wkexe.html");
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, L"index.html");
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, L"main.html");
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, L"wkexe.html");
if (PathFileExistsW(htmlPath))
break;
return FALSE;
}
while (0);
swprintf_s(urlBuffer, bufferSize, L"file:///%s", htmlPath);
return TRUE;
}
else//if (!wcsstr(pathOption, L"://"))
{
do
{
GetWorkingPath(htmlPath, MAX_PATH, pathOption);
if (PathFileExistsW(htmlPath))
break;
GetProgramPath(htmlPath, MAX_PATH, pathOption);
if (PathFileExistsW(htmlPath))
break;
return FALSE;
}
while (0);
swprintf_s(urlBuffer, bufferSize, L"file:///%s", htmlPath);
return TRUE;
}
return FALSE;
}
BOOL FixupHtmlUrl(Application* app)
{
LPWSTR htmlOption = app->options.htmlFile;
WCHAR htmlUrl[MAX_PATH + 1] = { 0 };
// 包含 :// 说明是完整的URL
if (wcsstr(htmlOption, L"://"))
{
wcsncpy_s(app->url, MAX_PATH, htmlOption, MAX_PATH);
return TRUE;
}
// 若不是完整URL,补全之
if (FixupHtmlFileUrl(htmlOption, htmlUrl, MAX_PATH))
{
wcsncpy_s(app->url, MAX_PATH, htmlUrl, MAX_PATH);
return TRUE;
}
// 无法获得完整的URL,出错
return FALSE;
}
BOOL ProcessOptions(Application* app)
{
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
ParseOptions(argc, argv, &app->options);
LocalFree(argv);
return TRUE;
}
// 回调:点击了关闭、返回 true 将销毁窗口,返回 false 什么都不做。
bool HandleWindowClosing(wkeWebView webWindow, void* param)
{
Application* app = (Application*)param;
return IDYES == MessageBoxW(NULL, L"确定要退出程序吗?", L"wkexe", MB_YESNO|MB_ICONQUESTION);
}
// 回调:窗口已销毁
void HandleWindowDestroy(wkeWebView webWindow, void* param)
{
Application* app = (Application*)param;
app->window = NULL;
PostQuitMessage(0);
}
// 回调:文档加载成功
void HandleDocumentReady(wkeWebView webWindow, void* param)
{
wkeShowWindow(webWindow, TRUE);
}
// 回调:页面标题改变
void HandleTitleChanged(wkeWebView webWindow, void* param, const wkeString title)
{
wkeSetWindowTitleW(webWindow, wkeGetStringW(title));
}
// 回调:创建新的页面,比如说调用了 window.open 或者点击了 <a target="_blank" .../>
wkeWebView HandleCreateView(wkeWebView webWindow, void* param, wkeNavigationType navType, const wkeString url, const wkeWindowFeatures* features)
{
wkeWebView newWindow = wkeCreateWebWindow(WKE_WINDOW_TYPE_POPUP, NULL, features->x, features->y, features->width, features->height);
wkeShowWindow(newWindow, true);
return newWindow;
}
bool HandleLoadUrlBegin(wkeWebView webView, void* param, const char *url, void *job)
{
if (strcmp(url, "http://hook.test/") == 0) {
wkeNetSetMIMEType(job, "text/html");
wkeNetChangeRequestUrl(job, url);
wkeNetSetData(job, "<li>这是个hook页面</li><a herf=\"http://www.baidu.com/\">HookRequest</a>", sizeof("<li>这是个hook页面</li><a herf=\"http://www.baidu.com/\">HookRequest</a>"));
return true;
}
else if (strcmp(url, "http://www.baidu.com/") == 0) {
wkeNetHookRequest(job);
}
return false;
}
void HandleLoadUrlEnd(wkeWebView webView, void* param, const char *url, void *job, void* buf, int len)
{
wchar_t *str = L"百度一下";
wchar_t *str1 = L"HOOK一下";
int slen = ::WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
if (slen == 0) return;
char utf81[100];
::WideCharToMultiByte(CP_UTF8, 0, str, -1, &utf81[0], slen, NULL, NULL);
slen = ::WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
if (slen == 0) return;
char utf82[100];
::WideCharToMultiByte(CP_UTF8, 0, str1, -1, &utf82[0], slen, NULL, NULL);
const char *b = strstr(static_cast<const char*>(buf), &utf81[0]);
memcpy((void *)b, &utf82, slen);
return ;
}
// 创建主页面窗口
BOOL CreateWebWindow(Application* app)
{
if (app->options.transparent)
app->window = wkeCreateWebWindow(WKE_WINDOW_TYPE_TRANSPARENT, NULL, 0, 0, 640, 480);
else
app->window = wkeCreateWebWindow(WKE_WINDOW_TYPE_POPUP, NULL, 0, 0, 640, 480);
if (!app->window)
return FALSE;
wkeOnWindowClosing(app->window, HandleWindowClosing, app);
wkeOnWindowDestroy(app->window, HandleWindowDestroy, app);
wkeOnDocumentReady(app->window, HandleDocumentReady, app);
wkeOnTitleChanged(app->window, HandleTitleChanged, app);
wkeOnCreateView(app->window, HandleCreateView, app);
wkeOnLoadUrlBegin(app->window, HandleLoadUrlBegin, app);
wkeOnLoadUrlEnd(app->window, HandleLoadUrlEnd, app);
wkeMoveToCenter(app->window);
wkeLoadURLW(app->window, app->url);
return TRUE;
}
void PrintHelpAndQuit(Application* app)
{
PrintHelp();
PostQuitMessage(0);
}
void RunMessageLoop(Application* app)
{
MSG msg = { 0 };
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
void RunApplication(Application* app)
{
memset(app, 0, sizeof(Application));
if (!ProcessOptions(app))
{
PrintHelpAndQuit(app);
return;
}
if (!FixupHtmlUrl(app))
{
PrintHelpAndQuit(app);
//打开默认页面
wcsncpy_s(app->url, MAX_PATH, L"http://www.baidu.com", MAX_PATH);
}
if (!CreateWebWindow(app))
{
PrintHelpAndQuit(app);
return;
}
RunMessageLoop(app);
}
void QuitApplication(Application* app)
{
if (app->window)
{
wkeDestroyWebWindow(app->window);
app->window = NULL;
}
}